#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { int n, x, m = 0, groups = 0; vector <int> counts; cin >> n; int count[n+1]; for(int i = 0; i <= n; i++) { count[i] = 0; } for(int i = 0; i < n; i++) { cin >> x; count[x] += 1; } for(int i = n; i >= 0; i--) { if(count[i] > 0) { counts.push_back(count[i]); } } x = counts.size(); sort(counts.begin(), counts.end()); for(int i = x - 1; i >= 0; i--) { m += counts[i] + counts[i] - 1; groups += 1; if(m >= n) { cout << groups; return 0; } } 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 | #include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { int n, x, m = 0, groups = 0; vector <int> counts; cin >> n; int count[n+1]; for(int i = 0; i <= n; i++) { count[i] = 0; } for(int i = 0; i < n; i++) { cin >> x; count[x] += 1; } for(int i = n; i >= 0; i--) { if(count[i] > 0) { counts.push_back(count[i]); } } x = counts.size(); sort(counts.begin(), counts.end()); for(int i = x - 1; i >= 0; i--) { m += counts[i] + counts[i] - 1; groups += 1; if(m >= n) { cout << groups; return 0; } } return 0; } |