#include<bits/stdc++.h> using namespace std; int n,a,l,r,mid,ans; vector<int> possible_leaders, pom; int val[500007]; void input() { cin >> n; for(int i=1; i<=n; i++) { cin >> a; val[a]++; } for(int i=1; i<=n; i++) { if(val[i]>0) { possible_leaders.push_back(val[i]); } } sort(possible_leaders.begin(),possible_leaders.end()); return; } bool check(int x) { pom=possible_leaders; int k=possible_leaders.size()-x-1; if(k>-1) for(int i=possible_leaders.size()-1; i>=possible_leaders.size()-x; i--) { a=pom[i]-1; while(k>=0) { if(a-pom[k]>=0) { a-=pom[k]; } else { pom[k]-=a; break; } k--; } } if(k<=-1) return 1; else return 0; } int bin_search() { ans=possible_leaders.size(); l=1; r=possible_leaders.size(); if(l!=r) while(l<=r) { mid=(l+r)/2; if(check(mid)==1) { r=mid-1; ans=mid; } else { l=mid+1; } } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); input(); cout << bin_search(); 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | #include<bits/stdc++.h> using namespace std; int n,a,l,r,mid,ans; vector<int> possible_leaders, pom; int val[500007]; void input() { cin >> n; for(int i=1; i<=n; i++) { cin >> a; val[a]++; } for(int i=1; i<=n; i++) { if(val[i]>0) { possible_leaders.push_back(val[i]); } } sort(possible_leaders.begin(),possible_leaders.end()); return; } bool check(int x) { pom=possible_leaders; int k=possible_leaders.size()-x-1; if(k>-1) for(int i=possible_leaders.size()-1; i>=possible_leaders.size()-x; i--) { a=pom[i]-1; while(k>=0) { if(a-pom[k]>=0) { a-=pom[k]; } else { pom[k]-=a; break; } k--; } } if(k<=-1) return 1; else return 0; } int bin_search() { ans=possible_leaders.size(); l=1; r=possible_leaders.size(); if(l!=r) while(l<=r) { mid=(l+r)/2; if(check(mid)==1) { r=mid-1; ans=mid; } else { l=mid+1; } } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); input(); cout << bin_search(); return 0; } |