比赛链接: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. Command2

    Ctrl^c 强制终止当前命令执行 chmod {ugoa(user/group/other/all)}{+-=(wrx权限增删改)} 文件或目录 权限 对文件 对目录 r 可查看文件内容 可以列出目 ...

  2. Docker PHP 扩展配置

    # PHP 容器配置 # 从官方基础版本构建 FROM php:7.2-fpm # 官方版本默认安装扩展: # Core, ctype, curl # date, dom # fileinfo, fi ...

  3. 【Java基础】反射

    反射 反射的概述 反射(Reflection)是被视为动态语言的关键,反射机制允许程序在执行期借助 Reflection API 取得任何类的内部信息,并能直接操作任意对象的内部属性和方法. 加载完类 ...

  4. LeetCode1337矩阵中最弱的K行

    题目 给你一个大小为 m * n 的矩阵 mat,矩阵由若干军人和平民组成,分别用 1 和 0 表示. 请你返回矩阵中战斗力最弱的 k 行的索引,按从最弱到最强排序. 如果第 i 行的军人数量少于第 ...

  5. Ubuntu_Gedit配置

    Ubuntu_Gedit配置 为了换Ubuntu的时候能够更加方便,不用再用手重新打一遍代码,丢几个Gedit配置-- External Tools gdb compile (F2) #!/bin/s ...

  6. C语言指针-从底层原理到花式技巧,用图文和代码帮你讲解透彻

    这是道哥的第014篇原创 目录 一.前言 二.变量与指针的本质 1. 内存地址 2. 32位与64位系统 3. 变量 4. 指针变量 5. 操作指针变量 5.1 指针变量自身的值 5.2 获取指针变量 ...

  7. Nacos(二)源码分析Nacos服务端注册示例流程

    上回我们讲解了客户端配置好nacos后,是如何进行注册到服务器的,那我们今天来讲解一下服务器端接收到注册实例请求后会做怎么样的处理. 首先还是把博主画的源码分析图例发一下,让大家对整个流程有一个大概的 ...

  8. linux查看文件夹和磁盘内存及服务器对应的ip

    多进程统计cpu 数目 n_cpu = multiprocessing.cpu_count() print(n_cpu) 查看文件夹占用磁盘空间 du -h --max-depth=1 /path 查 ...

  9. oracle rac切换到单实例DG后OGG的处理

    在RAC切换到单实例DG后,将OGG目录复制过去,在使用alter extract ext_name,begin now的时候报错 2016-04-10 11:27:03 WARNING OGG-01 ...

  10. Python安装教程之anaconda篇

    [导读]我们知道,Python的功能非常强大.那么对于迫切想学习Python的新手同学来说,第一件事情可能需要了解python是什么?能用来做什么?语法结构是怎样的?这些我们几句话很难介绍清楚,后续会 ...