#include<iostream> #include<string> #include<bitset> #include<utility> #include<vector> #include<array> #include<map> #include<algorithm> #include<deque> bool has_leader(int count, std::deque<int> const& counts) { int last = counts.back(); int rest_count = count - last; return last > rest_count; } int split(std::map<int, int> const& counter) { std::vector<int> counts_vec; int count = 0; int split_count = 1; for (auto const& [key, value] : counter) { counts_vec.push_back(value); count += value; } std::sort(counts_vec.begin(), counts_vec.end()); std::deque<int> counts(counts_vec.begin(), counts_vec.end()); while(!counts.empty() && !has_leader(count, counts)) { int last = counts.back(); counts.pop_back(); // for (int i : counts) // std::cout << i << " "; // std::cout << std::endl; int to_remove = last - 1; while (to_remove > 0) { int first = counts.front(); if (first <= to_remove) { counts.pop_front(); } else { counts[0] -= to_remove; } to_remove -= first; } split_count++; count -= 2*last-1; // std::cout << "COUNT " << count << std::endl; } return split_count; } int main() { int n; std::cin >> n; std::vector<int> data; std::map<int, int> counter; for (int i = 0; i<n; ++i) { int tmp; std::cin >> tmp; data.push_back(tmp); counter[tmp]++; } int result = split(counter); std::cout << result << std::endl; 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 78 | #include<iostream> #include<string> #include<bitset> #include<utility> #include<vector> #include<array> #include<map> #include<algorithm> #include<deque> bool has_leader(int count, std::deque<int> const& counts) { int last = counts.back(); int rest_count = count - last; return last > rest_count; } int split(std::map<int, int> const& counter) { std::vector<int> counts_vec; int count = 0; int split_count = 1; for (auto const& [key, value] : counter) { counts_vec.push_back(value); count += value; } std::sort(counts_vec.begin(), counts_vec.end()); std::deque<int> counts(counts_vec.begin(), counts_vec.end()); while(!counts.empty() && !has_leader(count, counts)) { int last = counts.back(); counts.pop_back(); // for (int i : counts) // std::cout << i << " "; // std::cout << std::endl; int to_remove = last - 1; while (to_remove > 0) { int first = counts.front(); if (first <= to_remove) { counts.pop_front(); } else { counts[0] -= to_remove; } to_remove -= first; } split_count++; count -= 2*last-1; // std::cout << "COUNT " << count << std::endl; } return split_count; } int main() { int n; std::cin >> n; std::vector<int> data; std::map<int, int> counter; for (int i = 0; i<n; ++i) { int tmp; std::cin >> tmp; data.push_back(tmp); counter[tmp]++; } int result = split(counter); std::cout << result << std::endl; return 0; } |