题解:CF2157E Adjusting Drones
WilliamFranklin · · 题解
Solution
首先考虑若操作次数
考虑对于一个
考虑对于数的相对顺序是没有用的,直接统计个数即可。那么按值域从大到小考虑,相当于将
用并查集维护下一个
总时间复杂度
:::success[AC Code]
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define mp(Tx, Ty) make_pair(Tx, Ty)
#define For(Ti, Ta, Tb) for(auto Ti = (Ta); Ti <= (Tb); Ti++)
#define Dec(Ti, Ta, Tb) for(auto Ti = (Ta); Ti >= (Tb); Ti--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
const int N = 2e5 + 5;
int n, k, a[N];
int fa[N * 6];
int cnt[N * 6];
int find(int x) {
if (fa[x] == x) return x;
fa[x] = find(fa[x]);
return fa[x];
}
void U(int x, int y) {
fa[find(x)] = find(y);
}
bool check(int x) {
For(i, 1, n * 6 + 1) fa[i] = i;
For(i, 1, n * 6 + 1) cnt[i] = 0;
For(i, 1, n) cnt[a[i]]++;
For(i, 1, n * 6 + 1) if (cnt[i]) U(i, i + 1);
Dec(i, n * 2, 1) {
if (cnt[i] <= 1) continue;
int X = cnt[i] - 1, now = find(i);
while (X && now <= i + x) {
cnt[now] = 1;
X--;
U(now, now + 1);
now = find(now);
}
cnt[i] = 1;
if (X) {
if (cnt[i + x] == 0) U(i + x, i + x + 1);
cnt[i + x] += X;
}
}
For(i, 1, n * 6) if (cnt[i] > k) return 0;
return 1;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int T = 1;
cin >> T;
while (T--) {
cin >> n >> k;
For(i, 1, n) cin >> a[i];
int l = 0, r = 3 * n, ans = -1;
while (l <= r) {
int mid = l + r >> 1;
if (check(mid)) {
ans = mid;
r = mid - 1;
} else l = mid + 1;
}
cout << ans << '\n';
}
return 0;
}
:::