#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; #define debug(x) cerr<<#x<<": "<<x<<'\n'; constexpr int LIM = 5e5 + 7; int a[LIM]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { int x; scanf("%d", &x); ++a[x]; } sort(a + 1, a + n + 1, greater<int>()); int lo = 1, hi = n, counter = 1; while (lo < hi && a[lo] > 0) { int to_get = a[lo] - 1; while (to_get > 0 && lo < hi) { if (to_get >= a[hi]) { to_get -= a[hi]; --hi; } else { a[hi] -= to_get; to_get = 0; } } if (lo != hi) { ++counter; } ++lo; } printf("%d\n", counter); }
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 | #include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; #define debug(x) cerr<<#x<<": "<<x<<'\n'; constexpr int LIM = 5e5 + 7; int a[LIM]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { int x; scanf("%d", &x); ++a[x]; } sort(a + 1, a + n + 1, greater<int>()); int lo = 1, hi = n, counter = 1; while (lo < hi && a[lo] > 0) { int to_get = a[lo] - 1; while (to_get > 0 && lo < hi) { if (to_get >= a[hi]) { to_get -= a[hi]; --hi; } else { a[hi] -= to_get; to_get = 0; } } if (lo != hi) { ++counter; } ++lo; } printf("%d\n", counter); } |