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

Codeforces Round #678 (Div. 2)【ABCD】的更多相关文章

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

    比赛链接:https://codeforces.com/contest/1438 A. Specific Tastes of Andre 题意 构造一个任意连续子数组元素之和为子数组长度倍数的数组. ...

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

    比赛链接:https://codeforces.com/contest/1421 A. XORwice 题意 给出两个正整数 \(a.b\),计算 \((a \oplus x) + (b \oplus ...

  3. Codeforces Round #675 (Div. 2)【ABCD】

    比赛链接:https://codeforces.com/contest/1422 A. Fence 题意 给出三条边 $a,b,c$,构造第四条边使得四者可以围成一个四边形. 题解 $d = max( ...

  4. Codeforces Round #668 (Div. 2)【ABCD】

    比赛链接:https://codeforces.com/contest/1405 A. Permutation Forgery 题意 给出一个大小为 $n$ 的排列 $p$,定义 \begin{equ ...

  5. Codeforces Round #732 (Div. 2)【ABCD】

    比赛链接:https://codeforces.com/contest/1546 A. AquaMoon and Two Arrays 题意 给出两个大小为 \(n\) 的数组 \(a, b\) ,每 ...

  6. Codeforces Round #677 (Div. 3)【ABCDE】

    比赛链接:https://codeforces.com/contest/1433 A. Boring Apartments 题解 模拟即可. 代码 #include <bits/stdc++.h ...

  7. Codeforces Round #382 Div. 2【数论】

    C. Tennis Championship(递推,斐波那契) 题意:n个人比赛,淘汰制,要求进行比赛双方的胜场数之差小于等于1.问冠军最多能打多少场比赛.题解:因为n太大,感觉是个构造.写写小数据, ...

  8. Codeforces Round #684 (Div. 2)【ABC1C2】

    比赛链接:https://codeforces.com/contest/1440 A. Buy the String 题解 枚举字符串中 \(0\) 或 \(1\) 的个数即可. 代码 #includ ...

  9. Codeforces Round #658 (Div. 2)【ABC2】

    做完前四题还有一个半小时... 比赛链接:https://codeforces.com/contest/1382 A. Common Subsequence 题意 给出两个数组,找出二者最短的公共子序 ...

随机推荐

  1. Oracle数据库基础操作语法

    转载自:https://www.cnblogs.com/fallen-seraph/p/10685997.html 一.登录Oracle数据库 首先运行Oracle数据库: 默认的有两个账号: 管理员 ...

  2. 在Linux系统下限制指定目录的大小以及文件/文件夹数量

    背景说明 在Linux操作系统下有时需要限制一个指定文件夹的大小和文件夹内可存储的文件数量,有可能是出于安全的考量或者定制化的配置,这里我们提供了一种方案:用dd创建一个空的img镜像,进行格式化的配 ...

  3. .NET Core引入日志(Log4Net篇)

    Demo版本信息如下: VS:2019 框架:.Net Core 3.1 Log4Net:2.0.12 思维导图: [1]添加依赖项 通过nuget添加Log4Net [2]创建公共类 添加公共类Lo ...

  4. python模块详解 | unittest(单元测试框架)(持续更新中)

    目录: why unittest? unittest的四个重要概念 加载测试用例的三个方法 自动加载测试用例 忽略测试和预期失败 生成html测试报告 why unittest? 简介: Unitte ...

  5. ElasticSearch极简入门总结

    一,目录 安装es 项目添加maven依赖 es客户端组件注入到spring容器中 es与mysql表结构对比 索引的删除创建 文档的crud es能快速搜索的核心-倒排索引 基于倒排索引的精确搜索. ...

  6. 中间件:ElasticSearch组件RestHighLevelClient用法详解

    本文源码:GitHub·点这里 || GitEE·点这里 一.基础API简介 1.RestHighLevelClient RestHighLevelClient的API作为ElasticSearch备 ...

  7. [APUE] 进程环境

    APUE 一书的第七章学习笔记. 进程终止 有 8 种方式可以使得进程终止,5 种为正常方式: Return from main Calling exit() Calling _exit or _Ex ...

  8. 避免重复提交?分布式服务的幂等性设计! 架构文摘 今天 点击蓝色“架构文摘”关注我哟 加个“星标”,每天上午 09:25,干货推送! 来源:https://www.cnblogs.com/QG-whz/p/10372458.html 作者:melonstreet

    避免重复提交?分布式服务的幂等性设计! 架构文摘  今天 点击蓝色"架构文摘"关注我哟    加个"星标",每天上午 09:25,干货推送!      来源:h ...

  9. udp 连接

    在今天的内容里,我对 UDP 套接字调用 connect 方法进行了深入的分析.之所以对 UDP 使用 connect,绑定本地地址和端口,是为了让我们的程序可以快速获取异步错误信息的通知,同时也可以 ...

  10. vscode远程开发安装

    https://www.cnblogs.com/xiaoqi/p/vs-code-remote.html ============================= https://blog.csdn ...