#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int count_liders(vector<int>& a){
sort(a.begin(), a.end());
int i = 0, j = a.size()-1;
int res = 1;
while(i < j){
if(!a[i]){
i++;
continue;
}
if(a[j] - a[i] > 0){
a[j] -= a[i];
i++;
}
else{
a[i] = a[i] - a[j] + 1;
j--;
res++;
}
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n; cin >> n;
vector<int> a(n,0);
for(int i = 0; i < n; i++){
int tmp; cin >> tmp;
a[tmp-1]++;
}
cout << count_liders(a) << 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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; int count_liders(vector<int>& a){ sort(a.begin(), a.end()); int i = 0, j = a.size()-1; int res = 1; while(i < j){ if(!a[i]){ i++; continue; } if(a[j] - a[i] > 0){ a[j] -= a[i]; i++; } else{ a[i] = a[i] - a[j] + 1; j--; res++; } } return res; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> a(n,0); for(int i = 0; i < n; i++){ int tmp; cin >> tmp; a[tmp-1]++; } cout << count_liders(a) << endl; return 0; } |
English