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 ...
随机推荐
- 【Flutter】容器类组件之剪裁
前言 Flutter中提供了一些剪裁函数,用于对组件进行剪裁. 剪裁Widget 作用 ClipOval 子组件为正方形时剪裁为内贴圆形,为矩形时,剪裁为内贴椭圆 ClipRRect 将子组件剪裁为圆 ...
- 区间合并 C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; ty ...
- 修改hosts文件后不生效,该怎么办
对于web开发来说,经常需要修改hosts文件,用来将域名与ip对应匹配.但是有时候发现hosts文件明明已经改了,但就是不生效,页面还会跳到某个丧心病狂的私人小站.hosts文件不生效有很多种原因, ...
- o_direct刷新方式和文件系统支持Direct i/o
若让innodb使用o_direct刷新方式,文件系统支持Direct i/o 是非常重要的.为啥
- QPainter 绘制图像接口
阅读本文大概需要 3 分钟 我们在开发软件的过程中,绘制图像功能必不可少,使用 Qt 绘制图像时非常简单,只需要传递几个参数就可以实现功能,在 Qt 中绘制图像的 api有好几个 void drawI ...
- Docker安装配置及华为云镜像加速
Docker华为云镜像加速 软件介绍 支持的操作系统 docker安装 docker镜像加速(华为云实现) 1.登录华为云网站,注册华为云账户 2.登录华为云账户,点击网页右上角的控制台 3.点击左上 ...
- SpringCloud zuul 网关限流分析
最近项目中 spring cloud zuul 运用到限流功能,打算配置一下就直接使用,不过在压测与调优过程中遇到一些没有预测到的问题,附上排查与解析结果 yml.pom配置 强烈推荐,按最新gith ...
- JavaScript中函数的this指向!
JavaScript的this的指向问题! 这是我自己敲的, 报错! <button>点击查看绑定事件的this指向!</button> <script> // 函 ...
- Crunch
Crunch 目录 1. 简介 2. 命令格式 3. options可选参数 3.1 -b number[type] 3.2 -c number 3.3 -d numbersymbol 3.4 -e ...
- LVS负载均衡之DR模式原理介绍
LVS基本原理 流程解释: 当用户向负载均衡调度器(Director Server)发起请求,调度器将请求发往至内核空间 PREROUTING 链首先会接收到用户请求,判断目标 IP 确定是本机 IP ...