• Codeforces Global Round 21(只会俩题)


    A. NIT orz!

    time limit per test

    1 second

    memory limit per test

    512 megabytes

    input

    standard input

    output

    standard output

    NIT, the cleaver, is new in town! Thousands of people line up to orz him. To keep his orzers entertained, NIT decided to let them solve the following problem related to or𝑧or⁡z. Can you solve this problem too?

    You are given a 1-indexed array of 𝑛n integers, 𝑎a, and an integer 𝑧z. You can do the following operation any number (possibly zero) of times:

    • Select a positive integer 𝑖i such that 1≤𝑖≤𝑛1≤i≤n. Then, simutaneously set 𝑎𝑖ai to (𝑎𝑖or𝑧)(aior⁡z) and set 𝑧z to (𝑎𝑖and𝑧)(aiand⁡z). In other words, let 𝑥x and 𝑦y respectively be the current values of 𝑎𝑖ai and 𝑧z. Then set 𝑎𝑖ai to (𝑥or𝑦)(xor⁡y) and set 𝑧z to (𝑥and𝑦)(xand⁡y).

    Here oror and andand denote the bitwise operations OR and AND respectively.

    Find the maximum possible value of the maximum value in 𝑎a after any number (possibly zero) of operations.

    Input

    Each test contains multiple test cases. The first line contains the number of test cases 𝑡t (1≤𝑡≤1001≤t≤100). Description of the test cases follows.

    The first line of each test case contains two integers 𝑛n and 𝑧z (1≤𝑛≤20001≤n≤2000, 0≤𝑧<2300≤z<230).

    The second line of each test case contains 𝑛n integers 𝑎1a1,𝑎2a2,……,𝑎𝑛an (0≤𝑎𝑖<2300≤ai<230).

    It is guaranteed that the sum of 𝑛n over all test cases does not exceed 104104.

    Output

    For each test case, print one integer — the answer to the problem.

    Example

    input

    Copy

    5
    2 3
    3 4
    5 5
    0 2 4 6 8
    1 9
    10
    5 7
    7 15 30 29 27
    3 39548743
    10293834 10284344 13635445
    

    output

    Copy

    7
    13
    11
    31
    48234367
    

    Note

    In the first test case of the sample, one optimal sequence of operations is:

    • Do the operation with 𝑖=1i=1. Now 𝑎1a1 becomes (3or3)=3(3or⁡3)=3 and 𝑧z becomes (3and3)=3(3and⁡3)=3.
    • Do the operation with 𝑖=2i=2. Now 𝑎2a2 becomes (4or3)=7(4or⁡3)=7 and 𝑧z becomes (4and3)=0(4and⁡3)=0.
    • Do the operation with 𝑖=1i=1. Now 𝑎1a1 becomes (3or0)=3(3or⁡0)=3 and 𝑧z becomes (3and0)=0(3and⁡0)=0.

    After these operations, the sequence 𝑎a becomes [3,7][3,7], and the maximum value in it is 77. We can prove that the maximum value in 𝑎a can never exceed 77, so the answer is 77.

    In the fourth test case of the sample, one optimal sequence of operations is:

    • Do the operation with 𝑖=1i=1. Now 𝑎1a1 becomes (7or7)=7(7or⁡7)=7 and 𝑧z becomes (7and7)=7(7and⁡7)=7.
    • Do the operation with 𝑖=3i=3. Now 𝑎3a3 becomes (30or7)=31(30or⁡7)=31 and 𝑧z becomes (30and7)=6(30and⁡7)=6.
    • Do the operation with 𝑖=5i=5. Now 𝑎5a5 becomes (27or6)=31(27or⁡6)=31 and 𝑧z becomes (27and6)=2(27and⁡6)=2.

    代码如下:

    1. #include <iostream>
    2. #include <vector>
    3. using namespace std;
    4. const int N = 1e05 + 10;
    5. int main() {
    6. int _;
    7. cin >> _;
    8. while (_ --) {
    9. int n, m;
    10. cin >> n >> m;
    11. vector<int> q(n);
    12. for (int i = 0; i < n; i++) cin >> q[i];
    13. int maxn = 0;
    14. for (int i = 0; i < n; i++) {
    15. maxn = max(maxn, q[i] | m);
    16. }
    17. cout << maxn << endl;
    18. }
    19. return 0;
    20. }

     按位与很显然只能比原来的数字小,所以说z的数值是越来越小的,然后两个数按位或的话只能比这两个数都大,所以说问题就来了:

    How many operations will we perform?

    The answer is at most one!

    Suppose we can only perform exactly one operation. In this case the answer is 𝑆=max1≤𝑖≤𝑛(𝑎𝑖 or 𝑧)S=max1≤i≤n(ai or z). In fact, we can prove that this is the answer.

    Define 𝑎′𝑖ai′ as the value of 𝑎𝑖ai after some operations.

    It suffices to prove the answer will never exceed 𝑆S. Note that 𝑧z will always become a submask of itself after any number of operations, so 𝑎𝑖ai will always be a submask of (𝑎𝑖 or 𝑧)(ai or z) after any number of operations. This leads to the conclusion that 𝑎′𝑖≤(𝑎𝑖 or 𝑧)ai′≤(ai or z) for all 𝑖i. Thus max1≤𝑖≤𝑛𝑎′𝑖≤max1≤𝑖≤𝑛(𝑎𝑖 or 𝑧)=𝑆max1≤i≤nai′≤max1≤i≤n(ai or z)=S.

    Time complexity is 𝑂(𝑛)O(n).

    所以我们只能进行一次变换,找最大的。

    B:

    B. NIT Destroys the Universe

    time limit per test

    2 seconds

    memory limit per test

    512 megabytes

    input

    standard input

    output

    standard output

    For a collection of integers 𝑆S, define mex(𝑆)mex⁡(S) as the smallest non-negative integer that does not appear in 𝑆S.

    NIT, the cleaver, decides to destroy the universe. He is not so powerful as Thanos, so he can only destroy the universe by snapping his fingers several times.

    The universe can be represented as a 1-indexed array 𝑎a of length 𝑛n. When NIT snaps his fingers, he does the following operation on the array:

    • He selects positive integers 𝑙l and 𝑟r such that 1≤𝑙≤𝑟≤𝑛1≤l≤r≤n. Let 𝑤=mex({𝑎𝑙,𝑎𝑙+1,…,𝑎𝑟})w=mex⁡({al,al+1,…,ar}). Then, for all 𝑙≤𝑖≤𝑟l≤i≤r, set 𝑎𝑖ai to 𝑤w.

    We say the universe is destroyed if and only if for all 1≤𝑖≤𝑛1≤i≤n, 𝑎𝑖=0ai=0 holds.

    Find the minimum number of times NIT needs to snap his fingers to destroy the universe. That is, find the minimum number of operations NIT needs to perform to make all elements in the array equal to 00.

    Input

    Each test contains multiple test cases. The first line contains the number of test cases 𝑡t (1≤𝑡≤1041≤t≤104). Description of the test cases follows.

    The first line of each test case contains one integer 𝑛n (1≤𝑛≤1051≤n≤105).

    The second line of each test case contains 𝑛n integers 𝑎1a1, 𝑎2a2, ……, 𝑎𝑛an (0≤𝑎𝑖≤1090≤ai≤109).

    It is guaranteed that the sum of 𝑛n over all test cases does not exceed 2⋅1052⋅105.

    Output

    For each test case, print one integer — the answer to the problem.

    Example

    input

    Copy

    4
    4
    0 0 0 0
    5
    0 1 2 3 4
    7
    0 2 3 0 1 2 0
    1
    1000000000
    

    output

    Copy

    0
    1
    2
    1
    

    Note

    In the first test case, we do 00 operations and all elements in the array are already equal to 00.

    In the second test case, one optimal way is doing the operation with 𝑙=2l=2, 𝑟=5r=5.

    In the third test case, one optimal way is doing the operation twice, respectively with 𝑙=4l=4, 𝑟=4r=4 and 𝑙=2l=2, 𝑟=6r=6.

    In the fourth test case, one optimal way is doing the operation with 𝑙=1l=1, 𝑟=1r=1.

    code:

    1. #include <iostream>
    2. #include <vector>
    3. using namespace std;
    4. int main() {
    5. ios::sync_with_stdio(false);
    6. cin.tie(0);
    7. int _;
    8. cin >> _;
    9. while (_ --) {
    10. int n, m;
    11. cin >> n;
    12. vector<int> q(n + 1);
    13. for (int i = 0; i < n; i++) cin >> q[i];
    14. int ans = 0;
    15. for (int i = 0; i < n; i++) ans += (q[i] > 0);
    16. for (int i = 0; i < n - 1; i++) ans -= (q[i] > 0 && q[i + 1] > 0);
    17. ans = min(2, ans);
    18. cout << ans << endl;
    19. }
    20. return 0;
    21. }

    答案只有012三种。

  • 相关阅读:
    C#获取一个固定范围内的随机数
    如何在不同平台上搭建Flutter开发环境
    Discrete Mathematics and Its Applications 8th Edition 目录
    【校招VIP】产品项目计划之功能分析
    Linux下安装Docker(centOS 8)
    Python使用EasyOCR库对行程码图片进行OCR文字识别介绍与实践
    【前端】WebWorker 在前端SPA框架的应用
    Node.js中的回调地狱
    每日OJ题_其它背包问题④_力扣96. 不同的二叉搜索树(卡特兰数)
    干洗店洗鞋店软件模版,成品系统功能非常完善;
  • 原文地址:https://blog.csdn.net/beloved_yu/article/details/125468758