Codeforces Round #682 (Div. 2)【ABCD】

比赛链接:https://codeforces.com/contest/1438

A. Specific Tastes of Andre

题意

构造一个任意连续子数组元素之和为子数组长度倍数的数组。

题解

构造全为同一值的任意数组即可。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        for (int i = 0; i < n; i++) {
            cout << 1 << " \n"[i == n - 1];
        }
    }
    return 0;
}

B. Valerii Against Everyone

题意

给出一个大小为 \(n\) 的数组 \(b\)\(a_i = 2^{b_i}\) ,判断数组 \(a\) 中是否存在和相同的两个不相交的连续子数组。

题解

判断 \(b\) 中是否有一个数出现了两次即可。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        map<int, int> mp;
        bool ok = false;
        for (int i = 0; i < n; i++) {
            int x;
            cin >> x;
            if (++mp[x] == 2) ok = true;
        }
        cout << (ok ? "YES" : "NO") << "\n";
    }
    return 0;
}

C. Engineer Artem

题意

给出一个 \(n \times m\) 的矩阵,给其中一些元素加一使得不存在两个相邻元素相等。

题解

像国际象棋棋盘那样把每个数的奇偶性对应到相应的黑白格即可。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n >> m;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int a;
                cin >> a;
                cout << a + (a % 2 != (i + j) % 2) << " \n"[j == m - 1];
            }
        }
    }
    return 0;
}

D. Powerful Ksenia

题意

给出一个大小为 \(n\) 的数组 \(a\) ,每次操作如下:

  • 选择三个不同的下标 \(i,\ j,\ k\)
  • \(a_i = a_j = a_k = a_i \oplus a_j \oplus a_k\)

问能否在 \(n\) 次操作内将 \(a\)\(n\) 个元素变为同一值,如果可以,给出操作过程。

题解

  • \(n\) 为奇数的话正反各来一遍即可,由于最后两个数不能作为起点,所以共需 \(n - 2\) 次操作。
  • \(n\) 为偶数的话需要判断前 \(n - 1\) 个数的异或和是否等于第 \(n\) 个数,即 \(n\) 个数的异或和是否为 \(0\) ,如果等于,去掉第 \(n\) 个数即为和奇数相同的情况。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    if (n & 1) {
        cout << "YES" << "\n";
        cout << n - 2 << "\n";
        for (int i = 1; i + 2 <= n; i += 2) cout << i << ' ' << i + 1 << ' ' << i + 2 << "\n";
        for (int i = n - 4; i >= 1; i -= 2) cout << i << ' ' << i + 1 << ' ' << i + 2 << "\n";
    } else {
        int xor_sum = 0;
        for (int i = 0; i < n; i++) {
            int a;
            cin >> a;
            xor_sum ^= a;
        }
        if (xor_sum != 0) {
            cout << "NO" << "\n";
        } else {
            --n;
            cout << "YES" << "\n";
            cout << n - 2 << "\n";
            for (int i = 1; i + 2 <= n; i += 2) cout << i << ' ' << i + 1 << ' ' << i + 2 << "\n";
            for (int i = n - 4; i >= 1; i -= 2) cout << i << ' ' << i + 1 << ' ' << i + 2 << "\n";
        }
    }
    return 0;
}
posted @ 2020-11-14 23:30  Kanoon  阅读(146)  评论(0编辑  收藏  举报