题目链接: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)的更多相关文章

  1. 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 ...

  2. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  3. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  4. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  5. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  6. 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 ...

  7. 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 ...

  8. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

  9. Codeforces Round #268 (Div. 2) ABCD

    CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...

  10. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts

    题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...

随机推荐

  1. 由C#委托回调想到的二三事

    写在前面: 之前的过开发程中,我愈发觉得面对复杂的界面要求,最好还是用UserControl将不同模块的界面设计单独封装,以应对客户频繁地需求更改.这样做能够在面对对不同的UI要求时,动态的加载预先设 ...

  2. [OtterCTF 2018]-内存取证-WP

    [OtterCTF 2018] WP [OtterCTF 2018] What the password? 题目描述: you got a sample of rick's PC's memory. ...

  3. ubuntu 使用tree打印树形结构

    ubuntu 使用 tree 命令能打印目录结构 sudo apt-get install tree 安装后使用tree就行了 . ├── index.php ├── phpQuery │   ├── ...

  4. Elasticsearch之性能优化

    elasticsearch 使用有时候会出现CPU飙升,内存爆满情况,需要进行一些优化设置. 1.  一定要用es自带id生产策略 2. 设置副本数量为0,写入完可恢复 put  localhost: ...

  5. windows下python批量更新软件包

    前言 相信很多小伙伴都遇到过python有些软件包版本过低导致无法安装一些模块的问题,刚好我前两天也遇到了,这里写个文章记录一下 一.更新pip版本 打开命令控制面板,输入: python -m pi ...

  6. Jetpack Compose学习(14)——ConstraintLayout约束布局使用

    原文地址: Jetpack Compose学习(14)--ConstraintLayout约束布局使用-Stars-One的杂货小窝 本文阅读之前,需要了解ConstraintLayout的使用! 各 ...

  7. k8s~service和deployment中的spec.selector

    service和deployment中的spec.selector 在 Kubernetes 中,Service 和 Deployment 的 spec.selector 在使用上是有一些不同之处的, ...

  8. 服务拆分之《阿里云OCR使用指南》

    在做一件什么事情: 遇到了什么问题: 问题分析: 业界解决方案: 我的方案: 最终的结果: 服务都已经迁移过来了,对应的那些使用的工具什么的也都得换成自己的账号.起初原始用的是什么忘记了,时间太长了已 ...

  9. Visual Studio 2017 rc 资源文件 预处理 宏 无效

    在属性c++下的预处理宏不会影响rc资源文件的,需要对rc资源文件单独设置. 右键rc资源文件,点击属性,在预处理器定义添加需要的宏

  10. SQLServer单表无缝转换到MySQL

    场景:SQLServer 单表结构,无缝转换到MySQL 方法: 1. Navicat-右键需要导出的数据表-逆向表到模型 2. 弹出来的模型窗口里,选择 转换模型为  默认MySQL8.0 确认 3 ...