#include <iostream>
using namespace std;
const int M=3001;
int tab[M],sorted[M],positnow[M],positwyn[M],temp1[M],temp2[M];
bool used[3001];
void init()
{
for(int a=1;a<=3000;a++)positnow[a]=a,positwyn[a]=a;
}
void clean()
{
for(int a=1;a<=3000;a++)used[a]=false;
}
int main()
{
init();
clean();
int n,tem=1;
cin>>n;
for(int a=1;a<=n;a++)cin>>tab[a];
for(int a=1;a<=n;a++)
{
for(int b=1;b<n;b++)
{
if(tab[b]>tab[b+1])
{
swap(tab[b],tab[b+1]);
swap(positwyn[b],positwyn[b+1]);
}
}
}
//for(int a=1;a<=n;a++)cout<<positwyn[a]<<" ";
//cout<<endl;
int k=0;
bool check=false;
while(check==false)
{
check=true;
for(int a=1;a<=n;a++)
{
if(k+2>n)break;
if(positnow[a]!=positwyn[a])
{
for(;tem<=n;tem++)if(positnow[tem]==positwyn[a])break;
if(used[a]==false&&used[tem]==false)
{
used[a]=true;
used[tem]=true;
temp1[k/2]=positnow[a];
temp2[k/2]=positwyn[a];
k=k+2;
swap(positnow[a],positnow[tem]);
check=false;
}
tem=1;
}
}
if(check==false)
{
cout<<k<<endl;
k=k/2;
for(int a=0;a<k;a++)cout<<temp1[a]<<" ";
for(int a=k-1;a>=0;a--)cout<<temp2[a]<<" ";
cout<<endl;
k=0;
clean();
}
}
}
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 | #include <iostream> using namespace std; const int M=3001; int tab[M],sorted[M],positnow[M],positwyn[M],temp1[M],temp2[M]; bool used[3001]; void init() { for(int a=1;a<=3000;a++)positnow[a]=a,positwyn[a]=a; } void clean() { for(int a=1;a<=3000;a++)used[a]=false; } int main() { init(); clean(); int n,tem=1; cin>>n; for(int a=1;a<=n;a++)cin>>tab[a]; for(int a=1;a<=n;a++) { for(int b=1;b<n;b++) { if(tab[b]>tab[b+1]) { swap(tab[b],tab[b+1]); swap(positwyn[b],positwyn[b+1]); } } } //for(int a=1;a<=n;a++)cout<<positwyn[a]<<" "; //cout<<endl; int k=0; bool check=false; while(check==false) { check=true; for(int a=1;a<=n;a++) { if(k+2>n)break; if(positnow[a]!=positwyn[a]) { for(;tem<=n;tem++)if(positnow[tem]==positwyn[a])break; if(used[a]==false&&used[tem]==false) { used[a]=true; used[tem]=true; temp1[k/2]=positnow[a]; temp2[k/2]=positwyn[a]; k=k+2; swap(positnow[a],positnow[tem]); check=false; } tem=1; } } if(check==false) { cout<<k<<endl; k=k/2; for(int a=0;a<k;a++)cout<<temp1[a]<<" "; for(int a=k-1;a>=0;a--)cout<<temp2[a]<<" "; cout<<endl; k=0; clean(); } } } |
English