#include <bits/stdc++.h>
using namespace std;
const int N = 5e5+3;
int cnt[N];
bool cmp(int a, int b){
return a>b;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for(int i=0; i<n; i++){
int x;
cin >> x;
x--;
cnt[x]++;
}
sort(cnt, cnt+n, cmp);
int k=n-1, cur=0, out=0;
for(int i=0; i<n; i++){
if(cnt[i]==0) break;
while(k>i && cur<cnt[i]){
if(cur+cnt[k]<cnt[i]){
cur+=cnt[k];
cnt[k]=0;
k--;
}else{
cnt[k]-=cnt[i]-1-cur;
cur=0;
break;
}
}
out++;
}
cout << out << '\n';
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 | #include <bits/stdc++.h> using namespace std; const int N = 5e5+3; int cnt[N]; bool cmp(int a, int b){ return a>b; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for(int i=0; i<n; i++){ int x; cin >> x; x--; cnt[x]++; } sort(cnt, cnt+n, cmp); int k=n-1, cur=0, out=0; for(int i=0; i<n; i++){ if(cnt[i]==0) break; while(k>i && cur<cnt[i]){ if(cur+cnt[k]<cnt[i]){ cur+=cnt[k]; cnt[k]=0; k--; }else{ cnt[k]-=cnt[i]-1-cur; cur=0; break; } } out++; } cout << out << '\n'; return 0; } |
English