CF1375D题解
upd on 2024.12.26 被撤了,添加证明,修改代码。
Link
一个序列的
我们认为序列
考虑将原序列
将每一步分情况考虑:
-
若当前
\operatorname{mex}<n ,显然只需要将a_{\operatorname{mex}} 上的数改为\operatorname{mex} 即可。 -
若当前
\operatorname{mex}=n ,我们可以将任意一个(代码中是最小的一个)a_i \ne i 的位置上的数改为\operatorname{mex} 。显然此操作后的\operatorname{mex}<n ,按\operatorname{mex}<n 的情况进行操作即可。 -
由
\operatorname{mex} 的定义,没有\operatorname{mex}>n 的情况。
对于每一步:第一种情况
@AzureHair 提示您:多测不清空,退役两行泪。
代码:
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n;
int a[2020];
int ans[2020];
int tot[2020];
int getmex() {
for (int i = 0; i <= 1010; i++)
tot[i] = 0;
for (int i = 1; i <= n; i++)
tot[a[i]] = 1;
for (int i = 0; i <= 1010; i++)
if (tot[i] == 0)
return i;
}
int find() {
for (int i = 1; i <= n; i++)
if (a[i] != i - 1)
return i;
return -1;
}
signed main() {
int t;
cin >> t;
while (t--) {
int now = 0;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; now <= 2 * n; i++) {
int m = getmex();
if (m < n) {
ans[++now] = m + 1;
a[m + 1] = m;
}
else {
int f = find();
if (f == -1)
break;
a[f] = m;
ans[++now] = f;
}
}
cout << now << endl;
for (int i = 1; i <= now; i++)
cout << ans[i] << " ";
cout << endl;
for (int i = 1; i <= n; i++)
a[i] = 0;
for (int i = 1; i <= now; i++)
ans[i] = 0;
}
return 0;
}