#include <algorithm> #include <iostream> #include <vector> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; std::cin >> n; std::vector<int> counters(n + 1, 0); for (int i = 0; i < n; i++) { int element; std::cin >> element; counters[element]++; } std::sort(counters.begin(), counters.end(), std::greater<int>()); std::vector<int> rests(n + 1, 0); rests[0] = n - counters[0]; for (int i = 1; i <= n; i++) { rests[i] = rests[i - 1] - counters[i]; } int result = 0; int taken = 0; for (int i = 0; i < n; i++) { result++; auto count = counters[i]; auto rest = rests[i] - taken; if (count > rest) { break; } taken += count - 1; } std::cout << result << "\n"; 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 | #include <algorithm> #include <iostream> #include <vector> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; std::cin >> n; std::vector<int> counters(n + 1, 0); for (int i = 0; i < n; i++) { int element; std::cin >> element; counters[element]++; } std::sort(counters.begin(), counters.end(), std::greater<int>()); std::vector<int> rests(n + 1, 0); rests[0] = n - counters[0]; for (int i = 1; i <= n; i++) { rests[i] = rests[i - 1] - counters[i]; } int result = 0; int taken = 0; for (int i = 0; i < n; i++) { result++; auto count = counters[i]; auto rest = rests[i] - taken; if (count > rest) { break; } taken += count - 1; } std::cout << result << "\n"; return 0; } |