Problem: 2052A - Adrenaline Rush
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 7):
#include<bits/stdc++.h>
#include<bits/extc++.h>
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(nullptr);std::cout.tie(nullptr);
#define all(x) (x).begin(),(x).end()
#define quchong(x) (x).erase(unique(all(x)),(x).end())
#define Yes(x,y) cout<<((x)?"Yes":"No")<<y
#define yes(x,y) cout<<((x)?"yes":"no")<<y
#define YES(x,y) cout<<((x)?"YES":"NO")<<y
#define ls ((u)<<1)
#define rs ((u)<<1|1)
#define mid (((l)+(r))>>1)
#define lowbit(x) ((x)&(-(x)))
#define itn int
#define asn ans
#define reisze resize
#define pdd pair<double,double>
#define pll pair<LL,LL>
#define pii pair<int,int>
#define tll tuple<LL,LL,LL>
#define tii tuple<int,int,int>
#define plll pair<LLL,LLL>
#define ULL unsigned long long
#define LL long long
#define LLL __int128
#define ld long double
#define ui64 uint64_t
#define ui32 uint32_t
using namespace std;
using namespace __gnu_pbds;
template<typename T>
using RBTree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
T fang(const T& a){
    return a*a;
}
template<typename T,typename Q>
bool chmax(T& u1,T& u2,const Q& v){
    if(v>u1) { u2 = u1, u1 = v;return true;}
    if(v>u2){u2=v;return true;}
    return false;
}
template<typename T,typename Q>
bool chmin(T& u1,T& u2,const Q& v){
    if(v<u1) { u2 = u1, u1 = v;return true;}
    if(v<u2){u2=v;return true;}
    return false;
}
template<typename T,typename Q>
bool chmin(T& a,const Q& b){
    return a > b && (a = b, true);
}
template<typename T,typename Q>
bool chmax(T& a,const Q& b){
    return a<b&&(a=b,true);
}
template<typename t1,typename t2>
istream& operator>>(istream& in,pair<t1,t2>& pa){
    in>>pa.first>>pa.second;
    return in;
}
template<typename t1,typename t2>
ostream& operator<<(ostream& out,const pair<t1,t2>& pa){
    out<<pa.first<<' '<<pa.second;
    return out;
}
template<typename T>
istream& operator>>(istream& in,vector<T>& arr){
    for(auto& v:arr)in>>v;
    return in;
}
template<typename T>
ostream& operator<<(ostream& out,const vector<T>& arr){
    for(auto& v:arr)out<<v<<' ';
    return out;
}
int rand(int l,int r){
    static mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
    return uniform_int_distribution<int>(l, r)(rng);
}
const ld eps=1e-9;
const int NN=2e6+5;
const int SIZ=1e7;
const LL inf=1e17;
void solve(){

}
signed main(){
    IOS;
    int n;
    cin>>n;
    vector<int> A(n);cin>>A;
    A.insert(A.begin(),0);
    vector<int> inA(n+1);
    for(int i=1;i<=n;i++)inA[A[i]]=i;
    vector<pii> ans;
    vector<int> pos(n+1);
    for(int i=1;i<=n;i++)pos[i]=i;
    vector<int> now(n+1);iota(all(now),0);
    for(int i=1;i<=n;i++){
        if(pos[i]==inA[i])continue;
        if(inA[i]>pos[i]){
            for(int j=pos[i]+1;j<=inA[i];j++){
                ans.emplace_back(now[j],now[j-1]);
                swap(pos[now[j]],pos[now[j-1]]);
                swap(now[j],now[j-1]);
            }
        }else{
            for(int j=pos[i]-1;j>=inA[i];j--){
                ans.emplace_back(now[j+1],now[j]);
                swap(pos[now[j+1]],pos[now[j]]);
                swap(now[j+1],now[j]);
            }
        }
    }
    assert(A==now);
    assert(pos==inA);
    vector<vector<int>> ed;
    ed.emplace_back();
    ed.back().emplace_back(A[1]);
    for(int i=2;i<=n;i++){
        if(A[i]>A[i-1])ed.back().emplace_back(A[i]);
        else {
            ed.emplace_back();
            ed.back().emplace_back(A[i]);
        }
    }
    for(auto& arr:ed){
        for(int i=arr.size()-1;i;i--){
            for(int j=i-1;~j;j--){
                ans.emplace_back(arr[i],arr[j]);
            }
            for(int j=0;j<i;j++)ans.emplace_back(arr[j],arr[i]);
        }
    }
    cout<<ans.size()<<'\n';
    for(auto& [l,r]:ans)cout<<l<<' '<<r<<'\n';
}


Submission (verdict: Runtime error on test 9):
#include<bits/stdc++.h>
#include<bits/extc++.h>
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(nullptr);std::cout.tie(nullptr);
#define all(x) (x).begin(),(x).end()
#define quchong(x) (x).erase(unique(all(x)),(x).end())
#define Yes(x,y) cout<<((x)?"Yes":"No")<<y
#define yes(x,y) cout<<((x)?"yes":"no")<<y
#define YES(x,y) cout<<((x)?"YES":"NO")<<y
#define ls ((u)<<1)
#define rs ((u)<<1|1)
#define mid (((l)+(r))>>1)
#define lowbit(x) ((x)&(-(x)))
#define itn int
#define asn ans
#define reisze resize
#define pdd pair<double,double>
#define pll pair<LL,LL>
#define pii pair<int,int>
#define tll tuple<LL,LL,LL>
#define tii tuple<int,int,int>
#define plll pair<LLL,LLL>
#define ULL unsigned long long
#define LL long long
#define LLL __int128
#define ld long double
#define ui64 uint64_t
#define ui32 uint32_t
using namespace std;
using namespace __gnu_pbds;
template<typename T>
using RBTree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
T fang(const T& a){
    return a*a;
}
template<typename T,typename Q>
bool chmax(T& u1,T& u2,const Q& v){
    if(v>u1) { u2 = u1, u1 = v;return true;}
    if(v>u2){u2=v;return true;}
    return false;
}
template<typename T,typename Q>
bool chmin(T& u1,T& u2,const Q& v){
    if(v<u1) { u2 = u1, u1 = v;return true;}
    if(v<u2){u2=v;return true;}
    return false;
}
template<typename T,typename Q>
bool chmin(T& a,const Q& b){
    return a > b && (a = b, true);
}
template<typename T,typename Q>
bool chmax(T& a,const Q& b){
    return a<b&&(a=b,true);
}
template<typename t1,typename t2>
istream& operator>>(istream& in,pair<t1,t2>& pa){
    in>>pa.first>>pa.second;
    return in;
}
template<typename t1,typename t2>
ostream& operator<<(ostream& out,const pair<t1,t2>& pa){
    out<<pa.first<<' '<<pa.second;
    return out;
}
template<typename T>
istream& operator>>(istream& in,vector<T>& arr){
    for(auto& v:arr)in>>v;
    return in;
}
template<typename T>
ostream& operator<<(ostream& out,const vector<T>& arr){
    for(auto& v:arr)out<<v<<' ';
    return out;
}
int rand(int l,int r){
    static mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
    return uniform_int_distribution<int>(l, r)(rng);
}
const ld eps=1e-9;
const int NN=2e6+5;
const int SIZ=1e7;
const LL inf=1e17;
vector<int> A,inA,pos,now;
vector<pii> ans;
void dfs(int l,int r){
    if(l>=r)return;
    int m=(l+r)>>1;
    int V=now[m];
    vector<int> l2r;
    for(int i=l;i<m;i++){
        if(inA[now[i]]>inA[V]){
            l2r.emplace_back(i);
        }
    }
    for(int p : l2r){
        for(int k=p-1;k>=l &&inA[now[k]]<inA[V]; k--){
            ans.emplace_back(now[k+1],now[k]);
            swap(pos[now[k]],pos[now[k+1]]);
            swap(now[k],now[k+1]);
        }
    }
    vector<int> r2l;
    for(int i=r;i>m;i--){
        if(inA[now[i]]<inA[V])r2l.emplace_back(i);
    }
    for(int p:r2l){
        for(int k=p+1;k<=r&&inA[now[k]>inA[V]];k++){
            ans.emplace_back(now[k],now[k-1]);
            swap(pos[now[k]],pos[now[k-1]]);
            swap(now[k],now[k-1]);
        }
    }

    {
        int i=m-1;
        for(;i>=l&&inA[now[i]]<inA[V];i--){
            for(int j=m;j<=r&&inA[now[j]]>=inA[V];j++)
                ans.emplace_back(now[j],now[i]);
        }
        i++;
        int up=m+1;
        while(up<=r&&inA[now[up]]>=inA[V])up++;
        up--;
        for(;i<m;i++)for(int j=up;j>=m;j--)ans.emplace_back(now[i],now[j]);
    }
    {
        int i=m+1;
        for(;i<=r&&inA[now[i]]>inA[V];i++){
            ans.emplace_back(now[i],V);
        }
        i--;
        for(;i>m;i--)ans.emplace_back(V,now[i]);
    }

    {
        int i=l;
        while(inA[now[i]]>inA[V])i++;
        if(i!=l){
            i--;
            for(;i>=l;i--){
                for(int j=i+1;j<m&&inA[now[j]]<inA[V];j++){
                    ans.emplace_back(now[j],now[j-1]);
                    swap(pos[now[j]],pos[now[j-1]]);
                    swap(now[j],now[j-1]);
                }
            }
        }
    }
    {
        int i=r;
        while(inA[now[i]]<inA[V])i--;
        if(i!=r){
            i++;
            for(;i<=r;i++){
                for(int j=i-1;j>m&&inA[now[j]]>inA[V];j--){
                    ans.emplace_back(now[j+1],now[j]);
                    swap(pos[now[j+1]],pos[now[j]]);
                    swap(now[j+1],now[j]);
                }
            }
        }
    }

    int k=m-1;
    for(;k>=l&&inA[now[k]]>inA[V];k--){
        for(int j=k+1;j<=r&&inA[now[j]]<=inA[V];j++){
            ans.emplace_back(now[j],now[j-1]);
            swap(pos[now[j]],pos[now[j-1]]);
            swap(now[j],now[j-1]);
        }
        m--;
    }
    k=m+1;
    for(;k<=r&&inA[now[k]]<inA[V];k++){
        for(int j=k-1;j>=l&&inA[now[j]]>=inA[V];j--){
            ans.emplace_back(now[j+1],now[j]);
            swap(pos[now[j+1]],pos[now[j]]);
            swap(now[j+1],now[j]);
        }
    }
    dfs(l,pos[V]-1);
    dfs(pos[V]+1,r);
}
signed main(){
    IOS;
    int n;
    cin>>n;
    A.resize(n);cin>>A;
    A.insert(A.begin(),0);
    inA.resize(n+1);
    for(int i=1;i<=n;i++)inA[A[i]]=i;
    pos.resize(n+1);
    for(int i=1;i<=n;i++)pos[i]=i;
    now.resize(n+1);
    iota(all(now),0);
    dfs(1,n);
    vector<int> test(n+1);
    iota(all(test),0);
    vector<int> tpos(n+1);
    iota(all(tpos),0);
    set<pii> HAS;
    auto SWAP=[&](int x,int y)->void{
        assert(!HAS.count({x,y}));
        assert(tpos[x]==tpos[y]+1);
        swap(test[tpos[x]],test[tpos[y]]);
        swap(tpos[x],tpos[y]);
        HAS.insert({x,y});
    };
    for(auto& [l,r]:ans){
        SWAP(l,r);
    }
    assert(test==A);
    cout<<ans.size()<<'\n';
    for(auto& [l,r]:ans)cout<<l<<' '<<r<<'\n';
}