#include <algorithm> #include <cstdio> #include <cstring> #include <vector> int solve(int n, const int* counter) { std::vector<int> data; for (int i = 1; i <= n; ++i) { if (0 < counter[i]) { data.emplace_back(counter[i]); } } std::sort(data.rbegin(), data.rend()); int solutions = 0; for (const auto& count : data) { solutions++; n -= count + count - 1; if (n <= 0) { break; } } return solutions; } int main() { constexpr auto MAX_N = 500000; int n, value, counter[MAX_N + 1]; std::fill_n(counter, MAX_N + 1, 0); scanf("%d\n", &n); for (int i = 0; i < n; ++i) { scanf("%d", &value); counter[value]++; } printf("%d\n", solve(n, counter)); }
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 | #include <algorithm> #include <cstdio> #include <cstring> #include <vector> int solve(int n, const int* counter) { std::vector<int> data; for (int i = 1; i <= n; ++i) { if (0 < counter[i]) { data.emplace_back(counter[i]); } } std::sort(data.rbegin(), data.rend()); int solutions = 0; for (const auto& count : data) { solutions++; n -= count + count - 1; if (n <= 0) { break; } } return solutions; } int main() { constexpr auto MAX_N = 500000; int n, value, counter[MAX_N + 1]; std::fill_n(counter, MAX_N + 1, 0); scanf("%d\n", &n); for (int i = 0; i < n; ++i) { scanf("%d", &value); counter[value]++; } printf("%d\n", solve(n, counter)); } |