#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 * 5 + 10; int A[MAX]; int cnt[MAX]; vector<int>L; int solve(int n){ for (int i = 1; i <= n; i++){ if (cnt[i]){ L.push_back(cnt[i]); } } sort(L.begin(),L.end(),greater<int>()); int ans = 0, x = L.size(), m = L.size(); for (int i = 0; i < m; i++){ if (i >= x){ break; } int tmp = L[i] - 1; while (x > 0 && tmp >= L[x-1]){ x--; tmp -= L[x]; } L[x-1] -= tmp; ans++; } return ans; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; i++){ cin >> A[i]; cnt[A[i]]++; } cout << solve(n) << "\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 | #include <bits/stdc++.h> using namespace std; const int MAX = 1e5 * 5 + 10; int A[MAX]; int cnt[MAX]; vector<int>L; int solve(int n){ for (int i = 1; i <= n; i++){ if (cnt[i]){ L.push_back(cnt[i]); } } sort(L.begin(),L.end(),greater<int>()); int ans = 0, x = L.size(), m = L.size(); for (int i = 0; i < m; i++){ if (i >= x){ break; } int tmp = L[i] - 1; while (x > 0 && tmp >= L[x-1]){ x--; tmp -= L[x]; } L[x-1] -= tmp; ans++; } return ans; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; i++){ cin >> A[i]; cnt[A[i]]++; } cout << solve(n) << "\n"; } |