#include <bits/stdc++.h> using namespace std; int main() { int n; scanf(" %d", &n); map<int, int> counts; for (int i = 0; i < n; i++) { int v; scanf(" %d", &v); counts[v]++; } deque<pair<int, int>> pairs; for (auto [k, v] : counts) { pairs.push_back({v, k}); } sort(pairs.begin(), pairs.end()); int result = 0; while (pairs.size()) { auto [count, val] = pairs.back(); count--; pairs.pop_back(); result++; while (count > 0 && pairs.size()) { auto [count_lower, val_lower] = pairs.front(); pairs.pop_front(); count -= count_lower; if (count < 0) { pairs.push_front({-count, val}); count = 0; } } } printf("%d\n", result); }
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 | #include <bits/stdc++.h> using namespace std; int main() { int n; scanf(" %d", &n); map<int, int> counts; for (int i = 0; i < n; i++) { int v; scanf(" %d", &v); counts[v]++; } deque<pair<int, int>> pairs; for (auto [k, v] : counts) { pairs.push_back({v, k}); } sort(pairs.begin(), pairs.end()); int result = 0; while (pairs.size()) { auto [count, val] = pairs.back(); count--; pairs.pop_back(); result++; while (count > 0 && pairs.size()) { auto [count_lower, val_lower] = pairs.front(); pairs.pop_front(); count -= count_lower; if (count < 0) { pairs.push_front({-count, val}); count = 0; } } } printf("%d\n", result); } |