#include <iostream> #include <vector> #include <algorithm> #include <cstdint> int main() { int n = 0; std::cin >> n; std::vector<int> tab; tab.assign(n, 0); for (int i = 0; i < n; ++i) { int a = 0; std::cin >> a; ++tab[a - 1]; } std::sort(tab.begin(), tab.end(), [](int a, int b) { if (a > b) return true; return false; }); if (tab[0] > n / 2) { std::cout << "1\n"; return 0; } int left = 0, right = std::lower_bound(tab.begin(), tab.end(), 0, [](int a, int b) { if (a > b) return true; return false; }) - tab.begin() - 1; while (left < right) { int sum = tab[left]; while (left < right) { if (tab[left] > (sum + tab[right]) / 2) { sum += tab[right]; --right; } else break; } ++left; } if (left == right) ++left; std::cout << left << '\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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | #include <iostream> #include <vector> #include <algorithm> #include <cstdint> int main() { int n = 0; std::cin >> n; std::vector<int> tab; tab.assign(n, 0); for (int i = 0; i < n; ++i) { int a = 0; std::cin >> a; ++tab[a - 1]; } std::sort(tab.begin(), tab.end(), [](int a, int b) { if (a > b) return true; return false; }); if (tab[0] > n / 2) { std::cout << "1\n"; return 0; } int left = 0, right = std::lower_bound(tab.begin(), tab.end(), 0, [](int a, int b) { if (a > b) return true; return false; }) - tab.begin() - 1; while (left < right) { int sum = tab[left]; while (left < right) { if (tab[left] > (sum + tab[right]) / 2) { sum += tab[right]; --right; } else break; } ++left; } if (left == right) ++left; std::cout << left << '\n'; } |