#include <bits/stdc++.h> using namespace std; int tab[500007]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; for(int i=0;i<n;i++){ int x; cin>>x; tab[x]++; } multiset<int> vals; for(int i=0;i<500007;i++){ if(tab[i]) vals.insert(tab[i]); } //for(auto i : vals) cout<<i<<" "; //cout<<endl; if(vals.size() == 1){ cout<<1<<endl; return 0; } int cnt = 0; while(!vals.empty()){ auto last = --vals.end(); int maxi = *last; vals.erase(last); int tmp = maxi; maxi--; while(maxi){ if(vals.empty()) break; //if no more to add auto first = vals.begin(); int mini = *first; if(mini <= maxi){ maxi -= mini; vals.erase(first); tmp += mini; } else{ // mini > maxi mini -= maxi; tmp += maxi; maxi = 0; vals.erase(first); vals.insert(mini); } } cnt++; //cout<<tmp<<endl; } cout<<cnt<<endl; 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 | #include <bits/stdc++.h> using namespace std; int tab[500007]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; for(int i=0;i<n;i++){ int x; cin>>x; tab[x]++; } multiset<int> vals; for(int i=0;i<500007;i++){ if(tab[i]) vals.insert(tab[i]); } //for(auto i : vals) cout<<i<<" "; //cout<<endl; if(vals.size() == 1){ cout<<1<<endl; return 0; } int cnt = 0; while(!vals.empty()){ auto last = --vals.end(); int maxi = *last; vals.erase(last); int tmp = maxi; maxi--; while(maxi){ if(vals.empty()) break; //if no more to add auto first = vals.begin(); int mini = *first; if(mini <= maxi){ maxi -= mini; vals.erase(first); tmp += mini; } else{ // mini > maxi mini -= maxi; tmp += maxi; maxi = 0; vals.erase(first); vals.insert(mini); } } cnt++; //cout<<tmp<<endl; } cout<<cnt<<endl; return 0; } |