#include <bits/stdc++.h>
using namespace std;
int n,m,a;
int ile;
int t[3005];
bool c;
vector <int> poc;
vector <int> kon;
vector <bool> odw;
map <int,int> e;
map <int,int>::iterator it;
int main()
{
std::ios_base::sync_with_stdio(0);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>m;
e[m]=i;
}
m=0;
ile=1;
for(it=e.begin();it!=e.end();it++)
{
t[it->second]=ile;
ile++;
}
ile=0;
odw.resize(n+2,false);
for(int i=1;i<=n;i++)
{
while(!odw[i])
{
ile++;
odw[i]=true;
i=t[i];
}
m=max(m,ile);
ile=0;
}
int g=m;
int h;
while(g!=1)
{
ile++;
h=g/2;
g-=h;
}
cout<<ile<<endl;
while(ile)
{
odw.resize(0);
odw.resize(n+2,false);
for(int i=1;i<=n;i++)
{
c=false;
while(!odw[i])
{
g=i;
i=t[i];
if(!c)
{
c=true;
a=g;
}
else
{
poc.push_back(a);
kon.push_back(g);
swap(t[a],t[g]);
c=false;
}
odw[g]=true;
}
}
cout<<poc.size()*2<<endl;
for(int f=0;f<poc.size();f++)
{
cout<<poc[f]<<" ";
}
for(int f=kon.size()-1;f>=0;f--)
{
cout<<kon[f]<<" ";
}
cout<<endl;
poc.clear();
kon.clear();
ile--;
}
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 92 | #include <bits/stdc++.h> using namespace std; int n,m,a; int ile; int t[3005]; bool c; vector <int> poc; vector <int> kon; vector <bool> odw; map <int,int> e; map <int,int>::iterator it; int main() { std::ios_base::sync_with_stdio(0); cin>>n; for(int i=1;i<=n;i++) { cin>>m; e[m]=i; } m=0; ile=1; for(it=e.begin();it!=e.end();it++) { t[it->second]=ile; ile++; } ile=0; odw.resize(n+2,false); for(int i=1;i<=n;i++) { while(!odw[i]) { ile++; odw[i]=true; i=t[i]; } m=max(m,ile); ile=0; } int g=m; int h; while(g!=1) { ile++; h=g/2; g-=h; } cout<<ile<<endl; while(ile) { odw.resize(0); odw.resize(n+2,false); for(int i=1;i<=n;i++) { c=false; while(!odw[i]) { g=i; i=t[i]; if(!c) { c=true; a=g; } else { poc.push_back(a); kon.push_back(g); swap(t[a],t[g]); c=false; } odw[g]=true; } } cout<<poc.size()*2<<endl; for(int f=0;f<poc.size();f++) { cout<<poc[f]<<" "; } for(int f=kon.size()-1;f>=0;f--) { cout<<kon[f]<<" "; } cout<<endl; poc.clear(); kon.clear(); ile--; } return 0; } |
English