rt,CF 上看数据发现 n 只有 100000,k 只有 262,复杂度是 nlogn 的,不知道为啥 TLE。
并且不是多测的原因,因为 T 只有 1。
不求调代码,但是有好心人帮吗看看这个复杂度哪里不对吗/kk,谢谢了
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
const int N = 10010;
const int INF = 1e9;
int T;
int a[N];
pair<int, int> p[N];
unordered_map<int, int> m;
int cnt = 0;
void init(int &sum, int &x, int n) {
x = -INF;
if (a[1]) x = 0;
else {
for (int i = 2; i <= n; i++)
if (a[i] - a[i - 1] >= 2) {
x = a[i - 1] + 1;
break;
}
}
if (x == -INF) x = a[n] + 1;
sum = 1;
for (int i = 2; i <= n; i++)
if (a[i] != a[i - 1]) sum++;
sum -= x;
}
int main() {
cin >> T;
while (T--) {
for(int i = 1; i <= cnt; i++) p[i] = make_pair(0, 0);
int n, k; cin >> n >> k, cnt = 0;
m.clear();
for (int i = 1; i <= n; i++) cin >> a[i], m[a[i]]++;
sort(a + 1, a + 1 + n);
if (a[1]) p[++cnt] = make_pair(0, a[1] - 1);
for (int i = 2; i <= n; i++)
if (a[i] - a[i - 1] >= 2) p[++cnt] = make_pair(a[i - 1] + 1, a[i] - 1);
p[++cnt] = make_pair(a[n] + 1, INF);
int res = 1, ans, now_mex, x = 0;
init(ans, now_mex, n);
for (int i = 1; i <= n; i++) {
if (x > k || res > cnt) break;
if (m[a[i]] > 2) {
x++, m[a[i]]--, a[i] = p[res].first;
p[res].first++;
if (p[res].first > p[res].second) res++;
now_mex = p[res].first;
ans = ans - p[res].first + now_mex;
}
}
int i = n;
while (x < k) {
if (a[i] < now_mex || res > cnt) break;
x++, m[a[i]]--, a[i] = p[res].first, i--;
p[res].first++;
if (p[res].first > p[res].second) res++;
ans = ans - p[res].first + now_mex;
now_mex = p[res].first;
}
cout << ans << endl;
}
return 0;
}