Codeforces Round #634 (Div. 3)

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

A. Candies and Two Sisters

题意

把一个数拆成两个不等的数有多少种情况。

思路

奇数时除以二即可,偶数时需要再减去相等的情况。

代码

#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n; cin >> n;
    if (n&1) cout << n / 2 << "
";
    else cout << n / 2 - 1 << "
";
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}

B. Construct the String

题意

构造一个长 $n$ 的字符串使得每个长 $a$ 的连续子串都恰有 $b$ 个不同的字母。

思路

只用 $b$ 个字母构造即可。

代码

#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n, a, b; cin >> n >> a >> b;
    for (int i = 0; i < n; i++) {
        cout << char('a' + i % b);
    }
    cout << "
";
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}

C. Two Teams Composing

题意

在一个数组中挑些数分为两组,一组全不相同,一组全部相同。

思路

记录只有一个的数的个数和一个数的最多个数,如果后者与前者差大于等于 $2$,那么第二个数组可以再给第一个数组一个数,否则取两者中的较小值,最后再特判样例中的两种情况即可。

代码

#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n; cin >> n;
    map<int, int> m;
    int c[n]; for (int & i : c) cin >> i, ++m[i];
    int a = 0, b = 0;
    for (auto i : m) 
        if (i.second == 1) ++a;
        else {
            if (b) ++a;
            b = max(b, i.second);
        }
    if (n == 1)
        cout << 0 << "
";
    else if (b == 0)
        cout << 1 << "
";
    else if (b - a >= 2) 
        cout << a + 1 << "
";
    else
        cout << min(a, b) << "
"; 
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}

D. Anti-Sudoku

题意

将一个 $9{ imes}9$ 的数独方阵改为反数独方阵,即行、列、$9$ 个 $3{ imes}3$ 的方阵中都至少有两个数相同,最多改动 $9$ 个数。

思路

在 $9$ 个方阵中不重复地选取 $9$ 个行和列,改变这 $9$ 个行列交点的数即可。

其实利用数独的性质把一个数都改成另一个数就可以了,因为在正确的数独方阵中每个数都对应九组不重复的行、列、方阵。

代码

#include <bits/stdc++.h>
using namespace std;

void solve() {
    char MP[10][10] = {};
    for (int i = 1; i <= 9; i++) {
        for (int j = 1; j <= 9; j++) {
            cin >> MP[i][j];
        }
    }
    bool vis_row[10] = {};
    bool vis_col[10] = {};
    for (int i = 0; i < 3; i++) { //以3x3方块为单位进行变更
        for (int j = 0; j < 3; j++) {
            bool flag = false;
            for (int x = 1 + 3 * i; x <= 3 * (i + 1); x++) {
                for (int y = 1 + 3 * j; y <= 3 * (j + 1); y++) {
                    if (!vis_row[x] and !vis_col[y]) {
                        if (MP[x][y] == '9') MP[x][y] = '1';
                        else MP[x][y]++;
                        vis_row[x] = vis_col[y] = true;
                        flag = true;
                        break;
                    }
                }
                if (flag) break;
            }
        }
    }
    for (int i = 1; i <= 9; i++) {
        for (int j = 1; j <= 9; j++) {
            cout << MP[i][j];
        }
        cout << "
";
    }
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}

EF待填。

原文地址:https://www.cnblogs.com/Kanoon/p/12695395.html