// Author: Bartek Knapik #include <cstdio> const int MAX_N = 500000 + 9; struct Pair { int val; int cnt; bool operator<(const Pair &oth) const { return cnt > oth.cnt; } }; Pair leaders[MAX_N], tmp[MAX_N]; int n, val, n_leader; int counter[MAX_N]; void sort(int begin, int end) { if (end - begin <= 1) return; int mid = (begin + end) / 2; sort(begin, mid); sort(mid, end); int a = begin; int b = mid; int c = 0; while (a < mid && b < end) { if (leaders[a] < leaders[b]) tmp[c++] = leaders[a++]; else tmp[c++] = leaders[b++]; } while (a < mid) tmp[c++] = leaders[a++]; while (b < end) tmp[c++] = leaders[b++]; c = 0; while (begin < end) leaders[begin++] = tmp[c++]; } int solve() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &val); counter[val]++; } for (int i = 0; i < MAX_N; ++i) if (counter[i]) leaders[n_leader++] = { i, counter[i] }; sort(0, n_leader); int ans, todo, begin, end, toremove; ans = 0; todo = n; begin = 0; end = n_leader - 1; while (todo) { ans++; if (leaders[begin].cnt > todo / 2) break; todo -= 2 * leaders[begin].cnt - 1; toremove = leaders[begin].cnt - 1; while (toremove) { if (leaders[end].cnt <= toremove) { toremove -= leaders[end].cnt; leaders[end].cnt = 0; end--; } else { leaders[end].cnt -= toremove; toremove = 0; } } begin++; } return ans; } int main() { printf("%d\n", solve()); 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | // Author: Bartek Knapik #include <cstdio> const int MAX_N = 500000 + 9; struct Pair { int val; int cnt; bool operator<(const Pair &oth) const { return cnt > oth.cnt; } }; Pair leaders[MAX_N], tmp[MAX_N]; int n, val, n_leader; int counter[MAX_N]; void sort(int begin, int end) { if (end - begin <= 1) return; int mid = (begin + end) / 2; sort(begin, mid); sort(mid, end); int a = begin; int b = mid; int c = 0; while (a < mid && b < end) { if (leaders[a] < leaders[b]) tmp[c++] = leaders[a++]; else tmp[c++] = leaders[b++]; } while (a < mid) tmp[c++] = leaders[a++]; while (b < end) tmp[c++] = leaders[b++]; c = 0; while (begin < end) leaders[begin++] = tmp[c++]; } int solve() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &val); counter[val]++; } for (int i = 0; i < MAX_N; ++i) if (counter[i]) leaders[n_leader++] = { i, counter[i] }; sort(0, n_leader); int ans, todo, begin, end, toremove; ans = 0; todo = n; begin = 0; end = n_leader - 1; while (todo) { ans++; if (leaders[begin].cnt > todo / 2) break; todo -= 2 * leaders[begin].cnt - 1; toremove = leaders[begin].cnt - 1; while (toremove) { if (leaders[end].cnt <= toremove) { toremove -= leaders[end].cnt; leaders[end].cnt = 0; end--; } else { leaders[end].cnt -= toremove; toremove = 0; } } begin++; } return ans; } int main() { printf("%d\n", solve()); return 0; } |