// https://sio2.mimuw.edu.pl/c/pa-2024-1/p/lid/ #include <bits/stdc++.h> #define st first #define nd second #define pb push_back #define all(x) x.begin(), x.end() #define rep(i, a) for (int i = 0; i < (a); i++) #define rep1(i, a) for (int i = 1; i <= (a); i++) using namespace std; typedef long long ll; typedef pair<int, int> pi; const int MAX_N = 5e5 + 5; int cnt[MAX_N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; rep(i, n) { int x; cin >> x; cnt[x]++; } vector<int> t; rep1(i, n) { if (cnt[i]) t.pb(cnt[i]); } sort(all(t), greater<int>()); deque<int> q; for (auto c : t) q.push_back(c); int res = 0; while (q.size()) { res++; int f = q.front() - 1; // cerr << f << '\n'; q.pop_front(); while (f > 0 && q.size()) { int b = q.back(); q.pop_back(); // cerr << ' ' << f << ' ' << b << '\n'; f -= b; if (f < 0) { b = -f; f = 0; q.pb(b); } } } cout << res << '\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 | // https://sio2.mimuw.edu.pl/c/pa-2024-1/p/lid/ #include <bits/stdc++.h> #define st first #define nd second #define pb push_back #define all(x) x.begin(), x.end() #define rep(i, a) for (int i = 0; i < (a); i++) #define rep1(i, a) for (int i = 1; i <= (a); i++) using namespace std; typedef long long ll; typedef pair<int, int> pi; const int MAX_N = 5e5 + 5; int cnt[MAX_N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; rep(i, n) { int x; cin >> x; cnt[x]++; } vector<int> t; rep1(i, n) { if (cnt[i]) t.pb(cnt[i]); } sort(all(t), greater<int>()); deque<int> q; for (auto c : t) q.push_back(c); int res = 0; while (q.size()) { res++; int f = q.front() - 1; // cerr << f << '\n'; q.pop_front(); while (f > 0 && q.size()) { int b = q.back(); q.pop_back(); // cerr << ' ' << f << ' ' << b << '\n'; f -= b; if (f < 0) { b = -f; f = 0; q.pb(b); } } } cout << res << '\n'; } |