#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
#include <unordered_map>
using namespace std;
int nextInt() { int n; scanf("%d", &n); return n; }
unordered_map<int, int> m;
int solve(int n) {
int res = 0;
vector<int> v;
v.reserve(m.size());
for (auto it = m.begin(); it != m.end(); ++it)
v.push_back(it->second);
sort(v.begin(), v.end());
int a = 0;
int b = v.size() - 1;
while (a < b) {
++res;
int cnt = v[b];
if (cnt + cnt > n) return res;
if (1 == cnt) return res + b - a;
n -= cnt + cnt - 1;
--b;
--cnt;
while (v[a] <= cnt) {
cnt -= v[a];
++a;
}
if (cnt > 0) v[a] -= cnt;
}
if (a == b) ++res;
return res;
}
int main() {
int n = nextInt();
for (int i = 0; i < n; ++i)
++m[nextInt()];
int res = solve(n);
printf("%d\n", res);
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 | #include <cstdio> #include <vector> #include <algorithm> #include <map> #include <unordered_map> using namespace std; int nextInt() { int n; scanf("%d", &n); return n; } unordered_map<int, int> m; int solve(int n) { int res = 0; vector<int> v; v.reserve(m.size()); for (auto it = m.begin(); it != m.end(); ++it) v.push_back(it->second); sort(v.begin(), v.end()); int a = 0; int b = v.size() - 1; while (a < b) { ++res; int cnt = v[b]; if (cnt + cnt > n) return res; if (1 == cnt) return res + b - a; n -= cnt + cnt - 1; --b; --cnt; while (v[a] <= cnt) { cnt -= v[a]; ++a; } if (cnt > 0) v[a] -= cnt; } if (a == b) ++res; return res; } int main() { int n = nextInt(); for (int i = 0; i < n; ++i) ++m[nextInt()]; int res = solve(n); printf("%d\n", res); return 0; } |
English