#include <bits/stdc++.h> #define ll long long #define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define pb push_back #define f first #define s second #define pii pair<int, int> #define pll pair<ll, ll> using namespace std; const int N = 5e5+1; int n,cnt[N]; int main(){ fast int n; cin >> n; for(int i = 0;i < n;i++){ int a; cin >> a; cnt[a]++; } vector<int>cnt2; for(int i = 1;i <= n;i++) if(cnt[i] > 0) cnt2.pb(cnt[i]); sort(cnt2.begin(),cnt2.end()); int a = 0,b = cnt2.size() - 1,res = cnt2.size(); while(a < b){ int ile = min(cnt2[a],cnt2[b]-1); cnt2[a] -= ile; cnt2[b] -= ile; if(cnt2[a] == 0){ res--; a++; } if(cnt2[b]==1) b--; } cout << res << "\n"; }
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 | #include <bits/stdc++.h> #define ll long long #define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define pb push_back #define f first #define s second #define pii pair<int, int> #define pll pair<ll, ll> using namespace std; const int N = 5e5+1; int n,cnt[N]; int main(){ fast int n; cin >> n; for(int i = 0;i < n;i++){ int a; cin >> a; cnt[a]++; } vector<int>cnt2; for(int i = 1;i <= n;i++) if(cnt[i] > 0) cnt2.pb(cnt[i]); sort(cnt2.begin(),cnt2.end()); int a = 0,b = cnt2.size() - 1,res = cnt2.size(); while(a < b){ int ile = min(cnt2[a],cnt2[b]-1); cnt2[a] -= ile; cnt2[b] -= ile; if(cnt2[a] == 0){ res--; a++; } if(cnt2[b]==1) b--; } cout << res << "\n"; } |