#include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> v(n + 1); for (int i = 0, e; i < n; i++) { cin >> e; v[e]++; } sort(v.rbegin(), v.rend()); int a = 0, b = n; while (v[b] == 0) { b--; } int res = 0; while (true) { res++; int lider_cnt = v[a]; v[a] = 0; a++; int other_cnt = 0; while (other_cnt < lider_cnt) { if (v[b] == 0) { cout << res << endl; return 0; } if (other_cnt + 1 == lider_cnt) { break; } if (v[b] + other_cnt >= lider_cnt) { v[b] -= lider_cnt - other_cnt - 1; break; } else { other_cnt += v[b]; v[b] = 0; b--; } } } 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 44 45 46 47 48 49 50 | #include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> v(n + 1); for (int i = 0, e; i < n; i++) { cin >> e; v[e]++; } sort(v.rbegin(), v.rend()); int a = 0, b = n; while (v[b] == 0) { b--; } int res = 0; while (true) { res++; int lider_cnt = v[a]; v[a] = 0; a++; int other_cnt = 0; while (other_cnt < lider_cnt) { if (v[b] == 0) { cout << res << endl; return 0; } if (other_cnt + 1 == lider_cnt) { break; } if (v[b] + other_cnt >= lider_cnt) { v[b] -= lider_cnt - other_cnt - 1; break; } else { other_cnt += v[b]; v[b] = 0; b--; } } } return 0; } |