#include <algorithm> #include <bits/stdc++.h> #include <vector> using namespace std; int main(){ int n; cin>>n; vector<int> v(n); vector<int> cnt(n+1); vector<int> amounts; int lim = 0; for(int i=0; i<n; i++){ cin>>v[i]; lim = max(lim, v[i]); } for(auto e: v) cnt[e]++; for(int i=1; i<=lim; i++){ if(cnt[i]) amounts.push_back(cnt[i]); } sort(amounts.rbegin(), amounts.rend()); int liders = 0; int rest = n; int result = amounts.size(); for(int i=0; i<(int)amounts.size(); i++){ liders += amounts[i]-1; rest-= amounts[i]; if(liders >= rest) { result = i+1; break; } } cout<<result<<endl; }
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 | #include <algorithm> #include <bits/stdc++.h> #include <vector> using namespace std; int main(){ int n; cin>>n; vector<int> v(n); vector<int> cnt(n+1); vector<int> amounts; int lim = 0; for(int i=0; i<n; i++){ cin>>v[i]; lim = max(lim, v[i]); } for(auto e: v) cnt[e]++; for(int i=1; i<=lim; i++){ if(cnt[i]) amounts.push_back(cnt[i]); } sort(amounts.rbegin(), amounts.rend()); int liders = 0; int rest = n; int result = amounts.size(); for(int i=0; i<(int)amounts.size(); i++){ liders += amounts[i]-1; rest-= amounts[i]; if(liders >= rest) { result = i+1; break; } } cout<<result<<endl; } |