提交时间:2024-11-06 11:37:57

运行 ID: 34353

#include <bits/stdc++.h> #define int long long #define mt(i, j, k) make_pair(i, make_pair(j, k)) #define fir first #define sec second.first #define thi second.second #define tup pair<int, pair<int, int>> #define ns \ { \ no_sol(); \ continue; \ } #define ed \ { \ eend(); \ continue; \ } using namespace std; int _, n, l, a[2000005], pos[2000005]; vector <tup> op; inline void no_sol() { puts("-1"); } inline void eend() { if (op.size() > l) { puts("-1"); return ; } printf("%ld\n", op.size()); for (auto x : op) printf("%lld %lld %lld\n", x.fir, x.sec, x.thi); } inline void swp(int x, int y, int z) { op.emplace_back(mt(x, y, z)); if (a[x] < a[z]) { swap(a[y], a[z]); swap(pos[a[y]], pos[a[z]]); } else { swap(a[x], a[y]); swap(pos[a[x]], pos[a[y]]); } } inline void dop() { for (int i = 1; i <= n; i++) if (a[i] != i) swp(1, i, pos[i]); } signed main() { scanf("%lld", &_); while (_--) { op.clear(); scanf("%lld %lld", &n, &l); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); pos[a[i]] = i; } // Case 1 n<=3 begin if (n == 1) ed; if (pos[1] == n) // Case 2 pos[1] == n ns; if (n == 2) ed; if (pos[1] == 1) // Case 3 pos[1] == 1 { dop(); ed; } if (n == 3) { if (a[1] != 2) { swp(1, 2, 3), swp(1, 2, 3); ed; } else ns; } // Case 1 end; // Case 4 Exist pos[1]<i<=n a[i]<a[1] bool b = 0; for (int i = pos[1] + 1; i <= n; i++) { if (a[i] < a[1]) { swp(1, pos[1], i); dop(); b = 1; eend(); break; } } if (b) continue; int tmp = pos[1]; // Case 5 a[1] >= 3 if (a[1] >= 3) { for (int i = 2; i < tmp; i++) { if (a[i] < a[1]) { swp(1, i, n); swp(1, tmp, n); dop(); b = 1; eend(); break; } } } if (b) continue; if (a[2] == 1) { for (int i = 3; i < n; i++) // Case 7 { if (a[i] > a[i + 1]) { swp(1, 2, i); swp(1, 2, i); swp(1, i, i + 1); dop(); b = 1; eend(); break; } } if (b) continue; swp(1, 2, 3); // Case 6 all increase except 1 2 swp(1, 2, 3); swp(1, 2, 4); swp(1, 3, 4); swp(1, 2, 4); ed; } if (pos[n] > 2) //Case 8 swp(1, 2, pos[n]); swp(1, 2, tmp); swp(1, tmp, n); dop(); ed; } return 0; }