zoukankan      html  css  js  c++  java
  • Good Bye 2020 B

    Good Bye 2020 B

    大意

    给你 (N) 个正整数,你可以将一个数加一(一个数仅能进行一次)或不变。

    问最多可以得到几个互不相同的数。

    思路

    现将原数组排序,从大到小考虑。

    对于最大的数,肯定贪心将其加一。

    考虑次大数,如果它和(加一后)最大的数相差大于一,那么肯定贪心的将其加一,累加答案。

    如果它的最大的数相差等于一,那么肯定不会修改,累加答案。

    考虑第三大的数,如果和次大的数(可能进行了操作),相差大于一,修改。

    如果相差等于一,不修改,累加答案。

    如果和次大的数相等,不修改,不累加答案。

    代码

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <cstring>
    using namespace std;
    
    #define ll long long
    #define ull unsigned long long
    #define cint const int&
    #define Pi acos(-1)
    
    const int mod = 998244353;
    const int inf_int = 0x7fffffff;
    const ll inf_ll = 0x7fffffffffffffff;
    const double ept = 1e-9;
    
    int t, n;
    int a[100100];
    
    int main() {
        ios::sync_with_stdio(false);
        cin >> t;
        while(t--) {
            int ans=0;
            cin >> n;
            for(int i=1; i<=n; i++) cin >> a[i];
            a[n+1] = 0;
            sort(a+1, a+1+n);
            for(int i=n; i; i--) if(a[i+1] != a[i]){
                if(a[i+1] != a[i]+1) ++a[i];
                ++ans;
            }
            cout << ans << endl;
        }
        return 0;
    }
    /*
    2
    7
    1 2 3 4 5 5 5
    7
    1 2 3 4 5 5 5
    */
    

    9min,-1

  • 相关阅读:
    centos 安装docker-ce
    quartz.net .netcore 定时任务部署到linux
    c# 获取linux 磁盘信息
    nginx 安装
    async await 理解
    Remote side unexpectedly closed network connection
    centos 安装。net
    yum 操作
    centos7下安装mysql5.7
    git 本地仓库版本无法从远程更新到本地
  • 原文地址:https://www.cnblogs.com/ullio/p/14215926.html
Copyright © 2011-2022 走看看