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;
}
Codeforces Round #678 (Div. 2)【ABCD】的更多相关文章
- Codeforces Round #682 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1438 A. Specific Tastes of Andre 题意 构造一个任意连续子数组元素之和为子数组长度倍数的数组. ...
- Codeforces Round #676 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1421 A. XORwice 题意 给出两个正整数 \(a.b\),计算 \((a \oplus x) + (b \oplus ...
- Codeforces Round #675 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1422 A. Fence 题意 给出三条边 $a,b,c$,构造第四条边使得四者可以围成一个四边形. 题解 $d = max( ...
- Codeforces Round #668 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1405 A. Permutation Forgery 题意 给出一个大小为 $n$ 的排列 $p$,定义 \begin{equ ...
- Codeforces Round #732 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1546 A. AquaMoon and Two Arrays 题意 给出两个大小为 \(n\) 的数组 \(a, b\) ,每 ...
- Codeforces Round #677 (Div. 3)【ABCDE】
比赛链接:https://codeforces.com/contest/1433 A. Boring Apartments 题解 模拟即可. 代码 #include <bits/stdc++.h ...
- Codeforces Round #382 Div. 2【数论】
C. Tennis Championship(递推,斐波那契) 题意:n个人比赛,淘汰制,要求进行比赛双方的胜场数之差小于等于1.问冠军最多能打多少场比赛.题解:因为n太大,感觉是个构造.写写小数据, ...
- Codeforces Round #684 (Div. 2)【ABC1C2】
比赛链接:https://codeforces.com/contest/1440 A. Buy the String 题解 枚举字符串中 \(0\) 或 \(1\) 的个数即可. 代码 #includ ...
- Codeforces Round #658 (Div. 2)【ABC2】
做完前四题还有一个半小时... 比赛链接:https://codeforces.com/contest/1382 A. Common Subsequence 题意 给出两个数组,找出二者最短的公共子序 ...
随机推荐
- Xamarin.Forms 5.0 来了
Xamarin.Forms 5.0 已经正式发布,并带来其新功能,具体看官方博客https://devblogs.microsoft.com/xamarin/xamarin-forms-5-0-is- ...
- Java 安全之Weblogic 2017-3248分析
Java 安全之Weblogic 2017-3248分析 0x00 前言 在开头先来谈谈前面的绕过方式,前面的绕过方式分别使用了streamMessageImpl 和MarshalledObject对 ...
- 【Linux】fdisk -l发现有加号"+"
今天分区不够了,打算扩下分区,想起当时创建机器的时候还有大约80多个G没有用,今天打算重新利用上 就用了fdisk /dev/sda 创建了两个分区,但是发现分区下面有加号,感到而别的困惑 最后在很多 ...
- 使用 gRPC-UI 调试.NET 5的gPRC服务
在上一篇文章中,我介绍了gRPCurl一个命令行工具,该工具可用于测试gRPC服务的端点,在本文中,我将向您介绍 gRPC-ui, 它可以作为Web工具使用,有点像Postman,但用于gRPC AP ...
- Spring Aop中四个重要概念,切点,切面,连接点,通知
1. 通知: 就是我们编写的希望Aop时执行的那个方法.我们通过Aop希望我们编写的方法在目标方法执行前执行,或者执行后执行.2. 切点:切点就是我们配置的满足我们条件的目标方法.比如我们规定:名字前 ...
- floating point
记录浮点数的单精度和双精度(IEEE754) 1.单精度(float) 1.定义:单精度占4字节/32位,其中1号位符号位,其次是8位阶码/指数(阶符+阶数),23位尾数(小数). 2.双精度(d ...
- matlab gui matlab gui 鼠标点击显示图像颜色值
首先看看效果 首先功能说明下,运行后通过myfile菜单打开一幅图片之后在axes中显示,由于要使用图片的放大缩小等功能将figure 的菜单栏与工具栏都一并打开了. 界面编程主要是callbac ...
- 02. struts2中Action名称的搜索顺序
搜索顺序 获得请求路径的URI,例如URL为:http://localhost:8080/struts2/path1/path2/path3/student.action 首先寻找namespace为 ...
- Memcached与Redis对比及其优劣分析
国外讨论 本文主要总结缓存Redis和Memcached的区别,总结之前先参考外国知乎上的一篇问答:<Is memcached a dinosaur in comparison to Redis ...
- javax.servlet.ServletException: No adapter for handler
问题描述: 我的web.xml如下: <?xml version="1.0" encoding="UTF-8"?> <web-app xmln ...