#include <bits/stdc++.h>
using namespace std;
struct repr {
int a;
int cnt;
repr(int x) : a(x), cnt(0) {}
};
bool operator<(const repr& x, const repr& y) {
return (x.cnt == y.cnt) ? (x.a < y.a) : (x.cnt < y.cnt);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> a;
a.reserve(n + 1);
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
a.push_back(x);
}
sort(a.begin(), a.end());
vector<repr> r;
repr c = repr(a[0]);
for (int i = 0; i < a.size(); ++i) {
if (c.a != a[i]) {
r.push_back(c);
c = repr(a[i]);
}
c.cnt++;
}
if (c.cnt > 0)
r.push_back(c);
sort(r.begin(), r.end());
int p = 0, q = r.size() - 1, res = 0;
while (p < q) {
while (p < q && r[q].cnt > 1) {
int d = min(r[q].cnt - 1, r[p].cnt);
r[p].cnt -= d;
r[q].cnt -= d;
if (r[p].cnt == 0)
p++;
}
q--;
res++;
}
if (p == q)
res++;
cout << res << endl;
}
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 | #include <bits/stdc++.h> using namespace std; struct repr { int a; int cnt; repr(int x) : a(x), cnt(0) {} }; bool operator<(const repr& x, const repr& y) { return (x.cnt == y.cnt) ? (x.a < y.a) : (x.cnt < y.cnt); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int> a; a.reserve(n + 1); for (int i = 0; i < n; ++i) { int x; cin >> x; a.push_back(x); } sort(a.begin(), a.end()); vector<repr> r; repr c = repr(a[0]); for (int i = 0; i < a.size(); ++i) { if (c.a != a[i]) { r.push_back(c); c = repr(a[i]); } c.cnt++; } if (c.cnt > 0) r.push_back(c); sort(r.begin(), r.end()); int p = 0, q = r.size() - 1, res = 0; while (p < q) { while (p < q && r[q].cnt > 1) { int d = min(r[q].cnt - 1, r[p].cnt); r[p].cnt -= d; r[q].cnt -= d; if (r[p].cnt == 0) p++; } q--; res++; } if (p == q) res++; cout << res << endl; } |
English