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

Previous submission (verdict: Wrong answer on test 4):
#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=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=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);
    for(int i=2;i<=n;i++){
        if(A[i]<A[i-1]){
        }else{
            ans.emplace_back(A[i],A[i-1]);
            ans.emplace_back(A[i-1],A[i]);
        }
    }
    cout<<ans.size()<<'\n';
    for(auto& [l,r]:ans)cout<<l<<' '<<r<<'\n';
}


Submission (verdict: Runtime error on test 5):
#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=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=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';
}