Codeforces Global Round 23 A-D
A
题解
知识点:贪心,构造。
注意到有 \(1\) 就一定能构造。
时间复杂度 \(O(n)\)
空间复杂度 \(O(1)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
bool solve() {
int n, k;
cin >> n >> k;
bool ok = 0;
for (int i = 1;i <= n;i++) {
int x;
cin >> x;
ok |= x;
}
if (ok) cout << "YES" << '\n';
else cout << "NO" << '\n';
return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}
B
题解
知识点:枚举,双指针。
用对撞指针,枚举左侧 \(1\) 和 右侧 \(0\) ,一次操作能消除一对。
时间复杂度 \(O(n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int a[100007];
bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i];
int l = 1, r = n;
int cnt = 0;
while (l <= r) {
while (l <= r && a[l] == 0)l++;
while (l <= r && a[r] == 1)r--;
if (l <= r) {
l++;
r--;
cnt++;
}
}
cout << cnt << '\n';
return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}
C
题解
知识点:枚举。
容易发现,我们可以通过操作将序列变成非减序列,只要我们从左到右操作每组 \(a_i<a_{i-1}\) 的 \(a_i\) ,使 \(a_i \geq a_{i-1}\) 。这样的相邻数对之差大于 \(i\) 的不会超过 \(n-i\) 组,即第 \(i\) 次操作修改的一定小于等于 \(i\) ,因此我们一定可以通过 \(n\) 次操作修改所有这样的数对。
把所有相邻两数的差带着下标从小到大排序输出下标就行。
时间复杂度 \(O(n \log n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int a[100007];
bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i];
vector<pair<int, int>> v;
v.push_back({ 0,1 });
for (int i = 2;i <= n;i++) {
v.push_back({ a[i - 1] - a[i], i });
}
sort(v.begin(), v.end());
for (auto [i, j] : v) cout << j << ' ';
cout << '\n';
return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}
D
题解
知识点:树形dp,贪心。
此题重点在于如何分配路径到子节点。
显然,为了保证子节点路径数至多相差 \(1\) ,若父节点有 \(p\) 或 \(p+1\) 条路径,那么 \(s\) 个子节点可能的路径数只有 \(\lfloor \frac{p}{s} \rfloor\) 或 \(\lfloor \frac{p}{s} \rfloor + 1\) 。
- \(s>2\) 和 \(s=1\) 时,显然成立。
- \(s = 2\) 时, \(p\) 能被整除时显然成立。
- \(s = 2\) 时, \(p\) 不能被整除时 \(p+1\) 一定能被整除,但只有 \(\lfloor \frac{p}{s} \rfloor + 1\) 一种合法情况,\(p\) 有 \(\lfloor \frac{p}{s} \rfloor\) 或 \(\lfloor \frac{p}{s} \rfloor + 1\) 两种,同样成立。
我们知道了子节点可能分配到路径后,对分配方法进行dp就行。
设 \(f[u][0/1]\) ,表示对于节点 \(u\) 的子树, \(u\) 具有路径数为 \(p\) 或 \(p+1\) 时,子树的总贡献。对于 \(f[u][0/1]\) ,先加上 \(u\) 本身的贡献,以及子节点 \(v\) 路径数为 \(\lfloor \frac{p}{s} \rfloor\) 的一种贡献,即 \(f[v][0]\) ,这是子节点都能分配到的。
然后,对于 \(f[u][0]\) ,可以给 \(p \mod s\) 个子节点多分配一条路径;对于 \(f[u][1]\) 可以给 \((p+1) \mod s\) 个子节点多分配一条路径。这些子节点的贡献可以加一个增量 \(f[v][1]-f[v][0]\) ,我们按照这个增量排序,就能找到增量最大的几个子节点,我们给它们分配即可。
最后输出 \(f[1][0]\) ,根节点没有多一条路径的选择。
时间复杂度 \(O(n \log n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int> g[200007];
int s[200007];
ll f[200007][2];
void dfs(int u, int p) {
f[u][0] = 1LL * p * s[u];
f[u][1] = f[u][0] + s[u];
if (!g[u].size()) return;
vector<ll> tb;
for (auto v : g[u]) {
dfs(v, p / g[u].size());
f[u][0] += f[v][0];
f[u][1] += f[v][0];
tb.push_back(f[v][1] - f[v][0]);
}
sort(tb.begin(), tb.end(), [&](ll a, ll b) {return a > b;});
int r = p % g[u].size();
for (int i = 0;i < r;i++) f[u][0] += tb[i];
for (int i = 0;i <= r;i++) f[u][1] += tb[i];
}
bool solve() {
int n, k;
cin >> n >> k;
for (int i = 1;i <= n;i++) g[i].clear();
for (int i = 2;i <= n;i++) {
int p;
cin >> p;
g[p].push_back(i);
}
for (int i = 1;i <= n;i++) cin >> s[i];
dfs(1, k);
cout << f[1][0] << '\n';
return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}
Codeforces Global Round 23 A-D的更多相关文章
- Codeforces Global Round 23 D.Paths on the Tree(记忆化搜索)
https://codeforces.ml/contest/1746/problem/D 题目大意:一棵n节点有根树,根节点为1,分别有两个数组 s[i] 顶点 i 的魅力值 c[i] 覆盖顶点 i ...
- CodeForces Global Round 1
CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...
- Codeforces Global Round 1 - D. Jongmah(动态规划)
Problem Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...
- Codeforces Global Round 2 题解
Codeforces Global Round 2 题目链接:https://codeforces.com/contest/1119 A. Ilya and a Colorful Walk 题意: 给 ...
- Codeforces Global Round 1 (A-E题解)
Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...
- Codeforces Global Round 3
Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...
- Codeforces Global Round 1 (CF1110) (未完结,只有 A-F)
Codeforces Global Round 1 (CF1110) 继续补题.因为看见同学打了这场,而且涨分还不错,所以觉得这套题目可能会比较有意思. 因为下午要开学了,所以恐怕暂时不能把这套题目补 ...
- 【手抖康复训练1 】Codeforces Global Round 6
[手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思 ...
- Codeforces Global Round 11 个人题解(B题)
Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...
随机推荐
- Java SE 10 Application Class-Data Sharing 示例
Java SE 10 Application Class-Data Sharing 示例 作者:Grey 原文地址:Java SE 10 Application Class-Data Sharing ...
- iommu分析之---intel irq remap框架实现
背景介绍: IRQ域层级结构: 在某些架构上,可能有多个中断控制器参与将一个中断从设备传送到目标CPU. 让我们来看看x86平台上典型的中断传递路径吧 Device --> IOAPIC -&g ...
- Java精进-20分钟学会mybatis使用
文字分享 希望现在的你无论有明确具体的目标还是没有,都能重视自己的需求和目标,并且常常回顾,或许可以找一个你习惯的方式写出来,挂在哪里,电脑或日记本都好.当你疲惫或迷茫的时候拿出来看一下,这在情怀领域 ...
- linux 3个快捷方式
Ctrl+c组合键:当同时按下键盘上的Ctrl和字母c的时候,意味着终止当前进程的运行.假如执行了一个错误命令,或者是执行某个命令后迟迟无法结束,这时就可以冷静地按下Ctrl+c组合键,命令行终端的控 ...
- 【Java】学习路径46-两种创建多线程的方法、以及在匿名内部类创建线程
两种方法: 1.创建一个继承自Thread的线程类,然后再main(不限)中构造这个线程类对象.方法在之前讲过. 2.创建一个使用Runnable接口的线程类,然后在main(不限)中构造这个Runn ...
- 并发编程二、CPU多级缓存架构与MESI协议的诞生
前言: 文章内容:线程与进程.线程生命周期.线程中断.线程常见问题总结 本文章内容来源于笔者学习笔记,内容可能与相关书籍内容重合 偏向于知识核心总结,非零基础学习文章,可用于知识的体系建立,核心内容 ...
- 禁止mysql自动更新
每到00:00时,MySQL弹出小黑框 这是mysql在自动检测更新 右键"此电脑",点击"管理" 依此操作即可
- Linux之firewalld防火墙规则
一, 什么是防火墙规则? 允许哪些服务端口被放行,怎么放行,及哪些服务端口被阻拦,如何阻拦的一组网络安全规则.支持ipv4和ipv6,且分为直接规则和富规则两种. 二, 如何管理firewalld 1 ...
- Macos下用pycharm运行django项目死活安装不上mysqlclient怎么办!!??
花了我三天时间,佛了 我刚从win过渡到mac,想着把代码迁移一下. 然后看到依赖里面有一个mysqlclient,然后pip3 install死活装不上 解决方案: 在这里写上这个 然后就好,死了
- Pytorch: repeat, repeat_interleave, tile的用法
https://zhuanlan.zhihu.com/p/474153365 torch.repeat 使张量沿着某个维度进行复制, 并且不仅可以复制张量,也可以拓展张量的维度: import tor ...