#include<bits/stdc++.h>
using namespace std;
int tab[1000005];
bool odw[1000005];
vector<vector<int>> res;
int cnt = 0;
void przenumeruj(int a)
{
vector<int> v;
for(int x=1;x<=a;x++)
v.push_back(tab[x]);
sort(v.begin(), v.end());
for(int x=1;x<=a;x++)
tab[x] = (lower_bound(v.begin(), v.end(), tab[x]) - v.begin()) + 1;
}
bool sorted(int a)
{
for(int x=1;x<=a;x++)
if(tab[x] != x)
return false;
return true;
}
void przestaw(int a)
{
/*cout<<"XD"<<endl;
for(int x=1;x<=a;x++)
cout<<tab[x]<<" ";
cout<<endl;
if(cnt == 5)
exit(0);
cnt++;*/
vector<int> v1, v2;
for(int i=1;i<=a;i++)
{
if(odw[i])
continue;
vector<int> v = {i};
int pom = tab[i];
odw[i] = true;
while(!odw[pom])
{
v.push_back(pom);
odw[pom] = true;
pom = tab[pom];
}
if(v.size() == 2)
{
v1.push_back(v[0]);
v2.push_back(v[1]);
}
else if(v.size() >= 3)
{
for(int i=0; i<(v.size() - 1) / 2; i++)
{
v1.push_back(v[i]);
v2.push_back(v[v.size() - i - 2]);
}
}
//cout<<v.size()<<'\n';
}
for(int x=1;x<=a;x++)
odw[x] = false;
vector<int> v;
for(int i=0;i<v1.size();i++)
swap(tab[v1[i]], tab[v2[i]]);
for(auto x : v1)
v.push_back(x);
reverse(v2.begin(), v2.end());
for(auto x : v2)
v.push_back(x);
res.push_back(v);
}
int main()
{
int a;
cin>>a;
for(int x=1;x<=a;x++)
cin>>tab[x];
przenumeruj(a);
while(!sorted(a))
przestaw(a);
cout<<res.size()<<'\n';
for(auto v : res)
{
cout<<v.size()<<'\n';
for(auto x : v)
cout<<x<<" ";
cout<<'\n';
}
return 0;
}
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 | #include<bits/stdc++.h> using namespace std; int tab[1000005]; bool odw[1000005]; vector<vector<int>> res; int cnt = 0; void przenumeruj(int a) { vector<int> v; for(int x=1;x<=a;x++) v.push_back(tab[x]); sort(v.begin(), v.end()); for(int x=1;x<=a;x++) tab[x] = (lower_bound(v.begin(), v.end(), tab[x]) - v.begin()) + 1; } bool sorted(int a) { for(int x=1;x<=a;x++) if(tab[x] != x) return false; return true; } void przestaw(int a) { /*cout<<"XD"<<endl; for(int x=1;x<=a;x++) cout<<tab[x]<<" "; cout<<endl; if(cnt == 5) exit(0); cnt++;*/ vector<int> v1, v2; for(int i=1;i<=a;i++) { if(odw[i]) continue; vector<int> v = {i}; int pom = tab[i]; odw[i] = true; while(!odw[pom]) { v.push_back(pom); odw[pom] = true; pom = tab[pom]; } if(v.size() == 2) { v1.push_back(v[0]); v2.push_back(v[1]); } else if(v.size() >= 3) { for(int i=0; i<(v.size() - 1) / 2; i++) { v1.push_back(v[i]); v2.push_back(v[v.size() - i - 2]); } } //cout<<v.size()<<'\n'; } for(int x=1;x<=a;x++) odw[x] = false; vector<int> v; for(int i=0;i<v1.size();i++) swap(tab[v1[i]], tab[v2[i]]); for(auto x : v1) v.push_back(x); reverse(v2.begin(), v2.end()); for(auto x : v2) v.push_back(x); res.push_back(v); } int main() { int a; cin>>a; for(int x=1;x<=a;x++) cin>>tab[x]; przenumeruj(a); while(!sorted(a)) przestaw(a); cout<<res.size()<<'\n'; for(auto v : res) { cout<<v.size()<<'\n'; for(auto x : v) cout<<x<<" "; cout<<'\n'; } return 0; } |
English