#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pll; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); ll n; cin >> n; vector<ll> a(n+1, 0); for (ll i = 0; i < n; i++) { ll x; cin >> x; a[x]++; } sort(a.begin(), a.end()); reverse(a.begin(), a.end()); ll ans = 0; ll used = 0; ll iBig = 0; ll iSmall = n; while (used < n) { ans++; used += a[iBig]; ll to = a[iBig] - 1; while (to > 0) { if (a[iSmall] <= to) { used += a[iSmall]; to -= a[iSmall]; iSmall--; }else { used += to; a[iSmall] -= to; to = 0; } } iBig++; } cout << ans << "\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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pll; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); ll n; cin >> n; vector<ll> a(n+1, 0); for (ll i = 0; i < n; i++) { ll x; cin >> x; a[x]++; } sort(a.begin(), a.end()); reverse(a.begin(), a.end()); ll ans = 0; ll used = 0; ll iBig = 0; ll iSmall = n; while (used < n) { ans++; used += a[iBig]; ll to = a[iBig] - 1; while (to > 0) { if (a[iSmall] <= to) { used += a[iSmall]; to -= a[iSmall]; iSmall--; }else { used += to; a[iSmall] -= to; to = 0; } } iBig++; } cout << ans << "\n"; } |