#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
int remaining = n;
vector<int> occur(n + 1);
vector<pair<int,int>> order;
int answer = 0;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
occur[a]++;
}
{
priority_queue<pair<int,int>> q_order;
for (int i = 1; i <= n; i++) {
if (occur[i] == 0) {
continue;
}
q_order.push({ occur[i], i});
}
while (!q_order.empty()) {
auto first = q_order.top();
order.push_back(first);
q_order.pop();
}
}
auto iter = order.rbegin();
for (auto elem : order) {
int amount = elem.first;
int which = elem.second;
if (amount > remaining / 2) {
answer++;
break;
}
int to_remove = amount - 1;
for (auto it = iter; it != order.rend() && to_remove != 0; it++) {
if (it->first >= to_remove) {
it->first -= to_remove;
to_remove = 0;
} else {
to_remove -= it->first;
it->first = 0;
}
}
remaining -= 2 * amount - 1;
answer++;
}
cout << answer << "\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 51 52 53 54 55 56 57 | #include <iostream> #include <vector> #include <queue> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int remaining = n; vector<int> occur(n + 1); vector<pair<int,int>> order; int answer = 0; for (int i = 0; i < n; i++) { int a; cin >> a; occur[a]++; } { priority_queue<pair<int,int>> q_order; for (int i = 1; i <= n; i++) { if (occur[i] == 0) { continue; } q_order.push({ occur[i], i}); } while (!q_order.empty()) { auto first = q_order.top(); order.push_back(first); q_order.pop(); } } auto iter = order.rbegin(); for (auto elem : order) { int amount = elem.first; int which = elem.second; if (amount > remaining / 2) { answer++; break; } int to_remove = amount - 1; for (auto it = iter; it != order.rend() && to_remove != 0; it++) { if (it->first >= to_remove) { it->first -= to_remove; to_remove = 0; } else { to_remove -= it->first; it->first = 0; } } remaining -= 2 * amount - 1; answer++; } cout << answer << "\n"; } |
English