比赛链接: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. 记一次多事件绑定中自己给自己设置的坑——click,dblclick,mousedown,mousemove,mouseup

    目录 项目综述 需求 问题 猜想 解决 反思 项目综述 在页面中模拟某操作系统的操作界面,提供应用窗口的最大化.最小化.还原等功能 需求 对一个应用窗口标题栏双击使其铺满整个视口,再次双击还原到原来大 ...

  2. PHP 插件资源

    PHP   jsonRPC  百度云网盘地址  https://pan.baidu.com/s/1itCIhrdd5bPGJMefNUuKvw   提取码 : ax4d PHP Excel 百度云网盘 ...

  3. 【Java基础】数组和算法

    数组和算法 查找算法 线性查找 ... 二分查找 二分查找要求数据结构是有序的. package com.parzulpan.java.ch03; /** * @Author : parzulpan ...

  4. Docker学习笔记之查看Docker

    命令: 使用history命令查看镜像历史 使用cp命令复制容器中的文件到主机 使用commit命令把修改过的容器创建为镜像 使用diff命令检查容器文件的修改 使用inspect命令查看容器/镜像详 ...

  5. Hbase snapshot数据迁移

    # 在源集群中创建快照(linux shell) hbase snapshot -t <table_name> -n <snapshot_name> 或(hbase shell ...

  6. 【数据库】MySQL & SQL 介绍

    文章目录 MySQL & SQL 介绍 1.MySQL的背景 2.MySQL的优点 3.MySQL的安装 4.MySQL服务的启动和停止 方式一 方式二 5.MySQL服务的登录和退出 方式一 ...

  7. ORACLE查找占用临时表空间多的SESSION

    需要使用SYS用户登录查看 /* Formatted on 2020/12/30 上午 11:17:12 (QP5 v5.163.1008.3004) */ SELECT k.inst_id &quo ...

  8. 糊糊的学习笔记--Fiddle抓包

    Fiddle简述 Fiddler是一个http调试代理,它能 够记录所有的你电脑和互联网之间的http通讯,Fiddler 可以也可以让你检查所有的http通讯,设置断点,以及Fiddle 所有的&q ...

  9. uni-app请求uni.request封装使用

    对uni.request的一些共同参数进行简单的封装,减少重复性数据请求代码.方便全局调用. 先在目录下创建 utils 和 common 这2个文件夹 utils 是存放工具类的,common 用来 ...

  10. windows命令行关闭IE代理

    打开:reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnab ...