#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin>>n; vector<int>a(n); map<int, int>mapa; for (auto &i : a) { cin>>i; mapa[i]=0; } int sc=0; for (auto &i : mapa) i.second=sc++; vector<int>id(n); for (int i=0; i<n; i++) id[i]=i; for (auto &i : a) i=mapa[i]; //~ for (auto i : a) cout<<i+1<<" ";; cout<<"\n"; vector<deque<int>>ans; while (a!=id) { //~ for (auto i : a) cout<<i<<" "; //~ cout<<"\n"; deque<int>dq; vector<char>u(n); for (int i=0; i<n; i++) { if (a[i]!=i && !u[i]) { vector<int>pom={i}; u[i]=1; int akt=a[i]; while (akt!=i) { u[akt]=true; pom.push_back(akt); akt=a[akt]; } //~ cout<<" "; //~ for (auto j : pom) cout<<j<<" "; //~ cout<<"\n"; int s=pom.size(); for (int i=0; 2*i<s-1; i++) { dq.push_front(pom[i]+1); dq.push_back(pom[s-1-i]+1); swap(a[pom[i]], a[pom[s-1-i]]); } } } ans.push_back(dq); } cout<<ans.size()<<"\n"; for (auto i : ans) { cout<<i.size()<<"\n"; for (auto j : i) cout<<j<<" "; 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 | #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin>>n; vector<int>a(n); map<int, int>mapa; for (auto &i : a) { cin>>i; mapa[i]=0; } int sc=0; for (auto &i : mapa) i.second=sc++; vector<int>id(n); for (int i=0; i<n; i++) id[i]=i; for (auto &i : a) i=mapa[i]; //~ for (auto i : a) cout<<i+1<<" ";; cout<<"\n"; vector<deque<int>>ans; while (a!=id) { //~ for (auto i : a) cout<<i<<" "; //~ cout<<"\n"; deque<int>dq; vector<char>u(n); for (int i=0; i<n; i++) { if (a[i]!=i && !u[i]) { vector<int>pom={i}; u[i]=1; int akt=a[i]; while (akt!=i) { u[akt]=true; pom.push_back(akt); akt=a[akt]; } //~ cout<<" "; //~ for (auto j : pom) cout<<j<<" "; //~ cout<<"\n"; int s=pom.size(); for (int i=0; 2*i<s-1; i++) { dq.push_front(pom[i]+1); dq.push_back(pom[s-1-i]+1); swap(a[pom[i]], a[pom[s-1-i]]); } } } ans.push_back(dq); } cout<<ans.size()<<"\n"; for (auto i : ans) { cout<<i.size()<<"\n"; for (auto j : i) cout<<j<<" "; cout<<"\n"; } } |