rendered paste body#include<iostream>#include<vector>#include<queue>#include<map>using namespace std;queue<pair<int,int> > q;map<int,bool> visited;int N,A[11];int get(){ int ret=0,x=1,pos[11],st=0; while(A[st]!=1) st++; st=(st+1)%N; while(A[st]!=1) pos[A[st]]=x++,st=(st+1)%N; for(int j=2;j<=N;j++) ret=ret*10+pos[j]; return ret;}void construct(int seq){ int st=N; while(seq){ A[(seq%10)]=st; st--; seq/=10; } A[0]=1; } bool isFinal(int seq){ int st=N-1; while(seq){ if(seq%10!=st) return false; seq/=10; st--; } return true;}int main(){ scanf("%d",&N); for(int i=0;i<N;i++) scanf("%d",&A[i]); int start=get(); q.push(make_pair(start,0)); visited[start]=1; int ans=0; while(!q.empty()){ pair<int,int> next=q.front(); q.pop(); if(isFinal(next.first)){ ans=next.second; break; } construct(next.first); for(int i=0;i<N;i++){ swap(A[i],A[(i+1)%N]); start=get(); if(!visited[start]) q.push(make_pair(start,next.second+1)),visited[start]=1; swap(A[i],A[(i+1)%N]); } } cout<<ans<<endl; //system("PAUSE");}