-
题意:
你被给予了一个数组,包含 n n n个 [ 0 , n ] [0, n] [0,n]的整数。每一次操作,你可以选择数组中的一个元素替换成这个数组的 M E X MEX MEX。例如,如果数组是 [ 0 , 2 , 2 , 1 , 4 ] [0, 2, 2, 1, 4] [0,2,2,1,4],你可以选择第二个元素替换成整个数组元素的 M E X MEX MEX,数组会变成 [ 0 , 3 , 2 , 1 , 4 ] [0, 3, 2, 1, 4] [0,3,2,1,4]。 M E X MEX MEX的定义为不包含这个于这个数组的最小的非负整数。 -
分析:
每次计算当前的 M E X MEX MEX,如果 M E X = 0 MEX=0 MEX=0,则找任意一个 a [ i ] ≠ i a[i] \not =i a[i]=i 的 a [ i ] a [ i] a[i]替换为 M E X MEX MEX使得 M E X MEX MEX不为 0 0 0,然后重新找MEX,更新 a [ M E X ] = M E X a [ M E X ] = M E X a[MEX]=MEX;否则直接更新 a [ M E X ] = M E X a [ M E X ] = M EX a[MEX]=MEX。
显然,对于第 i i i次操作, M E X MEX MEX要么等于 0 0 0,要么等于 i i i,因此第 i i i次操作至多更新两次,这就保证了操作次数至多 2 n 2n 2n次。
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+10;
int a[maxn];
bool vis[maxn];
int n;
inline int f() {
for (int i = 0; i <= n; i++) {
vis[i] = false;
}
for (int i = 1; i <= n; i++) {
vis[a[i]] = true;
}
for (int i = 0; i <= n + 1; i++) {
if (!vis[i]) return i;
}
}
vector<int>res;
inline void solve() {
scanf("%d", &n);
res.clear();
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= n; i++) {
int mex = f();
if (!mex) {
bool flag = false;
for (int j = 1; j <= n; j++) {
if (j != a[j]) {
a[j] = 0;
res.push_back(j);
flag = true;
break;
}
}
if (!flag) break;
mex = f();
}
a[mex] = mex;
res.push_back(mex);
}
printf("%d\n", res.size());
for (int i = 0; i < res.size(); i++) {
printf("%d", res[i]);
if (i == res.size() - 1) puts(""); else printf(" ");
}
}
int main() {
int _;
scanf("%d", &_);
while (_--) {
solve();
}
return 0;
}