1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #include<cstdio> #include<cstring> #include<vector> using namespace std; const int maxn=10010; int n,a[maxn],pos[maxn]; struct range{ int l,r; range(int l=0,int r=0):l(l),r(r){} void print(){ printf("%d %d\n",l+1,r+1); return; } }; vector<range> ans; void Swap(int sta,int len){ for(int i=0;i<len;++i) swap(a[sta+i],a[sta+len+i]),swap(pos[a[sta+i]-1],pos[a[sta+len+i]-1]); ans.push_back(range(sta,sta+len*2-1)); return; } void solve(){ ans.clear(); for(int i=0;i<n;++i){ if(pos[i]==i) continue; while(pos[i]-i>(n-i)/2) Swap(pos[i]*2-n,n-pos[i]); Swap(i,pos[i]-i); } return; } void print_ans(){ printf("%d\n",(int)ans.size()); for(int i=0;i<ans.size();++i) ans[i].print(); return; } int main(){ int t; scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i=0;i<n;++i) scanf("%d",&a[i]),pos[a[i]-1]=i; solve(); print_ans(); } return 0; }
|