#include <bits/stdc++.h> struct Cmp { bool operator()(const std::pair<int, int> & a, const std::pair<int, int> & b) const { if (a.second == b.second) { return a.first < b.first; } return a.second > b.second; } }; int T[500'005]; std::map<int, int> T_bucket; std::vector<std::pair<int, int>> T_score; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); int n; std::cin >> n; for (int i = 0; i < n; ++i) { std::cin >> T[i]; ++T_bucket[T[i]]; } for (auto & [k, v] : T_bucket) { T_score.push_back({k, v}); } std::sort(T_score.begin(), T_score.end(), Cmp()); if (T_score.size() == 1) { std::cout << 1 << '\n'; return 0; } int lo = 0; int hi = T_score.size() - 1; bool is_first = true; while (lo < hi) { // for (auto & x : T_score) { // std::cout << '(' << x.first << ',' << x.second << ") "; // } // std::cout << "Out: " << lo + 1; // std::cout << '\n'; if (T_score[lo].second == 0) { ++lo; is_first = true; } else if (is_first) { T_score[lo].second -= 1; is_first = false; } else if (T_score[lo].second > T_score[hi].second) { T_score[lo].second -= T_score[hi].second; T_score[hi].second = 0; --hi; } else if (T_score[lo].second == T_score[hi].second) { T_score[lo].second = 0; T_score[hi].second = 0; --hi; } else { T_score[hi].second -= T_score[lo].second; T_score[lo].second = 0; } } std::cout << lo + 1 << '\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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #include <bits/stdc++.h> struct Cmp { bool operator()(const std::pair<int, int> & a, const std::pair<int, int> & b) const { if (a.second == b.second) { return a.first < b.first; } return a.second > b.second; } }; int T[500'005]; std::map<int, int> T_bucket; std::vector<std::pair<int, int>> T_score; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); int n; std::cin >> n; for (int i = 0; i < n; ++i) { std::cin >> T[i]; ++T_bucket[T[i]]; } for (auto & [k, v] : T_bucket) { T_score.push_back({k, v}); } std::sort(T_score.begin(), T_score.end(), Cmp()); if (T_score.size() == 1) { std::cout << 1 << '\n'; return 0; } int lo = 0; int hi = T_score.size() - 1; bool is_first = true; while (lo < hi) { // for (auto & x : T_score) { // std::cout << '(' << x.first << ',' << x.second << ") "; // } // std::cout << "Out: " << lo + 1; // std::cout << '\n'; if (T_score[lo].second == 0) { ++lo; is_first = true; } else if (is_first) { T_score[lo].second -= 1; is_first = false; } else if (T_score[lo].second > T_score[hi].second) { T_score[lo].second -= T_score[hi].second; T_score[hi].second = 0; --hi; } else if (T_score[lo].second == T_score[hi].second) { T_score[lo].second = 0; T_score[hi].second = 0; --hi; } else { T_score[hi].second -= T_score[lo].second; T_score[lo].second = 0; } } std::cout << lo + 1 << '\n'; return 0; } |