#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using ld = long double; using vi = vector<int>; using vll = vector<ll>; using vii = vector<pii>; const int N = 5e5 + 10; int tab[N]; int cnt[N]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &tab[i]); } sort(tab, tab + n); int m = 0, idx = 0; for (int i = 0; i < n; i = idx) { idx = i; while (idx < n && tab[idx] == tab[i]) { idx++; } cnt[m++] = idx - i; i = idx; } sort(cnt, cnt + m); int ans = 0; int r = m - 1, l = 0; while (r > l) { int curr = cnt[r]; int left = 0; while (left + cnt[l] < curr) { left += cnt[l]; cnt[l] = 0; l++; } if (l != r) { int more = min(cnt[l], curr - left - 1); left += more; cnt[l] -= more; } cnt[r] = 0; r--; ans++; } if (cnt[r] != 0) { ans++; } printf("%d\n", ans); }
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 51 52 53 54 55 56 57 58 59 60 61 | #include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using ld = long double; using vi = vector<int>; using vll = vector<ll>; using vii = vector<pii>; const int N = 5e5 + 10; int tab[N]; int cnt[N]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &tab[i]); } sort(tab, tab + n); int m = 0, idx = 0; for (int i = 0; i < n; i = idx) { idx = i; while (idx < n && tab[idx] == tab[i]) { idx++; } cnt[m++] = idx - i; i = idx; } sort(cnt, cnt + m); int ans = 0; int r = m - 1, l = 0; while (r > l) { int curr = cnt[r]; int left = 0; while (left + cnt[l] < curr) { left += cnt[l]; cnt[l] = 0; l++; } if (l != r) { int more = min(cnt[l], curr - left - 1); left += more; cnt[l] -= more; } cnt[r] = 0; r--; ans++; } if (cnt[r] != 0) { ans++; } printf("%d\n", ans); } |