#include<iostream> #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,a,b) for(int i=(a);i<=(b);++i) #define FORD(i,a,b) for(int i=(a);i>=(b);--i) using namespace std; const int MAXN = 500000; int T[1+MAXN], Q[1+MAXN]; int n, a, res; int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; REP(i, n+1) T[i] = Q[i] = 0; REP(i, n) { cin >> a; T[a]++; } FOR(i, 1, n) Q[T[i]]++; Q[0] = 0; // FOR(i, 0, n) cout << T[i] << " "; cout << endl; // FOR(i, 0, n) cout << Q[i] << " "; cout << endl; res = 0; int s = 1; FORD(k, n, 1) while (Q[k] > 0) { Q[k]--; res++; int p = k-1; int i = s; for(;;) { if(Q[i] == 0) { i++; s=i; if (i > n) break; } else { Q[i] -= 1; if (p >= i) { p -= i; } else { i-=p; Q[i]++; s=i; break; } } } } cout << res << endl; 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 | #include<iostream> #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,a,b) for(int i=(a);i<=(b);++i) #define FORD(i,a,b) for(int i=(a);i>=(b);--i) using namespace std; const int MAXN = 500000; int T[1+MAXN], Q[1+MAXN]; int n, a, res; int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; REP(i, n+1) T[i] = Q[i] = 0; REP(i, n) { cin >> a; T[a]++; } FOR(i, 1, n) Q[T[i]]++; Q[0] = 0; // FOR(i, 0, n) cout << T[i] << " "; cout << endl; // FOR(i, 0, n) cout << Q[i] << " "; cout << endl; res = 0; int s = 1; FORD(k, n, 1) while (Q[k] > 0) { Q[k]--; res++; int p = k-1; int i = s; for(;;) { if(Q[i] == 0) { i++; s=i; if (i > n) break; } else { Q[i] -= 1; if (p >= i) { p -= i; } else { i-=p; Q[i]++; s=i; break; } } } } cout << res << endl; return 0; } |