#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double db; typedef pair<int,int> pii; typedef vector<int> vi; #define pb push_back #define fi first #define se second #define rep(i,b,e) for(int i=(b); i<(e); i++) #define each(a,x) for(auto &a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() bool isOk(const vi &arr, const int k){ int emptyPlaces = 0; rep(i,0,min(k,(int)arr.size())) emptyPlaces += arr[i]-1; rep(i,k,arr.size()) emptyPlaces -= arr[i]; return emptyPlaces >= 0; } int main(){ ios_base::sync_with_stdio(0); srand(time(0)); cin.tie(0); int n; cin >> n; vi arr; { map<int,int> cntElem; int a; rep(i,0,n){ cin >> a; ++cntElem[a]; } for(auto &&[elem,cnt] : cntElem){ arr.pb(cnt); } } sort(all(arr)); reverse(all(arr)); int L=1,R=n,best=n,mid; while(L<=R){ mid = (L+R)/2; if(isOk(arr,mid)){ best = mid; R = mid-1; }else{ L = mid+1; } } cout << best; 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double db; typedef pair<int,int> pii; typedef vector<int> vi; #define pb push_back #define fi first #define se second #define rep(i,b,e) for(int i=(b); i<(e); i++) #define each(a,x) for(auto &a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() bool isOk(const vi &arr, const int k){ int emptyPlaces = 0; rep(i,0,min(k,(int)arr.size())) emptyPlaces += arr[i]-1; rep(i,k,arr.size()) emptyPlaces -= arr[i]; return emptyPlaces >= 0; } int main(){ ios_base::sync_with_stdio(0); srand(time(0)); cin.tie(0); int n; cin >> n; vi arr; { map<int,int> cntElem; int a; rep(i,0,n){ cin >> a; ++cntElem[a]; } for(auto &&[elem,cnt] : cntElem){ arr.pb(cnt); } } sort(all(arr)); reverse(all(arr)); int L=1,R=n,best=n,mid; while(L<=R){ mid = (L+R)/2; if(isOk(arr,mid)){ best = mid; R = mid-1; }else{ L = mid+1; } } cout << best; return 0; } |