调了好久,搞不出来。请问什么原因
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
int n, m, K, a[N], ans[N << 1][3], s[N], v[N][2], stk[N][3];
bool inq[N];
queue<int> q;
int sl(int x) {
return (x & 1) ? x + 1 : x - 1;
}
inline void psh(int x) {
if (!inq[x]) q.push(x), inq[x] = 1;
}
int main() {
// freopen("data.in", "r", stdin);
// freopen("data.out", "w", stdout);
int T;
cin >> T;
while (T--) {//cout << "!!";
while (q.size()) q.pop();
cin >> n >> m >> K;
int fuzhu = n, tot = 0;
for (int i = 1; i <= m; ++i) scanf("%d", a + i), v[a[i]][0] = v[a[i]][1] = 0;
for (int i = 1; i <= n - 1; ++i)
q.push(i), inq[i] = 1, s[i] = 0, stk[i][0] = stk[i][1] = stk[i][2] = 0;
inq[fuzhu] = 0;
for (int i = 1; i <= m; ++i) {//cout << i << endl;
if (v[a[i]][1]) {
int now = v[a[i]][1];
stk[now][2] = 0;
ans[++tot][0] = 1, ans[tot][1] = now;
v[a[i]][1] = 0;
--s[now];
psh(now);
continue;
}
if (v[a[i]][0]) {
int now = v[a[i]][0], x = stk[now][2];
v[a[i]][0] = 0, v[x][0] = now, v[x][1] = 0;
stk[now][1] = stk[now][2], stk[now][2] = 0;
ans[++tot][0] = 1, ans[tot][1] = fuzhu;
ans[++tot][0] = 2, ans[tot][1] = now, ans[tot][2] = fuzhu;
--s[now];
psh(now);
continue;
}
if (q.size()) {
int now = q.front();
v[a[i]][s[now]] = now;
++s[now];
stk[now][s[now]] = a[i];
ans[++tot][0] = 1, ans[tot][1] = now;
if (s[now] == 2) q.pop(), inq[now] = 0;
continue;
}
int r = i;
while (r + 1 <= m && v[a[r + 1]][1]) ++r;
//if (r == m) cout << T << ' ' << r << ' ' << n << ' ' << m << ' ' << K << endl, assert(0);
int id = r + 1, cnt = 0;
if (a[id] == a[i]) {
ans[++tot][0] = 1, ans[tot][1] = fuzhu;
for (int j = i + 1; j <= r; ++j) {
if (v[a[j]][1]) {
int now = v[a[j]][1];
ans[++tot][0] = 1, ans[tot][1] = now;
--s[now];
stk[now][2] = 0;
continue;
}
}
for (int j = i + 1; j <= r; ++j) {
int now = v[a[j]][1] | v[a[j]][0];
if (!v[a[j]][1] || !v[a[j]][0]) psh(now);
}
ans[++tot][0] = 1, ans[tot][1] = fuzhu;
} else {
int cnt = 0, ss = v[a[id]][0], x = stk[ss][2];
for (int j = i + 1; j <= id; ++j) cnt += a[j] == x;
if (cnt & 1) {
ans[++tot][0] = 1, ans[tot][1] = fuzhu;
for (int j = i + 1; j <= r; ++j) {
if (v[a[j]][1]) {
int now = v[a[j]][1];
ans[++tot][0] = 1, ans[tot][1] = now;
--s[now];
stk[now][2] = 0;
continue;
}
}
psh(fuzhu);
stk[fuzhu][1] = a[i], s[fuzhu] = 1, v[a[i]][0] = fuzhu;
fuzhu = ss, s[ss] = 0, stk[ss][1] = stk[ss][2] = 0;
ans[++tot][0] = 1, ans[tot][1] = ss;
for (int j = i + 1; j <= r; ++j) {
int now = v[a[j]][1] | v[a[j]][0];
if (now != fuzhu && (!v[a[j]][1] || !v[a[j]][0])) psh(now);
}
} else {
ans[++tot][0] = 1, ans[tot][1] = ss;
for (int j = i + 1; j <= r; ++j) {
if (a[j] == x) {
ans[++tot][0] = 1, ans[tot][1] = ss;
continue;
}
if (v[a[j]][1]) {
int now = v[a[j]][1];
ans[++tot][0] = 1, ans[tot][1] = now;
--s[now];
stk[now][2] = 0;
continue;
}
}
ans[++tot][0] = 1, ans[tot][1] = fuzhu;
ans[++tot][0] = 2, ans[tot][1] = ss, ans[tot][2] = fuzhu;
v[stk[ss][2]][0] = ss, v[stk[ss][1]][0] = v[stk[ss][2]][1] = 0;
v[a[i]][1] = ss;
stk[ss][1] = stk[ss][2], stk[ss][2] = a[i];
}
for (int j = i + 1; j <= r; ++j) {
int now = v[a[j]][1] | v[a[j]][0];
if (now != ss && now != fuzhu && (!v[a[j]][1] || !v[a[j]][0])) psh(now);
}
}
i = id;
}
printf("%d\n", tot);
for (int i = 1; i <= tot; ++i) {
printf("%d %d ", ans[i][0], ans[i][1]);
if (ans[i][0] == 2) printf("%d", ans[i][2]);
puts("");
}
}
}
具体思路:参考的第二篇题解