• Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)


      第一次点进去,结果忘记参赛了。。。但还是做了2个题(水平低到没底。。请轻虐。。)下面是题和我做的不确定是否正确的解决方案。

      原题链接:http://codeforces.com/contest/1025


    A. Doggo Recoloring
    time limit per test:
    1 second
    memory limit per test:
    256 megabytes
    input:
    standard input
    output:
    standard output

    Panic is rising in the committee for doggo standardization — the puppies of the new brood have been born multi-colored! In total there are 26 possible colors of puppies in the nature and they are denoted by letters from 'a' to 'z' inclusive.

    The committee rules strictly prohibit even the smallest diversity between doggos and hence all the puppies should be of the same color. Thus Slava, the committee employee, has been assigned the task to recolor some puppies into other colors in order to eliminate the difference and make all the puppies have one common color.

    Unfortunately, due to bureaucratic reasons and restricted budget, there's only one operation Slava can perform: he can choose a color xxsuch that there are currently at least two puppies of color xx and recolor all puppies of the color xx into some arbitrary color yy. Luckily, this operation can be applied multiple times (including zero).

    For example, if the number of puppies is 77 and their colors are represented as the string "abababc", then in one operation Slava can get the results "zbzbzbc", "bbbbbbc", "aaaaaac", "acacacc" and others. However, if the current color sequence is "abababc", then he can't choose xx='c' right now, because currently only one puppy has the color 'c'.

    Help Slava and the committee determine whether it is possible to standardize all the puppies, i.e. after Slava's operations all the puppies should have the same color.

    Input

    The first line contains a single integer nn (1n1051≤n≤105) — the number of puppies.

    The second line contains a string ss of length nn consisting of lowercase Latin letters, where the ii-th symbol denotes the ii-th puppy's color.

    Output

    If it's possible to recolor all puppies into one color, print "Yes".

    Otherwise print "No".

    Output the answer without quotation signs.

    Examples
    input
    6
    aabddc
    output
    Yes
    input
    3
    abc
    output
    No
    input
    3
    jjj
    output
    Yes
    Note

    In the first example Slava can perform the following steps:

    1. take all puppies of color 'a' (a total of two) and recolor them into 'b';
    2. take all puppies of color 'd' (a total of two) and recolor them into 'c';
    3. take all puppies of color 'b' (three puppies for now) and recolor them into 'c'.

    In the second example it's impossible to recolor any of the puppies.

    In the third example all the puppies' colors are the same; thus there's no need to recolor anything.

    #include <bits/stdc++.h>
    #define Index(ch) (ch - 'a')
    const int N = 100000;
    int has[26];
    void init(char*c, int s)
    {
        memset(has, 0, 26);
        for (int i = 0; i < s; i++)
            has[Index(c[i])]++;
    }
    int main()
    {
        char cors[N];int dogs;
        while (~scanf("%d", &dogs))
        {
            if (dogs > 1 && dogs < N)
            {
                scanf("%s", cors);
                init(cors, dogs);
                for (int i = 0; i < 26; i++)
                    if (has[i] >= 2)
                    {
                        dogs = 0;
                        break;
                    }
                if (!dogs)
                    puts("Yes");
                else
                    puts("No");
            }
            else
                puts("No");
        }
        return 0;
    }
    

    B. Weakened Common Divisor
    time limit per test:
    1.5 seconds
    memory limit per test:
    256 megabytes
    input:
    standard input
    output:
    standard output

    During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers.

    For a given list of pairs of integers (a1,b1)(a1,b1), (a2,b2)(a2,b2), ..., (an,bn)(an,bn) their WCD is arbitrary integer greater than 11, such that it divides at least one element in each pair. WCD may not exist for some lists.

    For example, if the list looks like [(12,15),(25,18),(10,24)][(12,15),(25,18),(10,24)], then their WCD can be equal to 22, 33, 55 or 66 (each of these numbers is strictly greater than 11 and divides at least one number in each pair).

    You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently.

    Input

    The first line contains a single integer nn (1n1500001≤n≤150000) — the number of pairs.

    Each of the next nn lines contains two integer values aiai, bibi (2ai,bi21092≤ai,bi≤2⋅109).

    Output

    Print a single integer — the WCD of the set of pairs.

    If there are multiple possible answers, output any; if there is no answer, print 1−1.

    Examples
    input
    3
    17 18
    15 24
    12 15
    output
    6
    input
    2
    10 16
    7 17
    output
    -1
    input
    5
    90 108
    45 105
    75 40
    165 175
    33 30
    output
    5
    Note

    In the first example the answer is 66 since it divides 1818 from the first pair, 2424 from the second and 1212 from the third ones. Note that other valid answers will also be accepted.

    In the second example there are no integers greater than 11 satisfying the conditions.

    In the third example one of the possible answers is 55. Note that, for example, 1515 is also allowed, but it's not necessary to maximize the output.

    #include <bits/stdc++.h>
    const int N = 150000;
    int a[N], b[N];
    int gcd(int a, int b)
    {
        if (b == 0) return a;
        return gcd(b, a%b);
    }
    int main()
    {
        int s;
        while (~scanf("%d", &s))
        {
            int p1, p2, p3, p4, f1 = 0, f2 = 0, f3 = 0, f4 = 0;
            for (int i = 0; i < s; i++)
                scanf("%d %d", &a[i], &b[i]);
            p1 = gcd(a[0], a[1]);
            p2 = gcd(a[0], b[1]);
            p3 = gcd(b[0], a[1]);
            p4 = gcd(b[0], b[1]);
    
            if (p1 > 1)
            {
                f1 = p1;
                for (int i = 2; i < s; i++)
                    p1 = gcd(p1, a[i]);
                for (int i = 2; i < s; i++)
                    f1 = gcd(f1, b[i]);
            }
            if (p2 > 1)
            {
                f2 = p2;
                for (int i = 2; i < s; i++)
                    p2 = gcd(p2, a[i]);
                for (int i = 2; i < s; i++)
                    f2 = gcd(f2, b[i]);
            }
            if (p3 > 1)
            {
                f3 = p3;
                for (int i = 2; i < s; i++)
                    p3 = gcd(p3, a[i]);
                for (int i = 2; i < s; i++)
                    f3 = gcd(f3, b[i]);
            }
            if (p4 > 1)
            {
                f4 = p4;
                for (int i = 2; i < s; i++)
                    p4 = gcd(p4, a[i]);
                for (int i = 2; i < s; i++)
                    f4 = gcd(f4, b[i]);
            }
            if (p1 > 1) printf("%d
    ", p1);
            else if (f1 > 1) printf("%d
    ", f1);
            else if (p2 > 1) printf("%d
    ", p2);
            else if (f2 > 1) printf("%d
    ", f2);
            else if (p3 > 1) printf("%d
    ", p3);
            else if (f3 > 1) printf("%d
    ", f3);
            else if (p4 > 1) printf("%d
    ", p4);
            else if (f4 > 1) printf("%d
    ", f4);
            else
                puts("-1");
        }
        return 0;
    }
    

    D. Recovering BST
    time limit per test:
    1 second
    memory limit per test:
    256 megabytes
    input:
    standard input
    output:
    standard output

    Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees!

    Recently he found a binary search tree and instinctively nibbled all of its edges, hence messing up the vertices. Dima knows that if Andrew, who has been thoroughly assembling the tree for a long time, comes home and sees his creation demolished, he'll get extremely upset.

    To not let that happen, Dima has to recover the binary search tree. Luckily, he noticed that any two vertices connected by a direct edge had their greatest common divisor value exceed 11.

    Help Dima construct such a binary search tree or determine that it's impossible. The definition and properties of a binary search tree can be found here.

    Input

    The first line contains the number of vertices nn (2n7002≤n≤700).

    The second line features nn distinct integers aiai (2ai1092≤ai≤109) — the values of vertices in ascending order.

    Output

    If it is possible to reassemble the binary search tree, such that the greatest common divisor of any two vertices connected by the edge is greater than 11, print "Yes" (quotes for clarity).

    Otherwise, print "No" (quotes for clarity).

    Examples
    input
    6
    3 6 9 18 36 108
    output
    Yes
    input
    2
    7 17
    output
    No
    input
    9
    4 8 10 12 15 18 33 44 81
    output
    Yes
    Note

    The picture below illustrates one of the possible trees for the first example.

    The picture below illustrates one of the possible trees for the third example.

    还在推dp方程中。。。)方程是推不出来了(> <),直接看别人的题解吧:

      http://codeforces.com/contest/1025/submission/41898155 Orz...

      这里还有一份是循环的区间dp的解法 Orz....:

    #include<bits/stdc++.h>
    int n,s[704],g[705][705],L[705][705],R[705][705],f[705][705];
    int main()
    {
        for(int i = 0;i <= (n=s[0]); i++)
            scanf("%d",&s[i]);
    
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                g[i][j]=std::__gcd(s[i],s[j])>1;
    
        for(int i=1;i<=n;i++)
            L[i][i]=R[i][i]=1;
    
        for(int i=n;i>=1;i--)
            for(int j=i;j<=n;j++)
                for(int k=i;k<=j;k++)
                    if(L[i][k] && R[k][j])
                        f[i][j] |= 1,
                        R[i-1][j] |= g[k][i-1],
                        L[i][j+1] |= g[k][j+1];
    
        return puts(f[1][n]?"Yes":"No"),0;
    }
    

      

      

  • 相关阅读:
    LeetCode:12. Roman to Integer (Easy)
    Python:正则表达式—— re 模块
    RAID(冗余硬盘阵列)
    Mac OS下搭建Hadoop + Spark集群
    LeetCode:12. Integer to Roman(Medium)
    js事件中绑定另一事件导致事件多次执行
    ie8以上及非ie8情况下的写法
    javascript闭包
    bootstrap-datetimepicker年视图中endDate设置之后比正常时间提前两个月
    javascript的阻塞机制
  • 原文地址:https://www.cnblogs.com/darkchii/p/9506551.html
Copyright © 2020-2023  润新知