#include <bits/stdc++.h>#defineREP (I,A,B) for (int i=a;i<=b;i++)#defineMS0 (a) memset (A,0,sizeof (a))using namespaceStd;typedefLong Longll;Const intmaxn=1000100;Const intinf=1<< in;intN,A[MAXN];intA1[MAXN],ID1[MAXN];intA2[MAXN],ID2[MAXN];intSolveintMint*a,int*ID) { if(m==0)return 0; if(a[m]==m)returnSolve (M-1, A,id); intx=Id[m]; Swap (a[x],a[m]); Swap (id[a[x]],id[a[m]]); return 1+solve (M-1, A,id);}intMain () {Freopen ("In.txt","R", stdin); while(cin>>n,n) {MS0 (ID1); MS0 (ID2); intst=1; REP (i,1, N) scanf ("%d",&A[i]); intans=INF; REP (St,1, N) {REP (i,st,n) a1[i-st+1]=A[i]; REP (i,1, st-1) a1[i+ (n-st+1)]=A[i]; intt=0; for(inti=st;i>=1; i--) a2[++t]=A[i]; for(inti=n;i>st;i--) a2[++t]=A[i]; REP (i,1, N) id1[a1[i]]=i,id2[a2[i]]=i; //cout<<st<< "" <<ans<<endl;ans=min (Ans,min (Solve (N,A1,ID1), Solve (N,A2,ID2))); } cout<<ans<<Endl; } return 0;}/** Why is it so different from the starting point of the direct pick 1? */
View Code
Beginning directly to 1 as the starting point has not been, and the starting point enumerated a bit but somehow over, although do not know why, but dare to dare to do is acmer the necessary qualities.
UVA 10570 Meeting with aliens