当前位置:网站首页>C. Virus(贪心)
C. Virus(贪心)
2022-08-06 09:18:00 【罗gkv】
题意
有n只牛,它们围成一个环。其中第1只牛和第n只牛相邻。
初始有若干只(m)感染的牛。
对于每一秒,会发生以下事情。
- 选择未感染的一只牛,将它保护起来,使得他终生不感染。
- 对于没有被保护的、未感染的牛,如果它至少有一只相邻的牛被感染,那么它也被感染。
对于每一秒,应保护哪些牛,使得最终被感染的牛,数量最少。输出最终被感染的牛的数量。
数据范围
1 < = n < = 1 0 9 , 1 < = m < = 1 0 5 , n 1<=n<=10^9,1<=m<=10^5,n 1<=n<=109,1<=m<=105,n
思路
贪心,计算最终可以保护的牛的数量。
我们把所有初始时,所有的非感染牛区间,都提取出来,并按照从大到小顺序排列。
并按大区间优先处理的原则,尽量去阻止感染牛的蔓延。
详见代码
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 200010;
int n, m;
int a[maxn], b[maxn];
int cal() {
int res = 0;// 最多可以保护的牛的数量
for (int i = 0; i < m; ++i) {
b[i] = a[i+1] - a[i] - 1;
}
sort(b, b + m);// 排序
int cur = 0;// 时间戳
for (int i = m - 1; i >= 0; --i) {
if (b[i] <= 2 * cur) break;// 如果所有牛都被感染了,停止处理
if (b[i] == 2 * cur + 1) {
// 只有一只牛没感染
res += 1;
++cur;
} else {
// 可以放两只牛的场景
res += b[i] - (2 * cur + 1);
cur += 2;
}
}
return n - res;
}
void solve() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; ++i) {
scanf("%d", &a[i]);
}
sort(a, a + m);
a[m] = a[0] + n;// 添加个a[m],方便处理a[m-1]和a[0]
int res = cal();
printf("%d\n", res);
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
solve();
}
}
/* 20 3 3 7 12 0 0 1 0 0 1 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 */
最后
weixin gongzhonghao搜 对方正在debug,关注下,一起快乐刷题吧~
边栏推荐
猜你喜欢

【Untitled】

虹科分享|如何保障医疗数据安全?移动目标防御技术给你满意的答案

View the Linux log on the web side, and view the Linux log on the web side

自动化测试定位不到元素?可能是 frame 在搞鬼

Combination of Leetcode77.

RL reinforcement learning summary (2)

根据轮廓创建旋转框和椭圆

第十八天(链路聚合的配置、VRRP的工作过程、IPV6的配置)

Day 16 (Configuration BPDU, TCN BPDU)

回头再看ResNet——深度学习史上的关键一步
随机推荐
【Log】镜像源配置以及最新可用镜像源
【ELT.ZIP】OpenHarmony啃论文俱乐部——学术科研方法论沉淀辑
From "prairie cattle" to "digital cattle": Mengniu's digital transformation!
【机器学习】贝叶斯分类器
项目运维工作的心得总结
How is the LinkedList added?
网页版 Xshell支持FTP连接和SFTP连接 【详细教程】接上篇文章
(5) BuyFigrines Hd 2022 school training
Expansion mechanism of ArrayList
Xshell下载 破解,史上最简单易懂教程
第十七天(续第十六天BPDU相关知识以及STP的配置)
Card hovering frosted glass effect
第十八天(链路聚合的配置、VRRP的工作过程、IPV6的配置)
VS同一解决方案的不同项目的命名空间名字唯一
【TensorFlow&PyTorch】loss损失计算
Two important self-learning functions in pytorch dir(); help()
Remember to deduplicate es6 Set to implement common menus
Web端查看Liunx日志,网页端查看Liunx日志
ROS报错[rospack] Error: package ‘.....‘ not found
Page Loading Animation_Gradient Color Rotating Small Circle