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
#include <bits/stdc++.h>
using namespace std;

const int N = 500005;
int Count[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, x;
    cin >> n;

    for(int i = 0; i < n; i++) {
        cin >> x;
        Count[x]++;
    }
    sort(Count+1, Count + n+1);
    
    int idx1 = 1, idx2 = n;
    int cnt = 0;
    while(idx1 <= idx2) {
        int cnt2 = 0;
        
        while(idx1 < idx2 && cnt2 < Count[idx2]-1) {
            cnt2 += Count[idx1];
            Count[idx1++] = 0;
        }
        if(cnt2 >= Count[idx2]) {
            Count[--idx1] += (cnt2 - Count[idx2] + 1);
        }
        Count[idx2] = 0;
        idx2--;
        cnt++;
    }
    cout << cnt;
}