• 题意:给你一组数,每个数都可以进行一次加一减一,问最后最多能有多少不同的数.

  • 题解:我们可以用桶存每个数的次数,然后枚举\([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;
    }

最新文章

  1. Js 原型和原型链
  2. Android 天猫apk聊天数据库解密
  3. opencv中的SIFT,SURF,ORB,FAST 特征描叙算子比较
  4. 解决Window Azure: Failed to start Development Storage: the SQL Server instance ‘localhost\SQLExpress’ could not be found.
  5. SocketTcpServer
  6. iOS label根据显示内容自适应大小
  7. Collections.sort(List&lt;T&gt; Comparator) 自定义排序
  8. AOSP
  9. 李洪强iOS开发-网络新闻获取数据思路回顾
  10. hdu 1536 S-Nim_求sg值模版
  11. jQuery Validate W3C内容
  12. Linux 内核升级步骤
  13. ajax学习之post请求步骤
  14. nginx四层负载均衡配置
  15. JS 设计模式七 -- 外观模式
  16. JVM基础系列第13讲:JVM参数之追踪类信息
  17. 爬虫基础之urllib库(代码演示)
  18. redis 安装配置学习笔记
  19. windows 自动贴边
  20. 读书笔记 enum枚举之位标志属性(Flags)浅析

热门文章

  1. 来不及解释!Linux常用命令大全,先收藏再说
  2. 【.NET 与树莓派】使用 GPIO 库
  3. 【二分搜索树】1、二分查找法的实现 - Binary Search
  4. C#使用ODP.NET连接oracle数据库
  5. Java运算符及包机制
  6. Oracle 常用命令大全(持续更新)
  7. Linux文件系统之INode
  8. jdk安装简洁版
  9. MySQL时间格式转换函数
  10. LOJ10099矿场搭建