#include <iostream> #include <algorithm> using namespace std; int n,i,j,tab[3001],tab1[3001],tmp[3001],tmpr[3001],res1[3001],res2[3001],rind,rind2,maxpath,currpath,wyn,pow2,first,firstind,tmpx; void qs( int tab[],int tab2[], int left, int right ) { int i = left; int j = right; int x = tab[( left + right ) / 2 ]; do { while( tab[ i ] < x ) i++; while( tab[ j ] > x ) j--; if( i <= j ) { swap( tab[ i ], tab[ j ] ); swap( tab2[ i ], tab2[ j ] ); i++; j--; } } while( i <= j ); if( left < j ) qs( tab,tab2, left, j ); if( right > i ) qs( tab,tab2, i, right ); } int main(){ scanf("%d",&n); for(i=0;i<n;i++){ scanf("%d",&tab[i]); tmp[i]=tab[i]; tmpr[i]=i; } qs(tmp,tmpr,0,n-1); for(i=0;i<n;i++){ tab[tmpr[i]]=i; } for(i=0;i<n;i++){ // printf("%d ",tab[i]); tmp[i]=false; } maxpath=0; wyn=0; pow2=1; // policz wynik for(i=0;i<n;i++){ currpath=0; j=i; while(!tmp[j]){ tmp[j]=true; j=tab[j]; currpath++; } if(currpath>maxpath)maxpath=currpath; } // printf("maxp %d\n",maxpath); if(maxpath>2){ printf("%d\n",2); } else if(maxpath==2){ printf("%d\n",1); } else{ printf("%d\n",0); return 0; } // liczswapy // while(wyn--){ if(maxpath>2){ for(i=0;i<n;i++) tmp[i]=false; rind=rind2=0; for(i=0;i<n;i++){ currpath=0; j=i; rind2=rind; while(!tmp[j]){ tmp[j]=true; res1[rind2++]=tab[j]; j=tab[j]; currpath++; } if(currpath>2){ while(rind2>rind){ res2[rind++]=res1[--rind2]; } rind=rind2; } } printf("%d\n",rind*2); for(i=0;i<rind;i++){ printf("%d ",res1[i]+1); } for(i=rind-1;i>0;i--){ printf("%d ",res2[i]+1); } printf("%d\n",res2[0]+1); for(i=0;i<rind;i++){ tmpx = tab[res1[i]]; tab[res1[i]]=tab[res2[i]]; tab[res2[i]]=tmpx; } // print } if(true){ rind=0; for(i=0;i<n;i++) tmp[i]=false; for(i=0;i<n;i++){ j=i; first = -1; while(!tmp[j]){ tmp[j]=true; if(first == -1){ first = tab[j]; firstind = j; } else{ tab1[firstind] = tab[j]; tab1[j]=first; first = -1; res1[rind]=firstind; res2[rind++]=j; } j=tab[j]; } if(first != -1){ tab1[firstind]=first; } } for(i=0;i<n;i++){ tab[i]=tab1[i]; } printf("%d\n",rind*2); for(i=0;i<rind;i++){ printf("%d ",res1[i]+1); } for(i=rind-1;i>0;i--){ printf("%d ",res2[i]+1); } printf("%d\n",res2[0]+1); // for(i=0;i<n;i++){ // printf("%d ",tab[i]); //} printf("\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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | #include <iostream> #include <algorithm> using namespace std; int n,i,j,tab[3001],tab1[3001],tmp[3001],tmpr[3001],res1[3001],res2[3001],rind,rind2,maxpath,currpath,wyn,pow2,first,firstind,tmpx; void qs( int tab[],int tab2[], int left, int right ) { int i = left; int j = right; int x = tab[( left + right ) / 2 ]; do { while( tab[ i ] < x ) i++; while( tab[ j ] > x ) j--; if( i <= j ) { swap( tab[ i ], tab[ j ] ); swap( tab2[ i ], tab2[ j ] ); i++; j--; } } while( i <= j ); if( left < j ) qs( tab,tab2, left, j ); if( right > i ) qs( tab,tab2, i, right ); } int main(){ scanf("%d",&n); for(i=0;i<n;i++){ scanf("%d",&tab[i]); tmp[i]=tab[i]; tmpr[i]=i; } qs(tmp,tmpr,0,n-1); for(i=0;i<n;i++){ tab[tmpr[i]]=i; } for(i=0;i<n;i++){ // printf("%d ",tab[i]); tmp[i]=false; } maxpath=0; wyn=0; pow2=1; // policz wynik for(i=0;i<n;i++){ currpath=0; j=i; while(!tmp[j]){ tmp[j]=true; j=tab[j]; currpath++; } if(currpath>maxpath)maxpath=currpath; } // printf("maxp %d\n",maxpath); if(maxpath>2){ printf("%d\n",2); } else if(maxpath==2){ printf("%d\n",1); } else{ printf("%d\n",0); return 0; } // liczswapy // while(wyn--){ if(maxpath>2){ for(i=0;i<n;i++) tmp[i]=false; rind=rind2=0; for(i=0;i<n;i++){ currpath=0; j=i; rind2=rind; while(!tmp[j]){ tmp[j]=true; res1[rind2++]=tab[j]; j=tab[j]; currpath++; } if(currpath>2){ while(rind2>rind){ res2[rind++]=res1[--rind2]; } rind=rind2; } } printf("%d\n",rind*2); for(i=0;i<rind;i++){ printf("%d ",res1[i]+1); } for(i=rind-1;i>0;i--){ printf("%d ",res2[i]+1); } printf("%d\n",res2[0]+1); for(i=0;i<rind;i++){ tmpx = tab[res1[i]]; tab[res1[i]]=tab[res2[i]]; tab[res2[i]]=tmpx; } // print } if(true){ rind=0; for(i=0;i<n;i++) tmp[i]=false; for(i=0;i<n;i++){ j=i; first = -1; while(!tmp[j]){ tmp[j]=true; if(first == -1){ first = tab[j]; firstind = j; } else{ tab1[firstind] = tab[j]; tab1[j]=first; first = -1; res1[rind]=firstind; res2[rind++]=j; } j=tab[j]; } if(first != -1){ tab1[firstind]=first; } } for(i=0;i<n;i++){ tab[i]=tab1[i]; } printf("%d\n",rind*2); for(i=0;i<rind;i++){ printf("%d ",res1[i]+1); } for(i=rind-1;i>0;i--){ printf("%d ",res2[i]+1); } printf("%d\n",res2[0]+1); // for(i=0;i<n;i++){ // printf("%d ",tab[i]); //} printf("\n"); } // } } |