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

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

A. Reorder

题解

模拟一下这个二重循环发现每个位置数最终都只加了一次。

代码

#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;
        int sum = 0;
        for (int i = 0; i < n; i++) {
            int x;
            cin >> x;
            sum += x;
        }
        cout << (sum == m ? "YES" : "NO") << "\n";
    }
    return 0;
}

B. Prime Square

题解

沿着对角线填 \(2 \times 2\)\(1\) 矩阵即可。

代码

#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;
        int a[n][n] = {};
        for (int i = 0; i < n; i++) {
            a[i][i] = 1;
            if (i + 1 < n) {
                a[i][i + 1] = 1;
                a[i + 1][i] = 1;
                a[i + 1][i + 1] = 1;
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cout << a[i][j] << " \n"[j == n - 1];
            }
        }
    }
    return 0;
}

C. Binary Search

题解

模拟二分算法,将判断条件由值的大小变为位置的左右,在 \(pos\) 左边即说明这个数小于 \(x\),在 \(pos\) 右边即说明这个数大于 \(x\),答案即 \(A_{n - x}^{big} \times A_{x - 1}^{small} \times A_{other}^{ohter}\)

代码

#include <bits/stdc++.h>
using namespace std;
constexpr int MOD = 1e9 + 7;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, x, pos;
    cin >> n >> x >> pos;
    int big = 0, small = 0;
    int l = 0, r = n;
    while (l < r) {
        int mid = (l + r) / 2;
        if (mid <= pos) {
            if (mid != pos) ++small;
            l = mid + 1;
        } else {
            ++big;
            r = mid;
        }
    }
    auto A = [](int n, int m) {
        long long res = 1;
        for (int i = 0; i < m; i++) res = res * (n - i) % MOD;
        return res;
    };
    int other = n - big - small - 1;
    cout << A(n - x, big) * A(x - 1, small) %MOD * A(other, other) % MOD << "\n";
    return 0;
}

D. Bandit in a City

题解

最终所有结点的人都要分流到叶子结点,所以关键是叶子结点的个数,将叶子结点外的父节点 \(sz\) 都设为 \(0\),由下往上汇总判断即可。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<vector<int>> G(n);
    vector<int> sz(n, 1);
    for (int v = 1; v < n; v++) {
        int u;
        cin >> u;
        --u;
        sz[u] = 0;
        G[u].push_back(v);
    }
    vector<long long> a(n);
    for (auto &x : a) cin >> x;
    long long ans = 0;
    function<void(int)> dfs = [&](int u) {
        for (auto v : G[u]) {
            dfs(v);
            a[u] += a[v];
            sz[u] += sz[v];
        }
        ans = max(ans, (a[u] + sz[u] - 1) / sz[u]);
    };
    dfs(0);
    cout << ans << "\n";
    return 0;
}
posted @ 2020-10-25 08:30  Kanoon  阅读(407)  评论(0编辑  收藏  举报