#include <bits/stdc++.h> #define DEBUG(x) cout << #x << ": " << x << '\n'; using namespace std; using ll = long long; int main() { int n; cin >> n; vector<int>cnt(n); for (int i =0; i < n; ++i) { int x; cin >> x; cnt[x - 1]++; } multiset<int> s; for (int i = 0; i < n; ++i) { if (cnt[i] == 0) continue; s.insert(cnt[i]); } int ans = 0; while (!s.empty()) { ans++; int cap = *s.rbegin() / 2; s.erase(prev(s.end())); while (!s.empty() && cap != 0) { int x = *s.begin(); s.erase(s.begin()); int zz = min(x, cap); cap -= zz; x -= zz; if (x != 0) { s.insert(x); } } } cout << ans; }
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 | #include <bits/stdc++.h> #define DEBUG(x) cout << #x << ": " << x << '\n'; using namespace std; using ll = long long; int main() { int n; cin >> n; vector<int>cnt(n); for (int i =0; i < n; ++i) { int x; cin >> x; cnt[x - 1]++; } multiset<int> s; for (int i = 0; i < n; ++i) { if (cnt[i] == 0) continue; s.insert(cnt[i]); } int ans = 0; while (!s.empty()) { ans++; int cap = *s.rbegin() / 2; s.erase(prev(s.end())); while (!s.empty() && cap != 0) { int x = *s.begin(); s.erase(s.begin()); int zz = min(x, cap); cap -= zz; x -= zz; if (x != 0) { s.insert(x); } } } cout << ans; } |