#include<iostream> #include<vector> #include<list> #include<functional> using namespace std; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n, a; cin >> n; vector<int> counter(n + 1, 0); for (int i = 0; i < n; i++) { cin >> a; counter[a]++; } sort(counter.begin(), counter.end(), greater<>()); int result = 0; list<int> remaining_counters(counter.begin(), counter.end()); while (remaining_counters.back() == 0) remaining_counters.pop_back(); // for (auto idx = remaining_counters.begin(); idx != remaining_counters.end(); idx++) { // cout << *idx << " "; // } // cout << "\n"; while (remaining_counters.size() > 0) { result++; int top = remaining_counters.front(); remaining_counters.pop_front(); // cout << "top " << top << "\n"; while (remaining_counters.size() > 0 && top > 1) { int back = remaining_counters.back(); // cout << "back " << back << "\n"; remaining_counters.pop_back(); if (back < top) { top -= back; } else { remaining_counters.push_back(back - top + 1); top = 0; } } } cout << result << "\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 | #include<iostream> #include<vector> #include<list> #include<functional> using namespace std; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n, a; cin >> n; vector<int> counter(n + 1, 0); for (int i = 0; i < n; i++) { cin >> a; counter[a]++; } sort(counter.begin(), counter.end(), greater<>()); int result = 0; list<int> remaining_counters(counter.begin(), counter.end()); while (remaining_counters.back() == 0) remaining_counters.pop_back(); // for (auto idx = remaining_counters.begin(); idx != remaining_counters.end(); idx++) { // cout << *idx << " "; // } // cout << "\n"; while (remaining_counters.size() > 0) { result++; int top = remaining_counters.front(); remaining_counters.pop_front(); // cout << "top " << top << "\n"; while (remaining_counters.size() > 0 && top > 1) { int back = remaining_counters.back(); // cout << "back " << back << "\n"; remaining_counters.pop_back(); if (back < top) { top -= back; } else { remaining_counters.push_back(back - top + 1); top = 0; } } } cout << result << "\n"; } |