【解题报告】Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)
来源:互联网 发布:国外微博软件 编辑:程序博客网 时间:2024/04/28 15:58
题目链接
A. Bear and Big Brother(Codeforces 791A)
思路
由于
代码
#include <bits/stdc++.h>using namespace std;typedef long long ll;ll a, b, x, y;int main() {// freopen("Input3.txt", "r", stdin); ios::sync_with_stdio(false); cin.tie(0); cin >> a >> b; x = 1; y = 1; for(int i = 1; i <= 20; i++) { x *= 3; y *= 2; if(a * x > b * y) { cout << i << endl; break; } } return 0;}
B. Bear and Friendship Condition(Codeforces 791B)
思路
这种有“关系”的题目,用图来建模最合适不过。所以问题转化为每个连通分量的子图是否构成“完全图”,也就是两点之间是否都有边相连。完全图有一个性质:其点数
代码
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 2e5;vector <int> G[maxn];bool vis[maxn];int u, v, n, m;ll V, E;void dfs(int u) { vis[u] = true; E += G[u].size(); V++; for(int v : G[u]) { if(vis[v] == true) { continue; } dfs(v); }}int main() {// freopen("Input1.txt", "r", stdin); ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; while(m--) { cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } memset(vis, 0, sizeof(vis)); for(int i = 1; i <= n; i++) { if(vis[i] == true) { continue; } V = E = 0; dfs(i); if(V * (V - 1) != E) { puts("NO"); return 0; } } puts("YES"); return 0;}
C. Bear and Different Names(Codeforces 791C)
思路
本题只需要构造一组解。搜索的话复杂度太高,可以先考虑贪心的思想。首先应当明确,输入中的一个字符串
例如
如果窗口的描述是
(注意,下面的构造方法以第一组样例为例)
- 先构造前
k−1 个元素。将其构造成升序即可。那么我们构造出序列[1,2] 。 - 然后构造第
k 个元素。检查第1 个字符串,如果其为NO 则在序列末尾添加与其窗口起始元素相同的元素,如果其为YES ,那么添加新的元素即可。在第一组样例中此时的序列变为[1,2,1] 。 - 重复第
2 个步骤,即不断往序列末尾添加新元素。序列依次被构造成 ->1,[2,1,2] ->1,2,[1,2,3] ->1,2,1,[2,3,4] ->1,2,1,2,[3,4,5] ->1,2,1,2,3,[4,5,4] - 最后序列被构造出来了,第一组样例构造出的序列是
1,2,1,2,3,4,5,4 ,将数字映射到姓名即可。比如可以将数字看成26 进制数然后用英文字幕A,B,C …表示1,2,3 …(如下)。
代码
#include <bits/stdc++.h>using namespace std;const int maxn = 100;string s[maxn];int name[maxn];int n, k, cnt;int main() {// freopen("Input1.txt", "r", stdin); ios::sync_with_stdio(false); cin.tie(0); cin >> n >> k; for(int i = k; i <= n; i++) { cin >> s[i]; } for(int i = 1; i <= k - 1; i++) { name[i] = ++cnt; } for(int i = k; i <= n; i++) { if(s[i] == "YES") { name[i] = ++cnt; } else { name[i] = name[i - k + 1]; } } for(int i = 1; i <= n; i++) { cout << (char)(name[i] % 10 + 'A'); cout << (char)(name[i] / 10 + 'a') << ' '; } return 0;}
D. Bear and Tree Jumps(Codeforces 791D)
思路
先将问题简化为
令
接着要将问题推广到
也就是说
如果能求出
那么我们需要对每条路径算模
代码
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 2e5 + 5, maxk = 10;vector <int> G[maxn];int n, k, u, v;ll ans, w[maxn], cnt[maxn][maxk];int sub(int a, int b) { return ((a - b) % k + k) % k;}// 边搜索边统计需要的数据void dfs(int u, int p, int d) { w[u] = cnt[u][d % k] = 1; for(int v : G[u]) { if(v == p) { continue; } dfs(v, u, d + 1); for(int i = 0; i < k; i++) { for(int j = 0; j < k; j++) { // r为余数,k为还要多少能模k余0 int r = sub(i + j, 2 * d); int need = sub(k, r); // 计算贡献度 ans += need * cnt[u][i] * cnt[v][j]; } } // 从子树总累加cnt[u][i] for(int i = 0; i < k; i++) { cnt[u][i] += cnt[v][i]; } w[u] += w[v]; } // 计算贡献度 ans += w[u] * (n - w[u]);}int main() {// freopen("Input2.txt", "r", stdin); ios::sync_with_stdio(false); cin.tie(0); cin >> n >> k; for(int i = 1; i <= n - 1; i++) { cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } dfs(1, -1, 0); cout << ans / k << endl; return 0;}
(其它题目略)
- 【解题报告】Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)
- 【解题报告】Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 菜鸡只会ABC!
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)
- Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) 题解(待续)
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) A -- D
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) A. Vicious Keyboard
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) B. Valued Keys
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) C. Voltage Keepsake
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) D. Volatile Kite
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)-C. Voltage Keepsake
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) ABCD
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)
- Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) C. Bear and Company(DP)
- 深夜切题——Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)
- VxWorks内核解读 -设备驱动
- 装备选择
- AndroidStudio-如何正确的打开一个存在的项目
- MFC中显示图片
- poj 1011 Sticks 深度搜索+(剪枝)
- 【解题报告】Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)
- jsp语法
- 回溯模板
- POJ1835 宇航员(模拟)
- 使用librtmp进行H264与AAC直播(神文)
- 【知识点】 --- 第一类Stirling数和第二类Stirling数
- WebView加载完毕后onFinish等回调方法不走的原因
- 蓝桥杯 翻硬币
- windows下如何为Python安装setuptools,pip