#include <bits/stdc++.h> using namespace std; #define loop(i, a, b) for(int i = a; i <= b; i++) #define loop_rev(i, a, b) for(int i = a; i >= b; i--) #define all(x) x.begin(), x.end() #define sz(x) int(x.size()) #define pb push_back int main() { cin.tie(0)->sync_with_stdio(false); int n; cin >> n; vector<int> cnt(n+1); loop(i, 1, n) { int x; cin >> x; ++cnt[x]; } multiset<int> cnts; loop(i, 1, n) { if(cnt[i]) cnts.insert(cnt[i]); } int res = 0; while(!cnts.empty()) { auto it = prev(cnts.end()); int left = (*it) - 1; cnts.erase(it); while(left > 0 && !cnts.empty()) { left -= *cnts.begin(); cnts.erase(cnts.begin()); } if(left < 0) { cnts.insert(-left); } ++res; } cout << res << '\n'; }
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 44 45 46 47 48 49 | #include <bits/stdc++.h> using namespace std; #define loop(i, a, b) for(int i = a; i <= b; i++) #define loop_rev(i, a, b) for(int i = a; i >= b; i--) #define all(x) x.begin(), x.end() #define sz(x) int(x.size()) #define pb push_back int main() { cin.tie(0)->sync_with_stdio(false); int n; cin >> n; vector<int> cnt(n+1); loop(i, 1, n) { int x; cin >> x; ++cnt[x]; } multiset<int> cnts; loop(i, 1, n) { if(cnt[i]) cnts.insert(cnt[i]); } int res = 0; while(!cnts.empty()) { auto it = prev(cnts.end()); int left = (*it) - 1; cnts.erase(it); while(left > 0 && !cnts.empty()) { left -= *cnts.begin(); cnts.erase(cnts.begin()); } if(left < 0) { cnts.insert(-left); } ++res; } cout << res << '\n'; } |