#include <bits/stdc++.h> using namespace std; #define s second #define f first const int MAX_N = 3e3+7; int n, a, l, b, c, tab[MAX_N], tab2[MAX_N], ind[MAX_N], vis2[MAX_N][MAX_N], vis[MAX_N], poz[MAX_N]; vector<pair<int, int>> vec, w[MAX_N]; vector<int> kol; int f(int a){ while (a!=tab2[a]) a = tab2[a]; return a; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; for (int i = 0; i < n; i++) { cin>>a; vec.push_back({a, i+1}); ind[a]=i+1; tab2[i+1] = i+1; } sort(vec.begin(), vec.end()); for (int i = 0; i < n; i++) tab[i+1] = ind[vec[i].f]; for (int i = 0; i <= n; i++) poz[tab[i+1]] = tab2[i+1]; kol.push_back(0); for (int i = 1; i <= n; i++) { kol.push_back(tab[i]); } b=0; while (true) { a=0; for (int i = 1; i <= n; i++) { c = f(kol[i]); if (!vis[i] && !vis2[b][i] && !vis2[b][c] && i!=c) { vis[i]=1; poz[c] = poz[i]; tab[c] = poz[i]; tab[i] = i; poz[i] = i; vis2[b][i]=1; vis2[b][c]=1; tab2[i]=c; w[b].push_back({i, c}); a++; } } if (a==0) break; b++; } cout<<b<<"\n"; for (int i = 0; i < b; i++) { cout<<w[i].size()*2<<"\n"; for (int j = 0; j < w[i].size(); j++) { cout<<w[i][j].f<<" "; } for (int j = w[i].size()-1; j >= 0; j--) { cout<<w[i][j].s<<" "; } cout<<"\n"; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | #include <bits/stdc++.h> using namespace std; #define s second #define f first const int MAX_N = 3e3+7; int n, a, l, b, c, tab[MAX_N], tab2[MAX_N], ind[MAX_N], vis2[MAX_N][MAX_N], vis[MAX_N], poz[MAX_N]; vector<pair<int, int>> vec, w[MAX_N]; vector<int> kol; int f(int a){ while (a!=tab2[a]) a = tab2[a]; return a; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; for (int i = 0; i < n; i++) { cin>>a; vec.push_back({a, i+1}); ind[a]=i+1; tab2[i+1] = i+1; } sort(vec.begin(), vec.end()); for (int i = 0; i < n; i++) tab[i+1] = ind[vec[i].f]; for (int i = 0; i <= n; i++) poz[tab[i+1]] = tab2[i+1]; kol.push_back(0); for (int i = 1; i <= n; i++) { kol.push_back(tab[i]); } b=0; while (true) { a=0; for (int i = 1; i <= n; i++) { c = f(kol[i]); if (!vis[i] && !vis2[b][i] && !vis2[b][c] && i!=c) { vis[i]=1; poz[c] = poz[i]; tab[c] = poz[i]; tab[i] = i; poz[i] = i; vis2[b][i]=1; vis2[b][c]=1; tab2[i]=c; w[b].push_back({i, c}); a++; } } if (a==0) break; b++; } cout<<b<<"\n"; for (int i = 0; i < b; i++) { cout<<w[i].size()*2<<"\n"; for (int j = 0; j < w[i].size(); j++) { cout<<w[i][j].f<<" "; } for (int j = w[i].size()-1; j >= 0; j--) { cout<<w[i][j].s<<" "; } cout<<"\n"; } } |