#include <bits/stdc++.h> const int MAX_N = 500000; int dp[MAX_N + 1]; int main() { int n; std::cin >> n; for (int i = 0; i < n; i++) { int num; std::cin >> num; dp[num]++; } std::vector<int> counts; for (int i = 0; i <= n; i++) if (dp[i] > 0) counts.push_back(dp[i]); std::sort(counts.begin(), counts.end(), std::greater<int>{}); // for (auto i : counts) // std::cout << std::setw(3) << i; // std::cout << '\n'; int i = 0, j = counts.size() - 1; while (i < j) { int cur = counts[i] - 1; while (cur > 0) { if (counts[j] <= cur) { cur -= counts[j]; counts[j] = 0; j--; } else { counts[j] -= cur; cur = 0; } } i++; } std::cout << i + (counts[i] != 0) << '\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 | #include <bits/stdc++.h> const int MAX_N = 500000; int dp[MAX_N + 1]; int main() { int n; std::cin >> n; for (int i = 0; i < n; i++) { int num; std::cin >> num; dp[num]++; } std::vector<int> counts; for (int i = 0; i <= n; i++) if (dp[i] > 0) counts.push_back(dp[i]); std::sort(counts.begin(), counts.end(), std::greater<int>{}); // for (auto i : counts) // std::cout << std::setw(3) << i; // std::cout << '\n'; int i = 0, j = counts.size() - 1; while (i < j) { int cur = counts[i] - 1; while (cur > 0) { if (counts[j] <= cur) { cur -= counts[j]; counts[j] = 0; j--; } else { counts[j] -= cur; cur = 0; } } i++; } std::cout << i + (counts[i] != 0) << '\n'; } |