#include <algorithm> #include <iostream> #include <unordered_map> #include <vector> int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; std::unordered_map<int, int> v_to_count; for (int i = 0; i < n; ++i) { int v; std::cin >> v; ++v_to_count[v]; } std::vector<int> counts; for (auto [_, c] : v_to_count) { counts.emplace_back(c); } std::sort(counts.begin(), counts.end()); int total = 0; int l = 0; int r = counts.size() - 1; while (l <= r) { ++total; while (l < r && counts[r] > 1) { if (counts[r] - 1 >= counts[l]) { counts[r] -= counts[l]; ++l; } else { counts[l] -= counts[r] - 1; counts[r] = 1; } } --r; } std::cout << total << std::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 | #include <algorithm> #include <iostream> #include <unordered_map> #include <vector> int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; std::unordered_map<int, int> v_to_count; for (int i = 0; i < n; ++i) { int v; std::cin >> v; ++v_to_count[v]; } std::vector<int> counts; for (auto [_, c] : v_to_count) { counts.emplace_back(c); } std::sort(counts.begin(), counts.end()); int total = 0; int l = 0; int r = counts.size() - 1; while (l <= r) { ++total; while (l < r && counts[r] > 1) { if (counts[r] - 1 >= counts[l]) { counts[r] -= counts[l]; ++l; } else { counts[l] -= counts[r] - 1; counts[r] = 1; } } --r; } std::cout << total << std::endl; return 0; } |