#include "bits/stdc++.h" using namespace std; int const N = 5e5+5; int n, amt[N], ans; vector <int> v; bool cmp(int a, int b){return amt[a] < amt[b];} int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for(int i = 1; i <= n; ++i){ int x; cin >> x; amt[x] += 1; } for(int i = 1; i <= n; ++i) if(amt[i]) v.push_back(i); sort(v.begin(), v.end(), cmp); int l = 0, r = v.size() - 1; while(l < r){ int space = amt[v[r]] - 1; while(space){ int c = min(space, amt[v[l]]); amt[v[l]] -= c; space -= c; if(amt[v[l]] == 0) l++; } r--; ans++; } cout << max(1, ans + (l==r)); }
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 | #include "bits/stdc++.h" using namespace std; int const N = 5e5+5; int n, amt[N], ans; vector <int> v; bool cmp(int a, int b){return amt[a] < amt[b];} int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for(int i = 1; i <= n; ++i){ int x; cin >> x; amt[x] += 1; } for(int i = 1; i <= n; ++i) if(amt[i]) v.push_back(i); sort(v.begin(), v.end(), cmp); int l = 0, r = v.size() - 1; while(l < r){ int space = amt[v[r]] - 1; while(space){ int c = min(space, amt[v[l]]); amt[v[l]] -= c; space -= c; if(amt[v[l]] == 0) l++; } r--; ans++; } cout << max(1, ans + (l==r)); } |