#include<bits/stdc++.h>
using namespace std;
int tab[500007];
set<int> uniq;
bool comp(int a, int b){
return a > b;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, nr, ile = 0, sum = 0;
cin >> n;
for(int i = 0; i < n; i++){
cin >> nr;
tab[nr] += 1;
uniq.insert(nr);
}
sort(tab, tab + n + 1, comp);
n = uniq.size();
for(int i = 0; i < n; i++){
sum = tab[i] - 1;
tab[i] = 0;
if(sum == -1){break;}
if(sum == 0){ile++;}
for(int j = n - 1; sum > 0; j--){
if(tab[j] == 0){cout << ile + 1; return 0;}
sum -= tab[j];
tab[j] = 0;
if(sum < 0){
tab[j] += -1 * sum;
n = j + 1;
ile++;
} else if(sum == 0){
ile++;
n = j;
}
}
}
cout << ile;
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 | #include<bits/stdc++.h> using namespace std; int tab[500007]; set<int> uniq; bool comp(int a, int b){ return a > b; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, nr, ile = 0, sum = 0; cin >> n; for(int i = 0; i < n; i++){ cin >> nr; tab[nr] += 1; uniq.insert(nr); } sort(tab, tab + n + 1, comp); n = uniq.size(); for(int i = 0; i < n; i++){ sum = tab[i] - 1; tab[i] = 0; if(sum == -1){break;} if(sum == 0){ile++;} for(int j = n - 1; sum > 0; j--){ if(tab[j] == 0){cout << ile + 1; return 0;} sum -= tab[j]; tab[j] = 0; if(sum < 0){ tab[j] += -1 * sum; n = j + 1; ile++; } else if(sum == 0){ ile++; n = j; } } } cout << ile; return 0; } |
English