#include <bits/stdc++.h> using namespace std; const int N = 5e5+7; int cnt[N]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; for(int i = 1;i<=n;i+=1){ int val; cin>>val; cnt[val] += 1; } sort(cnt+1,cnt+1+n,greater<int>()); int sum = 0; int ans = 0; for(int i = 1;i<=n;i+=1){ sum += cnt[i]*2-1; ans += 1; if (sum>=n){ break; } } cout<<ans<<'\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 | #include <bits/stdc++.h> using namespace std; const int N = 5e5+7; int cnt[N]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; for(int i = 1;i<=n;i+=1){ int val; cin>>val; cnt[val] += 1; } sort(cnt+1,cnt+1+n,greater<int>()); int sum = 0; int ans = 0; for(int i = 1;i<=n;i+=1){ sum += cnt[i]*2-1; ans += 1; if (sum>=n){ break; } } cout<<ans<<'\n'; } |