#include <bits/stdc++.h> typedef long long ll; typedef long double ld; typedef unsigned long long ull; using namespace std; int main(){ cin.tie(0); ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> a(n); vector<int> b(n); for(int i = 0; i < n; i++){ cin >> a[i]; b[i]=a[i]; } sort(b.begin(), b.end()); map<int, int> m; for(int i = 0; i < n; i++) m[b[i]]=i; for(int i = 0; i < n; i++) a[i]=m[a[i]]; vector<vector<int> > ans; while(true){ // for(auto x: a) // cout << x << ' '; // cout << '\n'; vector<int> lefts; vector<int> rights; vector<bool> swapped(n, false); map<int, int> finds; for(int i = 0; i < n; i++){ finds[a[i]]=i; } for(int i = 0; i < n; i++){ if(a[i]==i) continue; if(swapped[i] || swapped[a[i]]) continue; lefts.push_back(i); rights.push_back(a[i]); swapped[i]=true; swapped[a[i]]=true; int j = i; int k = a[i]; //cout << j << " " << k << " " << a[k] << endl; while(a[k]!=j){ int pos = finds[j]; if (a[k]==pos){ swapped[pos]=true; break; } lefts.push_back(pos); rights.push_back(a[k]); swapped[pos]=true; swapped[a[k]]=true; j = pos; k = a[k]; } } for (int i = 0; i < lefts.size(); i++){ swap(a[lefts[i]], a[rights[i]]); } if(lefts.size()==0) break; vector<int> tmp; for(int i = 0; i < lefts.size(); i++){ tmp.push_back(lefts[i]); } // cout << endl; for(int i = rights.size()-1; i >= 0; i--){ tmp.push_back(rights[i]); } // for(auto x: tmp) // cout << x << ' '; // cout << endl; ans.push_back(tmp); } cout << ans.size() << endl; for(int i = 0; i < ans.size(); i++){ cout << ans[i].size() << "\n"; for(int j = 0; j < ans[i].size(); j++){ cout << ans[i][j]+1 << " "; } 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | #include <bits/stdc++.h> typedef long long ll; typedef long double ld; typedef unsigned long long ull; using namespace std; int main(){ cin.tie(0); ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> a(n); vector<int> b(n); for(int i = 0; i < n; i++){ cin >> a[i]; b[i]=a[i]; } sort(b.begin(), b.end()); map<int, int> m; for(int i = 0; i < n; i++) m[b[i]]=i; for(int i = 0; i < n; i++) a[i]=m[a[i]]; vector<vector<int> > ans; while(true){ // for(auto x: a) // cout << x << ' '; // cout << '\n'; vector<int> lefts; vector<int> rights; vector<bool> swapped(n, false); map<int, int> finds; for(int i = 0; i < n; i++){ finds[a[i]]=i; } for(int i = 0; i < n; i++){ if(a[i]==i) continue; if(swapped[i] || swapped[a[i]]) continue; lefts.push_back(i); rights.push_back(a[i]); swapped[i]=true; swapped[a[i]]=true; int j = i; int k = a[i]; //cout << j << " " << k << " " << a[k] << endl; while(a[k]!=j){ int pos = finds[j]; if (a[k]==pos){ swapped[pos]=true; break; } lefts.push_back(pos); rights.push_back(a[k]); swapped[pos]=true; swapped[a[k]]=true; j = pos; k = a[k]; } } for (int i = 0; i < lefts.size(); i++){ swap(a[lefts[i]], a[rights[i]]); } if(lefts.size()==0) break; vector<int> tmp; for(int i = 0; i < lefts.size(); i++){ tmp.push_back(lefts[i]); } // cout << endl; for(int i = rights.size()-1; i >= 0; i--){ tmp.push_back(rights[i]); } // for(auto x: tmp) // cout << x << ' '; // cout << endl; ans.push_back(tmp); } cout << ans.size() << endl; for(int i = 0; i < ans.size(); i++){ cout << ans[i].size() << "\n"; for(int j = 0; j < ans[i].size(); j++){ cout << ans[i][j]+1 << " "; } cout << "\n"; } } |