Problem: 2052A - Adrenaline Rush
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 5):
 
#include <bits/stdc++.h>
using namespace std;

// segmentr tree template
// https://atcoder.jp/contests/abc382/submissions/60341342

#define ll long long 
#define lld long double
#define pll pair<long long, long long>
#define vll vector<ll>
#define vvll vector<vll>
#define vlld vector<lld>
#define vpll vector<pair<ll, ll>>
#define forn(i, n) for (int i = 1; i <= int(n); ++i)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define ff first
#define ss second
#define YES cout << "Yes\n"
#define NO cout << "No\n"
#define nl "\n"
#define mod 1000000007
// #define mod 998244353 


ll mod_add(ll a, ll b ) {a = a % mod; b = b % mod; return (((a + b) % mod) + mod) % mod;}
ll mod_mul(ll a, ll b ) {a = a % mod; b = b % mod; return (((a * b) % mod) + mod) % mod;}
ll power(ll a , ll b) { if(b == 0) return 1;ll res = power(a , b>>1);if(b & 1) return (res * res % mod) * a % mod;return res * res % mod;}
ll expo(ll a, ll b) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}


void solve() {   

    int n; cin>>n;
    vll v(n+10), vis(n+10,0);
    vpll ans;

    for( int i=1;i<=n;i++ ) {
        cin>>v[i];
    }

    for( int i=1;i<=n;i++ ) {
        vis[v[i]] = 1;
        vpll dup;
        for( int j=v[i]+1;j<=n;j++ ) {
            if( vis[j] == 0 ) {
                ans.pb({j,v[i]}) ;
                dup.pb({v[i],j});
            }
        }
        while( dup.size() ) {
            ans.pb(dup.back());
            dup.pop_back();
        }
        for( int j=1;j<v[i];j++ ) {
            if(  vis[j] == 0 ) {
                ans.pb({v[i],j});
            }
        }
    }

    cout << ans.size() << nl;
    for( auto it : ans ) {
        cout << it.ff << " " << it.ss << nl ;
    }
    
}


void main2( ) {
     
    //  freopen("hps.in", "r", stdin);
    // freopen("hps.out", "w", stdout);

#ifndef ONLINE_JUDGE
     freopen("i1.txt" , "r" , stdin);
     freopen("output.txt", "w", stdout);
#endif

    int tt = 1;
    // cin >> tt;

    // genfact() ; 
    
    for( int i=1;i<=tt;i++ )  {
        // cout << "Case #" << i << ": " ; 
        solve(); 
    } 

}
 
// static void run_with_stack_size(void (*func)(void), size_t stsize) {
//     char *stack, *send;
//     stack = (char *)malloc(stsize);
//     send = stack + stsize - 16;
//     send = (char *)((uintptr_t)send / 16 * 16);
//     asm volatile(
//         "mov %%esp, (%0)\n"
//         "mov %0, %%esp\n"
//         :
//         : "r"(send));
//     func();
//     asm volatile("mov (%0), %%esp\n" : : "r"(send));
//     free(stack);
// }

int main() {
    
    ios::sync_with_stdio(0);
    cin.tie(0);

    // run_with_stack_size( main2 , 512 * 512 * 512 );
    
    main2() ; 

    return 0 ; 

   
}

Submission (verdict: Accepted):
 
#include <bits/stdc++.h>
using namespace std;

// segmentr tree template
// https://atcoder.jp/contests/abc382/submissions/60341342

#define ll long long 
#define lld long double
#define pll pair<long long, long long>
#define vll vector<ll>
#define vvll vector<vll>
#define vlld vector<lld>
#define vpll vector<pair<ll, ll>>
#define forn(i, n) for (int i = 1; i <= int(n); ++i)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define ff first
#define ss second
#define YES cout << "Yes\n"
#define NO cout << "No\n"
#define nl "\n"
#define mod 1000000007
// #define mod 998244353 


ll mod_add(ll a, ll b ) {a = a % mod; b = b % mod; return (((a + b) % mod) + mod) % mod;}
ll mod_mul(ll a, ll b ) {a = a % mod; b = b % mod; return (((a * b) % mod) + mod) % mod;}
ll power(ll a , ll b) { if(b == 0) return 1;ll res = power(a , b>>1);if(b & 1) return (res * res % mod) * a % mod;return res * res % mod;}
ll expo(ll a, ll b) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}


void solve() {   

    int n; cin>>n;
    vll v(n+10), vis(n+10,0);
    vpll ans;

    for( int i=1;i<=n;i++ ) {
        cin>>v[i];
    }

    for( int i=n;i>0;i-- ) {
        vis[v[i]] = 1;
        vpll dup;
        for( int j=v[i]-1;j>0;j-- ) {
            if(  vis[j] == 0 ) {
                ans.pb({v[i],j});
                dup.pb({j,v[i]}) ;
            } 
        }
        while( dup.size() ) {
            ans.pb(dup.back());
            dup.pop_back();
        }
        for( int j=v[i]+1;j<=n;j++ ) {
            if( vis[j] == 0 ) {
                ans.pb({j,v[i]});
            }
        }
    }

    cout << ans.size() << nl;
    for( auto it : ans ) {
        cout << it.ff << " " << it.ss << nl ;
    }
    
}


void main2( ) {
     
    //  freopen("hps.in", "r", stdin);
    // freopen("hps.out", "w", stdout);

#ifndef ONLINE_JUDGE
     freopen("i1.txt" , "r" , stdin);
     freopen("output.txt", "w", stdout);
#endif

    int tt = 1;
    // cin >> tt;

    // genfact() ; 
    
    for( int i=1;i<=tt;i++ )  {
        // cout << "Case #" << i << ": " ; 
        solve(); 
    } 

}
 
// static void run_with_stack_size(void (*func)(void), size_t stsize) {
//     char *stack, *send;
//     stack = (char *)malloc(stsize);
//     send = stack + stsize - 16;
//     send = (char *)((uintptr_t)send / 16 * 16);
//     asm volatile(
//         "mov %%esp, (%0)\n"
//         "mov %0, %%esp\n"
//         :
//         : "r"(send));
//     func();
//     asm volatile("mov (%0), %%esp\n" : : "r"(send));
//     free(stack);
// }

int main() {
    
    ios::sync_with_stdio(0);
    cin.tie(0);

    // run_with_stack_size( main2 , 512 * 512 * 512 );
    
    main2() ; 

    return 0 ; 

   
}