显示原始代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int n, a[N];
bool cmp(int x, int y) { return x < y; }
int ans, tot[N];
int zh(int x) {
int f[x + 10], l = 1, r = 0, q[x];
for (int i = 0; i <= x; i++) f[x] = 0;
f[0] = tot[0] - 1;
for (int i = 1; i <= x; i++) {
f[i] = (tot[0] - 1) * (i + 1);
if (l > r or tot[q[r]] > tot[i]) {
r++;
q[r] = i;
}
for (int j = l; j <= r; j++) f[i] = min(f[i], f[q[j] - 1] + (tot[q[j]] - 1) * (i + 1) + q[j]);
}
return f[x];
}
void A() {
scanf("%lld", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
sort(a + 1, a + n + 1, cmp);
if (a[1] > 0) {
printf("0\n");
return;
}
if (a[n] == 0) {
printf("%lld\n", n - 1);
return;
}
ans = 0;
int sl = 1;
for (int i = 0; a[sl] == i; i++) {
tot[i] = 0;
while (a[sl] == i and sl <= n) {
tot[i]++;
sl++;
}
if (a[sl] != i + 1 or sl > n) {
printf("%lld\n", zh(i));
return;
}
}
}
signed main() {
int t;
scanf("%lld", &t);
while (t--) A();
return 0;
}