#include <iostream> #include <vector> #include <unordered_map> #include <algorithm> using namespace std; bool cmp(int a, int b) { return a > b; } int main() { ios_base::sync_with_stdio(false); int n,q; cin >> n; unordered_map<int, int> num_to_counters; for(int i = 0; i < n; i++) { int x; cin >> x; num_to_counters[x]++; } vector<int> counters; for(auto num_to_counter : num_to_counters) { counters.push_back(num_to_counter.second); } sort(counters.begin(), counters.end(), cmp); // for(auto k : counters) // { // cout << k << " | "; // } // cout << endl; int result = 0; int p = 0; int k = counters.size()-1; while(p <= k) { if(counters[p] * 2 > n) { result++; break; } int tmp = counters[p] - 1; n -= counters[p]; counters[p] = 0; p++; result++; while(tmp > 0) { if(counters[k] <= tmp) { tmp -= counters[k]; n -= counters[k]; counters[k] = 0; k--; } else { counters[k] -= tmp; n -= tmp; tmp = 0; } } } cout << result << endl; }
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 | #include <iostream> #include <vector> #include <unordered_map> #include <algorithm> using namespace std; bool cmp(int a, int b) { return a > b; } int main() { ios_base::sync_with_stdio(false); int n,q; cin >> n; unordered_map<int, int> num_to_counters; for(int i = 0; i < n; i++) { int x; cin >> x; num_to_counters[x]++; } vector<int> counters; for(auto num_to_counter : num_to_counters) { counters.push_back(num_to_counter.second); } sort(counters.begin(), counters.end(), cmp); // for(auto k : counters) // { // cout << k << " | "; // } // cout << endl; int result = 0; int p = 0; int k = counters.size()-1; while(p <= k) { if(counters[p] * 2 > n) { result++; break; } int tmp = counters[p] - 1; n -= counters[p]; counters[p] = 0; p++; result++; while(tmp > 0) { if(counters[k] <= tmp) { tmp -= counters[k]; n -= counters[k]; counters[k] = 0; k--; } else { counters[k] -= tmp; n -= tmp; tmp = 0; } } } cout << result << endl; } |