#include <cstdio>
#include <functional>
using namespace std;
int n, i, x, r, a, b, s;
int t[500001];
int main() {
scanf("%d", &n);
for (i = 1; i <= n; i++) {
t[i] = 0;
}
for (i = 0; i < n; i++) {
scanf("%d", &x);
t[x] += 1;
}
x = 0;
for (int i = 1; i <= n; i++) {
if (t[i] > 0) {
t[x] = t[i];
x++;
}
}
sort(t, t + x, greater<int>());
// for (i = 0; i < x; i++) {
// printf("X: %d\n", t[i]);
// }
r = 1;
a = 1;
b = x - 1;
s = t[0] - 1;
while (a <= b) {
// printf("XXX a:%d b:%d r:%d s:%d\n", a, b, r, s);
if (s == 0) {
r++;
s = t[a] - 1;
a++;
} else if (t[b] > s) {
t[b] -= s;
s = 0;
} else {
s -= t[b];
b--;
}
}
// printf("YYY a:%d b:%d r:%d s:%d\n", a, b, r, s);
printf("%d\n", r);
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 | #include <cstdio> #include <functional> using namespace std; int n, i, x, r, a, b, s; int t[500001]; int main() { scanf("%d", &n); for (i = 1; i <= n; i++) { t[i] = 0; } for (i = 0; i < n; i++) { scanf("%d", &x); t[x] += 1; } x = 0; for (int i = 1; i <= n; i++) { if (t[i] > 0) { t[x] = t[i]; x++; } } sort(t, t + x, greater<int>()); // for (i = 0; i < x; i++) { // printf("X: %d\n", t[i]); // } r = 1; a = 1; b = x - 1; s = t[0] - 1; while (a <= b) { // printf("XXX a:%d b:%d r:%d s:%d\n", a, b, r, s); if (s == 0) { r++; s = t[a] - 1; a++; } else if (t[b] > s) { t[b] -= s; s = 0; } else { s -= t[b]; b--; } } // printf("YYY a:%d b:%d r:%d s:%d\n", a, b, r, s); printf("%d\n", r); return 0; } |
English