A permutation on the integers from 1 to n are, simply put, a particular rearrangement of these integers. Your task is to generate a given permutation from the initial arrangement 1, 2, 3, ..., n using only and simple Operati Ons.
operation 1:you may swap the first and the numbers. For example, this would change the arrangement 3,2,4,5,1 to 2,3,4,5,1.
operation 2:you may move the first number to the end of the arrangement. For example, this would change the arrangement 3,2,4,5,1 to 2,4,5,1,3.
Input
The input consists of a number of test cases. Each test case is begins with a single integer n between 1 and 300. On the same line, a permutation of integers 1 through n was given where consecutive integers is separated by a single SPAC E. Input is terminated by a line containing ' 0 ' which should not being processed.
Output
For the test case, you is to output a string of describes a sequence of operations. The string itself should consist only of the characters ' 1 ' and ' 2 '. This string should is such if we start with the initial arrangement 1, 2, 3, ..., n−1, N and successively apply Rules 1 and 2 according to the order they appear in the output, then the resulting permutation was identical to the input p Ermutation. The output string does not necessarily need to is the shortest such string, but it must is no longer than 2n 2 characters. If It is possible to generate the permutation using 0 operations and then your may simply output a blank line.
Sample Input
3 2 1 3
3 2 3 1
4 4 2) 3 1
0
Sample Output
1
2
12122
Test instructions: There is a set of sequences originally arranged at 1 2 3 4 5 .... N, after Operation 1 and Operation 2 can be changed into the sequence given in the input, which requires the output of what has been done
If 123 is operated 1 can get 213
Idea: Construction Method ... There is an algorithm, fortunately, the topic said not necessarily require the least input of the series of operations, then for the program to find an operation 1 or Operation 2 of the decision condition, the condition is not to perform operation 1 on the execution of 2, then when the operation can not be performed 1, the answer is the first two numbers orderly, disorderly when the exchange of Bai, If you want to change the order, you can't change it. The point is that there is a special case in this judgment, that is, if there are 1 o'clock in the previous two numbers, then no matter if they have no order, they can not perform operation 1, otherwise it will get into the loop, how can not get the result
The rest, simulation bar ...
#include"Iostream"#include"Cstdio"#include"CString"UsingNamespace Std;Constint MAXN=300+10;int a[MAXN];int n;StringAns;voidSwap1(){int T; T=a[0]; A[0]=a[1]; A[1]=t;}voidSwap2(){int T; T=a[n-1];For(int I=n-1; I>0; I--){A[I]=a[I-1];} A[0]=t;}boolCheck(){For(int I=0; I<n; I++){If(A[I]!=i+1)ReturnFalse;}ReturnTrue;}voidPrint(){For(int I=ans.Length()-1; I>=0; I--) cout<<ans[I];}IntMain(){While(CIN>>n&&n){Memset(A,0,sizeof(A)); Ans.Clear();For(int I=0; I<n; I+ +) CIN>>a[I];int T=2*n*n;While(T--){If(Check()){Print();Break;}If(A[0]>a[1]&&a[0]!=1&&a[1]!=1) {swap1 (); Ans+=< Span class= "sh-string" > ' 1 ' ;} else {swap2 Ans+= ' 2 ' ;< Span class= "Sh-cbracket" >} } cout<<endl return 0;< Span class= "Sh-cbracket" >}
Fourth week of training (efficient algorithm design) o problem (structural problem)