#include <bits/stdc++.h> constexpr int MAXN = 5e5 + 7; int cnt[MAXN]; auto main() -> int { int n; std::cin >> n; std::vector<int> a(n); for (auto &elem : a) { std::cin >> elem; cnt[elem]++; } std::vector<int> v; for (int i = 1; i <= n; ++i) { if (cnt[i]) v.push_back(cnt[i]); } sort(v.begin(), v.end()); size_t j = v.size() - 1; int ans = 1; for (size_t i = 0; i < j; ++i) { int to_be_paired = v[j] - 1; while (i < j) { if (to_be_paired - v[i] >= 0) { to_be_paired -= v[i]; ++i; } else { ans++; v[i] -= to_be_paired; // we take as much as we can so that it will be less exhausting for next leaders --i; // it will be added back in the next loop --j; // next leader break; } } } std::cout << ans << '\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> constexpr int MAXN = 5e5 + 7; int cnt[MAXN]; auto main() -> int { int n; std::cin >> n; std::vector<int> a(n); for (auto &elem : a) { std::cin >> elem; cnt[elem]++; } std::vector<int> v; for (int i = 1; i <= n; ++i) { if (cnt[i]) v.push_back(cnt[i]); } sort(v.begin(), v.end()); size_t j = v.size() - 1; int ans = 1; for (size_t i = 0; i < j; ++i) { int to_be_paired = v[j] - 1; while (i < j) { if (to_be_paired - v[i] >= 0) { to_be_paired -= v[i]; ++i; } else { ans++; v[i] -= to_be_paired; // we take as much as we can so that it will be less exhausting for next leaders --i; // it will be added back in the next loop --j; // next leader break; } } } std::cout << ans << '\n'; } |