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
| #include<cstdio> #include<vector> #include<random> #include<chrono> #include<cstring> #include<algorithm> using namespace std; const int N=3010,FSIZE=1<<26; int n,a[N],b[N]; mt19937 rnd(chrono::steady_clock().now().time_since_epoch().count()); vector<pair<int,int>> ans; char BuF[FSIZE],*InF=BuF; template<typename T>void read(T &x){ for(;*InF<33;++InF); for(x=0;*InF>32;x=x*10+(*InF++^48)); } void change(int l,int r){ ans.emplace_back(l,r); for(int i=l,mid=(l+r+1)>>1,cnt=0;i<=mid;++i){ b[cnt++]=a[mid+i-l]; b[cnt++]=a[i]; } memcpy(a+l,b,(r-l+1)<<2); } int main(){ fread(BuF,1,FSIZE,stdin); read(n); for(int i=1,x;i<=n;++i){read(x);a[x]=i;} for(int i=1;i<=100;++i){ int l=rnd()%n+1,r=rnd()%n+1; if(l>r) swap(l,r); if(l<r) change(l,r); } for(int i=n;i;--i){ int p=find(a+1,a+i+1,i)-a; for(;p+p<=i;change(1,p<<=1)); if(p!=i) change(p+p-i+1,i); } reverse(ans.begin(),ans.end()); printf("%lld\n",ans.size()); for(auto [i,j]:ans) printf("%d %d\n",i,j); return(0); }
|