Codeforces Round #579 (Div. 3) E. Boxers (贪心)
题意:给你一组数,每个数都可以进行一次加一减一,问最后最多能有多少不同的数.
题解:我们可以用桶存每个数的次数,然后枚举\([1,150001]\)来求对答案的贡献,然后贪心,这里我们不用担心其他乱七八糟的东西,直接根据桶中的个数来求贡献即可,但是要先选\(i-1\)的情况,因为后面的数取不到\(i-1\),假如我们不选\(i-1\),而是选了\(i\)和\(i+1\),后面的数可能会冲突不能选,而\(i\)和\(i+1\)就没必要考虑选择顺序了,因为无论他们和后面的数冲不冲突,对答案的贡献都是一样的.
代码:
int n;
int tot[N];
bool st[N];
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n;
rep(i,1,n){
int x;
cin>>x;
tot[x]++;
}
int res=0;
rep(i,1,150010){
if(tot[i]==0) continue;
if(i==1){
if(tot[1]>=2){
st[1]=true;
st[2]=true;
res+=2;
}
else if(tot[1]==1){
st[1]=true;
res++;
}
}
else{
if(tot[i]==1){
if(!st[i-1]) {res++;st[i-1]=true;}
else if(!st[i]) {res++;st[i]=true;}
else if(!st[i+1]) {res++;st[i+1]=true;}
}
else if(tot[i]==2){
int cnt=0;
if(!st[i]) {res++;st[i]=true;cnt++;}
if(!st[i-1]) {res++;st[i-1]=true;cnt++;}
if(cnt==2) continue;
if(!st[i+1]) {res++;st[i+1]=true;}
}
else{
if(!st[i]) {res++;st[i]=true;}
if(!st[i-1]) {res++;st[i-1]=true;}
if(!st[i+1]) {res++;st[i+1]=true;}
}
}
}
cout<<res<<'\n';
return 0;
}
Codeforces Round #579 (Div. 3) E. Boxers (贪心)的更多相关文章
- Codeforces Round #579 (Div. 3)
Codeforces Round #579 (Div. 3) 传送门 A. Circle of Students 这题我是直接把正序.逆序的两种放在数组里面直接判断. Code #include &l ...
- Codeforces Round #579 (Div. 3) Complete the Projects(贪心、DP)
http://codeforces.com/contest/1203/problem/F1 Examples input 1 - - output 1 YES input 2 - - output 2 ...
- Codeforces Round #579 (Div. 3) D2. Remove the Substring (hard version) (思维,贪心)
题意:给你一个模式串\(t\),现在要在主串\(s\)中删除多个子串,使得得到的\(s\)的子序列依然包含\(t\),问能删除的最长子串长度. 题解:首先,我们不难想到,我们可以选择\(s\)头部到最 ...
- Codeforces Round #202 (Div. 1) A. Mafia 贪心
A. Mafia Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/348/problem/A D ...
- Codeforces Round #382 (Div. 2)B. Urbanization 贪心
B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...
- Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp
题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...
- Codeforces Round #180 (Div. 2) B. Sail 贪心
B. Sail 题目连接: http://www.codeforces.com/contest/298/problem/B Description The polar bears are going ...
- Codeforces Round #192 (Div. 1) A. Purification 贪心
A. Purification Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/329/probl ...
- Codeforces Round #274 (Div. 1) A. Exams 贪心
A. Exams Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/A Des ...
随机推荐
- python基础学习总结
python管理cisco设备:http://www.linuxeye.com/program/1680.html 学习:https://www.liaoxuefeng.com/wiki/001431 ...
- 剑指Offer-连续子数组中的最大和
题目 输入一个整型数组,数组里有正数也有负数.数组中的一个或连续多个整数组成一个子数组.求所有子数组的和的最大值.要求时间复杂度为 O(n). 输入 [1,-2,3,10,-4,7,2,-5] 返回值 ...
- 面试官:Netty的线程模型可不只是主从多Reactor这么简单
笔者看来Netty的内核主要包括如下图三个部分: 其各个核心模块主要的职责如下: 内存管理 主要提高高效的内存管理,包含内存分配,内存回收. 网通通道 复制网络通信,例如实现对NIO.OIO等底层JA ...
- 【Linux】用yum来下载rpm,而不安装
方法一:yum yum命令本身就可以用来下载一个RPM包,标准的yum命令提供了--downloadonly(只下载)的选项来达到这个目的. $ sudo yum install --download ...
- Android之Xposed
基础书籍推荐:1.疯狂JAVA讲义:2.疯狂安卓讲义: 逆向分析必须知道他的原理,不然只会用工具,那就直接GG. 谷歌的镜像网站:https://developers.google.com/andro ...
- Docker数据目录迁移解决方案
场景 在docker的使用中随着下载镜像越来越多,构建镜像.运行容器越来越多, 数据目录必然会逐渐增大:当所有docker镜像.容器对磁盘的使用达到上限时,就需要对数据目录进行迁移. 如何避免: 1. ...
- Connection Pool
MySQL :: MySQL Connector/NET Developer Guide :: 4.3 Managing a Connection Pool in Connector/NET http ...
- TCP/IP网络中的显式拥塞通告(ECN)
当前的TCP 实现将TCP 端节点之间的中间网络视为一个不透明的"黑盒".TCP 包进入和流出这个盒子.有些时候进入盒子的包被丢失了.因为今天的数字和光媒体上出现比特级错误的机会非 ...
- WPF combobox设置默认选项不生效的问题
combobox 是常用的控件,当我们需要绑定设置默认选项时,往往会绑定 SelectedItem 属性去设置, 可是你会惊奇地发现SelectedItem的值绑定了, 它依旧是熟悉的模样 根据官方的 ...
- 云原生项目实践DevOps(GitOps)+K8S+BPF+SRE,从0到1使用Golang开发生产级麻将游戏服务器—第1篇
项目初探 项目地址: 原项目:https://github.com/lonng/nanoserver 调过的:https://github.com/Kirk-Wang/nanoserver 这将是一个 ...