#include <bits/stdc++.h> using namespace std; #define maxn 500010 int buckets[maxn]; int solve(int n) { vector<int> counts; for (int i = 1; i <= n; ++i) { if (buckets[i] != 0) { counts.push_back(buckets[i]); } } sort(counts.begin(), counts.end()); int p = 0; int k = (int)counts.size() - 1; int res = 0; while (p < k) { counts[k]--; res++; while (p < k && counts[p] <= counts[k]) { counts[k] -= counts[p]; ++p; } counts[p] -= counts[k]; --k; } if (counts[p] != 0) { ++res; } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int x,n; cin >> n; for (int i = 0; i < n; ++i) { cin >> x; buckets[x]++; } cout << solve(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 40 41 42 43 44 45 | #include <bits/stdc++.h> using namespace std; #define maxn 500010 int buckets[maxn]; int solve(int n) { vector<int> counts; for (int i = 1; i <= n; ++i) { if (buckets[i] != 0) { counts.push_back(buckets[i]); } } sort(counts.begin(), counts.end()); int p = 0; int k = (int)counts.size() - 1; int res = 0; while (p < k) { counts[k]--; res++; while (p < k && counts[p] <= counts[k]) { counts[k] -= counts[p]; ++p; } counts[p] -= counts[k]; --k; } if (counts[p] != 0) { ++res; } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int x,n; cin >> n; for (int i = 0; i < n; ++i) { cin >> x; buckets[x]++; } cout << solve(n); } |