• Codeforces Round #696 (Div. 2) 解题报告


    A. Puzzle From the Future

    time limit per test 1 second
    memory limit per test 256 megabytes
    input standard input
    output standard output

    In the 2022 year, Mike found two binary integers a and b of length n (both of them are written only by digits 0 and 1) that can have leading zeroes. In order not to forget them, he wanted to construct integer d in the following way:

    he creates an integer c as a result of bitwise summing of a and b without transferring carry, so c may have one or more 2-s. For example, the result of bitwise summing of 0110 and 1101 is 1211 or the sum of 011000 and 011000 is 022000;
    after that Mike replaces equal consecutive digits in c by one digit, thus getting d. In the cases above after this operation, 1211 becomes 121 and 022000 becomes 020 (so, d won't have equal consecutive digits).
    Unfortunately, Mike lost integer a before he could calculate d himself. Now, to cheer him up, you want to find any binary integer a of length n such that d will be maximum possible as integer.

    Maximum possible as integer means that 102>21, 012<101, 021=21 and so on.

    Input
    The first line contains a single integer t (1≤t≤1000) — the number of test cases.

    The first line of each test case contains the integer n (1≤n≤105) — the length of a and b.

    The second line of each test case contains binary integer b of length n. The integer b consists only of digits 0 and 1.

    It is guaranteed that the total sum of n over all t test cases doesn't exceed 105.

    Output
    For each test case output one binary integer a of length n. Note, that a or b may have leading zeroes but must have the same length n.

    Example
    inputCopy
    5
    1
    0
    3
    011
    3
    110
    6
    111000
    6
    001011
    outputCopy
    1
    110
    100
    101101
    101110
    Note
    In the first test case, b=0 and choosing a=1 gives d=1 as a result.

    In the second test case, b=011 so:

    if you choose a=000, c will be equal to 011, so d=01;
    if you choose a=111, c will be equal to 122, so d=12;
    if you choose a=010, you'll get d=021.
    If you select a=110, you'll get d=121.
    We can show that answer a=110 is optimal and d=121 is maximum possible.
    In the third test case, b=110. If you choose a=100, you'll get d=210 and it's the maximum possible d.

    In the fourth test case, b=111000. If you choose a=101101, you'll get d=212101 and it's maximum possible d.

    In the fifth test case, b=001011. If you choose a=101110, you'll get d=102121 and it's maximum possible d.

    题目大意:
    T组样例,对于每组样例,输入一个n,再输入一个长度为n的01串,你需要构造一个长度为n的01串,使得这两个串相加的值尽可能大,注意:这两个串的和不能有连续相同的数字,形如221不可以, 212则可以,要求输出构造好的串。

    解题思路:
    因为要尽可能大,所以第一位一定是1,并把s[0] ++,贪心一下即可得到答案,如果s[i] - s[i - 1]恰好等于1,则说明这一位不能加了,因为会出现连续数字,其他情况均为1,边构造边修改原串。

    Code:

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
     
    using namespace std;
     
    const int N = 1e5 + 10;
     
    int T, n;
    string s, res;
     
    int main() {
        cin >> T;
        while (T --) {
            cin >> n >> s;
            res = "1";
            s[0] ++;
            for (int i = 1; i < s.size(); i ++)
                if (s[i - 1] - s[i] == 1) res += '0';
                else res += '1', s[i] ++;
            cout << res << endl;
        }
        
        return 0;
    }
    
    

    B. Different Divisors

    time limit per test 1 second
    memory limit per test 256 megabytes
    input standard input
    output standard output

    Positive integer x is called divisor of positive integer y, if y is divisible by x without remainder. For example, 1 is a divisor of 7 and 3 is not divisor of 8.

    We gave you an integer d and asked you to find the smallest positive integer a, such that

    a has at least 4 divisors;
    difference between any two divisors of a is at least d.
    Input
    The first line contains a single integer t (1≤t≤3000) — the number of test cases.

    The first line of each test case contains a single integer d (1≤d≤10000).

    Output
    For each test case print one integer a — the answer for this test case.

    Example
    inputCopy
    2
    1
    2
    outputCopy
    6
    15
    Note
    In the first test case, integer 6 have following divisors: [1,2,3,6]. There are 4 of them and the difference between any two of them is at least 1. There is no smaller integer with at least 4 divisors.

    In the second test case, integer 15 have following divisors: [1,3,5,15]. There are 4 of them and the difference between any two of them is at least 2.

    The answer 12 is INVALID because divisors are [1,2,3,4,6,12]. And the difference between, for example, divisors 2 and 3 is less than d=2.

    题目大意:
    T组测试数据,每组一个d,你需要找到符合条件的最小正整数。这个数满足:

    • 至少存在4个约数
    • 约数之前相差至少为d

    输出这个数。

    解题思路:
    预处理一下2e4以内的素数(此题枚举也可),第一个约数一定是1,我们需要找到x >= 1 + d,再找到y >=x + d,x和y均为最小素数,最后输出x * y 即可。

    Code:

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
     
    using namespace std;
     
    const int N = 1e5 + 10;
     
    int T, d;
    bool st[N];
     
    void init() {
        for (int i = 2; i < N; i ++)
            if (!st[i]) for (int j = 2 * i; j < N; j += i) st[j] = true;
    }
     
    int main() {
        init();
        cin >> T;
        while (T --) {
            cin >> d;
            int x, y;
            int t = 1 + d;
            while (st[t]) t ++;
            x = t;
            t += d;
            while (st[t]) t ++;
            y = t;
            cout << x *y << endl;
        }
        
        return 0;
    }
    

    C. Array Destruction

    time limit per test 1 second
    memory limit per test 256 megabytes
    input standard input
    output standard output

    You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a.

    It could have been an easy task, but it turned out that you should follow some rules:

    In the beginning, you select any positive integer x.
    Then you do the following operation n times:
    select two elements of array with sum equals x;
    remove them from a and replace x with maximum of that two numbers.
    For example, if initially a=[3,5,1,2], you can select x=6. Then you can select the second and the third elements of a with sum 5+1=6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation.

    Note, that you choose x before the start and can't change it as you want between the operations.

    Determine how should you behave to throw out all elements of a.

    Input
    The first line contains a single integer t (1≤t≤1000) — the number of test cases.

    The first line of each test case contains the single integer n (1≤n≤1000).

    The second line of each test case contains 2n integers a1,a2,…,a2n (1≤ai≤106) — the initial array a.

    It is guaranteed that the total sum of n over all test cases doesn't exceed 1000.

    Output
    For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise.

    If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove.

    Example
    inputCopy
    4
    2
    3 5 1 2
    3
    1 1 8 8 64 64
    2
    1 1 2 4
    5
    1 2 3 4 5 6 7 14 3 11
    outputCopy
    YES
    6
    1 5
    2 3
    NO
    NO
    YES
    21
    14 7
    3 11
    5 6
    2 4
    3 1
    Note
    The first test case was described in the statement.

    In the second and third test cases, we can show that it is impossible to throw out all elements of array a.

    题目大意:
    T组样例。每组输入一个n,接下来有2 * n个整数,你可以任选一个数x作为起点,当且仅当a[i] + a[j](i != j) == x时,从数组中删除这两个数,同时x = max(a[i], a[jj]) 询问是否能清空整个数组,输出YES or NO yes时打印方案。

    解题思路:
    此题的性质时,x的变换一定是递减的。所以一定要从大到小消,看出这个性质就很好做了,枚举起点即可。从大到小排序,起点为a[1] + a[x](2 <= x <= 2 * n),之后的顺序一定是固定的,按照题意模拟即可。

    Code:

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <vector>
    #include <map>
     
    using namespace std;
     
    typedef pair<int, int> PII;
     
    const int N = 2010;
     
    int T, n, res;
    int a[N];
    bool st[N];
    map<int, int> mp;
    vector<PII > v;
     
    vector<PII > check(map<int, int> m, int x) {
        vector<PII > ans;
        for (int i = 1; i <= 2 * n; i ++) {
            if (!m[a[i]]) continue;
            m[a[i]] --;
            if (m[x - a[i]]) ans.push_back({a[i], x - a[i]}), m[x - a[i]] --;
            else return {};
            x = max(a[i], x - a[i]);
        }
        
        return ans;
    }
     
    int main() {
        cin >> T;
        while (T --) {
            mp.clear();
            v.clear();
            cin >> n;
            for (int i = 1; i <= 2 * n; i ++) {
                scanf("%d", a + i);
                mp[a[i]] ++;
            }
            sort(a + 1, a + 1 + 2 * n, greater<int>());
            bool ok = false;
            for (int i = 2; i <= 2 * n; i ++) {
                res = a[1] + a[i];
                v = check(mp, res);
                if (v.size()) break;
            }
            
            if (v.size()) {
                puts("YES");
                cout << res << endl;
                for (auto it : v) cout << it.first << ' ' << it.second << endl;
            }
            else puts("NO");
        }
     
        return 0;
    }
    
  • 相关阅读:
    小D课堂
    小D课堂
    小D课堂
    小D课堂
    小D课堂
    小D课堂
    小D课堂
    阶段3 3.SpringMVC·_07.SSM整合案例_09.ssm整合之Spring整合MyBatis框架配置事务
    阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
    阶段3 3.SpringMVC·_07.SSM整合案例_07.ssm整合之编写MyBatis框架测试保存的方法
  • 原文地址:https://www.cnblogs.com/Hayasaka/p/14303290.html
Copyright © 2020-2023  润新知