P4447
作者:
fyygfuy
,
2020-09-14 19:38:33
,
所有人可见
,
阅读 506
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 10;
int n, tot;
int a[N], st[N], b[N];
map<int, int> M;
bool check(int len) {
for (int i = 1; i <= tot; i++) {
b[i] = st[i] - st[i - 1];
}
for (int i = 1; i <= tot; i++) {
if (st[i] > st[i - 1]) {
if (i + len - 1 > tot) return false;
int d = st[i] - st[i - 1];
b[i] -= d, b[i + len] += d;
}
}
for (int i = 1; i <= tot; i++) {
b[i] += b[i - 1];
if (b[i] < 0) return false;
}
return true;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + 1 + n);
a[0] = 1e9 + 7;
for (int i = 1; i <= n; i++) {
if (a[i] != a[i - 1]) {
tot++;
if (a[i] - a[i - 1] > 1) tot++;
M[a[i]] = tot;
}
st[M[a[i]]]++;
}
int l = 1, r = tot;
while (l < r) {
int mid = l + r + 1 >> 1;
if (check(mid)) l = mid;
else r = mid - 1;
}
cout << l << endl;
return 0;
}