提交时间:2024-11-03 14:44:02
运行 ID: 34081
#include <bits/stdc++.h> #define pb emplace_back using namespace std; int n, l, a[2000005], pos[2000005]; struct opt { int i, j, k; }; vector <opt> op; inline void swp(int x, int y) { // printf("swp %lld %lld\n", x, y); pos[a[x]] = y, pos[a[y]] = x; swap(a[x], a[y]); } // #define getchar getchar_unlocked inline int read() { int x = 0; bool fl = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') fl = 1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return fl ? -x : x; } signed main() { freopen("sort.in", "r", stdin); freopen("sort.out", "w", stdout); // ios::sync_with_stdio(0); // cin.tie(0); // cout.tie(0); int _; cin >> _; while (_--) { op.clear(); cin >> n >> l; // cerr << n << endl; int ps = -1; for (int i = 1; i <= n; i++) { cin >> a[i]; pos[a[i]] = i; if (a[i] == 1) ps = i; } // cout << n << ' ' << l << endl; // for (int i = 1; i <= n; i++) // { // cout << a[i] << " "; // } // cout << endl; // cerr << clock() * 1.0 / CLOCKS_PER_SEC << endl; int lm = -1, mp = -1; for (int i = 1; i < ps; i++) { if (a[i] > lm) mp = i; lm = max(lm, a[i]); } int rm = 0x3f3f3f3f, rmp = -1; for (int i = ps + 1; i <= n; i++) { if (a[i] < rm) rmp = i; rm = min(rm, a[i]); } // cerr << clock() * 1.0 / CLOCKS_PER_SEC << endl; if (ps != 1 && rm > lm) { puts("-1"); continue; } // printf("mp %lld rmp %lld\n", mp, rmp); if (ps != 1) { if (mp != 1) { op.pb((opt){1, mp, ps}); swp(1, mp); } op.pb((opt){1, ps, rmp}); swp(1, ps); } // cerr << clock() * 1.0 / CLOCKS_PER_SEC << endl; for (int i = 2; i < n; i++) { // if (pos[i] < i) // cerr << "YES" << endl; if (a[i] != i) { op.pb((opt){1, i, pos[i]}); swp(i, pos[i]); } } // cerr << clock() * 1.0 / CLOCKS_PER_SEC << endl; // cerr << "g" << endl; cout << op.size() << '\n'; int cnt = 0; for (opt x : op) { // printf("%d %d %d\n", op.front().i, op.front().j, op.front().k); cout << x.i << ' ' << x.j << ' ' << x.k << '\n'; } // cerr << clock() * 1.0 / CLOCKS_PER_SEC << endl; // cerr << cnt << endl; // cerr << clock() * 1.0 / CLOCKS_PER_SEC << endl; // cerr << "end" << endl; } cout.flush(); return 0; }