Good Bye 2020 B
阿新 • • 發佈:2020-12-31
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 */