The main idea: to give a sequence of 1~n, each operation can be the length of an even-numbered sequence to exchange the first half and the last half of the position. Find the steps to turn this sequence into ascending order.
Problem Analysis: Structural solution.
The code is as follows:
# include<iostream># include<cstdio># include<queue># include<cstring># include< Algorithm>using namespace Std;int a[10005],pos[10005],n;queue<int>l,r;void print (int ans) {printf ("%d\n", ANS); while (! L.empty ()) {printf ("%d%d\n", L.front (), R.front ()); L.pop (), R.pop (); }}void change (int l,int r) {L.push (L), R.push (R); for (int i=l,j=l+ (r-l)/2+1;j<=r;++i,++j) {pos[a[i]]=j,pos[a[j]]=i; Swap (a[i],a[j]); }}void solve () {while (! L.empty ()) L.pop (); while (! R.empty ()) R.pop (); int l=1,ans=0; while (L<=n) {for (int i=l;i<=n;++i) {if (a[i]==i) {++l; Continue } ++ans; int p=pos[i]; if (2*p-i-1<=n) change (i,2*p-i-1); else{if ((p-i) &1) Change (i,p); else change (i+1,p); } if (a[i]==i) ++l; Break }} print (ANS);} int main () {int T; scanf ("%d", &t); while (t--) {scanf ("%d", &n); for (int i=1;i<=n;++i) {scanf ("%d", a+i); Pos[a[i]]=i; } solve (); } return 0;}
UVA-1611 Crane (construction)