ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2020-10-25 08:32:15  阅读:283  来源: 互联网

标签:ABCD sz 678 int 题解 cin ++ Div main


比赛链接: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;
}

标签:ABCD,sz,678,int,题解,cin,++,Div,main
来源: https://www.cnblogs.com/Kanoon/p/13871773.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有