Codeforces Round 958 (Div. 2)
题目链接:Codeforces Round 958 (Div. 2)
总结:C因为常数没转\(long long\) \(wa\)两发,难绷。
A. Split the Multiset
fag:模拟
Description:给定一个\(n\)和一个\(k\),每次可以将\(n\)减掉一个数\(u\),然后插入\(x\)个数,\(x <= k\),并且插入的数之和等于\(u\)。求将其转化为全\(1\)的最小操作次数。
Solution:因为最后要得到全\(1\),所以每次操作我们插入尽可能多的\(1\),即插入\(1, 1, ..., u - (k - 1)\)。相当于每次将\(n\)减少\(k - 1\)。那么答案为\(k --, (n - 1 + k - 1) / k\),这里是向上取整。
void solve(){
cin >> n >> k;
k --;
if (n == 1){
cout << 0 << endl;
return;
}
n --;
cout << (n + k - 1) / k << endl;
}
B. Make Majority
fag: 思维
Description: 给定一个只含\(0\)或\(1\)的字符串,每次操作可以选择一个区间\(l, r\),将这个区间变为一个数(区间内出现次数最多的数)。
问能否将其变为\(1\)。
Solution:手摸几组样例发现:我们可以把所有连续的多个\(0\)变为一个\(0\),如果我们需要将一个\(0\)去掉,那么至少需要两个\(1\)。
- 那么统计连续\(0\)的区间个数\(x\),\(1\)的个数\(y\)。
- 当\(y >= x + 1\)时,有解,否则无解。
void solve(){
cin >> n;
cin >> s;
int x = 0, y = 0;
for (int i = 0; i < n; i ++){
if (s[i] == '0')
x ++;
else
y ++;
}
int xx = 0;
bool flag = true;
for (int i = 0; i < n; i ++){
if (s[i] == '0' && flag){
xx ++;
flag = false;
continue;
}
if (s[i] == '1')
flag = true;
}
if (y >= xx + 1){
cout << "Yes\n";
}
else{
cout << "No\n";
}
}
C. Increasing Sequence with Fixed OR
fag:位运算
Description:给定一个\(n\),求一个最长的序列,序列满足\(ai < a_{i +1}\),且\(a_i | a_{i + 1} == 1\)。输出序列的长度和序列值。
Solution:显然我们需要将\(n\)转化为二进制。模拟样例后发现,如果\(n\)的二进制含有\(x\)个\(1\),那么序列长度为\(x + 1\)。
然后考虑如何构造。我们只需要依次去掉低位\(1\),就可以满足以上要求。注意对常数进行位运算时,需要将其强制转化为long long
void solve(){
cin >> n;
vector<int> ans;
ans.eb(n);
for (int i = 0; i < 64; i ++){
if (n >> i & 1){ // 这一位是1
ans.eb(n ^ (1LL << i)); // 注意这里
}
}
if (ans[ans.size() - 1] == 0) // 注意特判
ans.pop_back();
cout << ans.size() << endl;
sort(ans.begin(), ans.end());
for (int i = 0; i < ans.size(); i ++){
cout << ans[i] << " ";
}
cout << endl;
}
D. The Omnipotent Monster Killer
fag:树形DP
Description:一颗有\(n\)个点的数,每个点有一个权值\(a_i\),每回合所有点会对玩家造成大小为自身权值的伤害。玩家每回合可以消灭一些点(不能删除被一条边相连的两点)。求玩家受到的最小伤害。
1 <= n <= 3e5
1 <= ai <= 1e12
Solution:只需要最多\(L\)个回合即可,\(L <= log_{2}n + 1\),我们令\(L = 20\)即可,不太会证。
如果一个节点在第\(i\)个回合删除,那么它的伤害为\(i * a_{i}\)
令\(f[x][j]\)表示,\(x\)节点在第\(j\)个回合删除,以\(x\)为根的子树的伤害最小值。\(f[x][j] = j * a_x + \sum_{i \in son(x)}min(f[i][k]), k != j\)
答案\(ans\): \(min(f[0][j], 1 <= j <= 20)\)
void solve(){
cin >> n;
vector<int> a(n);
vector g(n, vector<int>());
vector f(n, vector<int>(21, 0));
for (int i = 0; i < n; i ++)
cin >> a[i];
for (int i = 0; i < n - 1; i ++){
int a, b;
cin >> a >> b;
a --, b --;
g[a].eb(b), g[b].eb(a);
}
auto dfs = [&](auto self, int u, int v) -> void {
for (int i = 1; i <= 20; i ++){
f[u][i] = i * a[u];
}
for (auto x : g[u]){
if (x == v)
continue;
self(self, x, u);
for (int i = 1; i <= 20; i ++){
int mi = 1e18;
for (int j = 1; j <= 20; j ++){
if (i == j)
continue;
mi = min(mi, f[x][j]);
}
f[u][i] += mi;
}
}
};
dfs(dfs, 0, -1);
int ans = 1e18;
for (int i = 1; i <= 20; i ++){
ans = min(ans, f[0][i]);
}
cout << ans << endl;
}
Codeforces Round 958 (Div. 2)的更多相关文章
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
- Codeforces Round #262 (Div. 2) 1004
Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...
- Codeforces Round #268 (Div. 2) ABCD
CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
随机推荐
- 低功耗4G模组:Air780EP开发板RC522实例
本文讲解合宙Air780EP开发板RC522实例,文末[阅读原文]获取最新资料. 本文档适用于Air780EP开发板 关联文档和使用工具 LuatOS-Soc固件获取 https://gitee. ...
- The 2024 ICPC Asia East Continent Online Contest (I) G
Link: The Median of the Median of the Median 考虑二分答案,对中位数进行二分,每次去判断是否比中位数大即可. 我们钦定了一个中位数 \(x\),对于 \(\ ...
- 基本数据结构-双端队列(Deque)
6.基本数据结构-双端队列(Deque) 一.双端队列(Deque) - 概念:deque(也称为双端队列)是与队列类似的项的有序集合.它有两个端部,首部和尾部,并且项在集合中保持不变. - 特性:d ...
- 前端实战之使用canvas合并图片
最近做一个完整的系统,前端中涉及到一个推广图片的生成,其中推广图片是由一个变化的链接生成的二维码与一个固定图片拼接而成 实现demo: qrcode.png:https://images.cnblog ...
- 文件监控工具之fileboy
github:dengsgo/fileboy: fileboy,文件变更监听通知工具,使用 Go 编写.Fileboy, File Change Monitoring Notification Too ...
- Go Vue3 CMS管理后台(前后端分离模式)
本后台使用前后端分离模式开发,前端UI为Vue3+Ant Design Vue,后端Api为Go+Gin,解耦前后端逻辑,使开发更专注 技术栈 前端:Vue3,Ant Design Vue,Axios ...
- JAVA中ScheduledExecutorService的使用方法
ScheduledExecutorService 简介 ScheduledExecutorService是 Java 中的一个接口,它是ExecutorService的子接口.它主要用于在给定的延迟之 ...
- 前端好用API之MutationObserver
前情 一直以来都没有好的方式可以监听元素变化,Mutation events虽然可以监听DOM树结构变化,但是因性能问题和差的兼容问题(Webkit内核不支持)并不推荐使用. MutationObse ...
- webpack-dev-server配置https
前情 最近在做一个浏览器通知的交互需求,但是查阅官方文挡,浏览器通知需要在https环境下才能工作,于是就研究怎么在开发环境下配置一个https服务器 STEP1 安装Chocolatey Choco ...
- OS之《死锁》
什么是死锁 一组进程中的每一个进程都在等待仅由该组进程中其他进程才能引发的事件,这样就形成死锁了. 死锁的原因 竞争不可抢占的资源 竞争可消耗资源 进程推进顺序不当 死锁产生的必要条件 1.互斥条件: ...