//2024-03-11 //author: Marcin Rolbiecki #include <bits/stdc++.h> using namespace std; const int maxN = 5e5+2; int n; int a[maxN]; int ile[maxN]; bool comp (int x, int y) { if (ile[x] == ile[y]) return x > y; else return ile[x] >= ile[y]; } int main () { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; ile[a[i]]++; } sort(a+1, a+1+n, comp); int f = 1, b = n, ans = 0, co, cnt; while (f <= b) { co = a[f], cnt = 1; while (f+1 <= b && a[f+1] == co) cnt++, f++; while (f <= b && cnt > 1) cnt--, b--; ans++; f++; } cout << ans; 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 | //2024-03-11 //author: Marcin Rolbiecki #include <bits/stdc++.h> using namespace std; const int maxN = 5e5+2; int n; int a[maxN]; int ile[maxN]; bool comp (int x, int y) { if (ile[x] == ile[y]) return x > y; else return ile[x] >= ile[y]; } int main () { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; ile[a[i]]++; } sort(a+1, a+1+n, comp); int f = 1, b = n, ans = 0, co, cnt; while (f <= b) { co = a[f], cnt = 1; while (f+1 <= b && a[f+1] == co) cnt++, f++; while (f <= b && cnt > 1) cnt--, b--; ans++; f++; } cout << ans; return 0; } |