#include<bits/stdc++.h> using namespace std; #define int int64_t #define pb push_back #define pii pair<int, int> #define st first #define nd second #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() const int MAXN = 500'009; int cnt[MAXN]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for(int i=0;i<n;i++) { int x; cin >> x; cnt[x-1]++; } sort(cnt, cnt+n); int cur = 0; for(int i=n-1;i>=0;i--) { int x = cnt[i]-1; while(cur<i) { int val = min(cnt[cur], x); x -= val; cnt[cur] -= val; if(cnt[cur]) break; cur++; } if(cur==i) { cout << n-i << "\n"; return 0; } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #include<bits/stdc++.h> using namespace std; #define int int64_t #define pb push_back #define pii pair<int, int> #define st first #define nd second #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() const int MAXN = 500'009; int cnt[MAXN]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for(int i=0;i<n;i++) { int x; cin >> x; cnt[x-1]++; } sort(cnt, cnt+n); int cur = 0; for(int i=n-1;i>=0;i--) { int x = cnt[i]-1; while(cur<i) { int val = min(cnt[cur], x); x -= val; cnt[cur] -= val; if(cnt[cur]) break; cur++; } if(cur==i) { cout << n-i << "\n"; return 0; } } } |