#include <iostream> #include <map> #include <unordered_map> #include <algorithm> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::unordered_map<int, int> data; std::map<int, int> used; int n, a; int res = 0; std::cin >> n; for (int i = 0; i < n; i++) { std::cin >> a; data[a]++; } for (const auto &[key, value] : data) { used[std::min(value + value - 1, n)]++; } data.clear(); auto i_used = used.rbegin(); while (i_used != used.rend()) { a = std::min(n / i_used->first + (n % i_used->first ? 1 : 0), i_used->second); n -= i_used->first * a; res += a; if (n <= 0) break; i_used++; } std::cout << res << '\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 | #include <iostream> #include <map> #include <unordered_map> #include <algorithm> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::unordered_map<int, int> data; std::map<int, int> used; int n, a; int res = 0; std::cin >> n; for (int i = 0; i < n; i++) { std::cin >> a; data[a]++; } for (const auto &[key, value] : data) { used[std::min(value + value - 1, n)]++; } data.clear(); auto i_used = used.rbegin(); while (i_used != used.rend()) { a = std::min(n / i_used->first + (n % i_used->first ? 1 : 0), i_used->second); n -= i_used->first * a; res += a; if (n <= 0) break; i_used++; } std::cout << res << '\n'; } |