#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> seq(n); for(int i = 0; i < n; i++) { cin >> seq[i]; } sort(seq.begin(), seq.end()); vector<pair<int, int>> cnt; int uni = 0; for (int i = 0; i < n;) { int c = 0; int v = seq[i]; while (i < n && seq[i] == v) { i++; c++; } if (c == 1) { uni++; } else { cnt.push_back({v, c}); } } if (cnt.empty()) { cout << (uni > 0 ? 1 : 0) << endl; return 0; } sort(cnt.begin(), cnt.end(), [](const pair<int, int>& a, const pair<int, int>& b) { return a.second < b.second; }); int res = 0; while (!cnt.empty()) { int lst = cnt.size() - 1; int spc = cnt[lst].second - 1; if (uni > 0) { int used = min(uni, spc); uni -= used; res++; cnt.pop_back(); } else if (cnt.size() > 1) { for (int i = 0; i < lst; ) { if (cnt[i].second - 1 <= spc) { spc -= cnt[i].second; cnt.erase(cnt.begin() + i); lst--; } else { i++; } } res++; cnt.pop_back(); } else { res += cnt.size(); break; } } res += uni; cout << res << 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 | #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> seq(n); for(int i = 0; i < n; i++) { cin >> seq[i]; } sort(seq.begin(), seq.end()); vector<pair<int, int>> cnt; int uni = 0; for (int i = 0; i < n;) { int c = 0; int v = seq[i]; while (i < n && seq[i] == v) { i++; c++; } if (c == 1) { uni++; } else { cnt.push_back({v, c}); } } if (cnt.empty()) { cout << (uni > 0 ? 1 : 0) << endl; return 0; } sort(cnt.begin(), cnt.end(), [](const pair<int, int>& a, const pair<int, int>& b) { return a.second < b.second; }); int res = 0; while (!cnt.empty()) { int lst = cnt.size() - 1; int spc = cnt[lst].second - 1; if (uni > 0) { int used = min(uni, spc); uni -= used; res++; cnt.pop_back(); } else if (cnt.size() > 1) { for (int i = 0; i < lst; ) { if (cnt[i].second - 1 <= spc) { spc -= cnt[i].second; cnt.erase(cnt.begin() + i); lst--; } else { i++; } } res++; cnt.pop_back(); } else { res += cnt.size(); break; } } res += uni; cout << res << endl; return 0; } |