The title of the topic is wrong, his second operation is to transfer the last one to the first, and then output to reverse the output
Problem-solving ideas: Similar bubble sorting method. First, if the first is N, then the back to the front, and then compare, take the largest put in front, this next time N to the front, n-1 on the back of N.
#include <cstdio>#include <vector>#include <deque>#define MAXN 310using namespace STD;intNUM[MAXN], N; deque<int>dq vector<int>AnsBOOLJudge () { for(inti =1; I <= N; i++)if(Dq[i-1]! = i)return false;return true;}intMain () { while(scanf("%d", &n) = =1&& N) {dq.clear (); Ans.clear ();intT for(inti =1; I <= N; i++) {scanf("%d", &t); Dq.push_back (t); }intMAX =2* n * N; for(inti =0; I <= MAX; i++) {intx = dq[0];inty = dq[1];if(x = =1&& judge ()) { Break; }if(x > y && x! = N) {Ans.push_back (1); Swap (dq[0],dq[1]); }Else{Ans.push_back (2); t = Dq.back (); Dq.pop_back (); Dq.push_front (t); } } for(inti = ans.size ()-1; I >=0; i--)printf("%d", Ans[i]);printf("\ n"); }return 0;}
UVA-11925 Generating permutations Reasoning