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

Previous submission (verdict: Wrong answer on test 7):
#include <iostream>
#include <numeric>
#include <cstring>
#include <algorithm>
#include <functional>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <utility>
#include <string>
#include <cmath>
#include <iomanip>
#include <climits>
#include <random>

using namespace std;

const long long int inf = LLONG_MAX-10000000000ll;
const long long mod = 1e9+7;
const long long mod2 = 998244353;

#define inp(a,n) for(int i=0;i<n;i++){cin>>a[i];}
#define inp1(a,n) for(int i=1;i<=n;i++){cin>>a[i];}
#define inp2(a,b,n) for(int i=0;i<n;i++){cin>>a[i]>>b[i];}
#define out(a,n) for(int i=0;i<n;i++){cout<<a[i]<<" ";}
#define out1(a,n) for(int i=1;i<=n;i++){cout<<a[i]<<" ";}
#define loop(n) for(int i=0;i<n;i++)
#define loop1(n) for(int i=1;i<=n;i++)
#define rloop(n) for(int i=n-1;i>=0;i--)
#define loop2(n) for(int j=0;j<n;j++)
#define neg(n) if(i==n)continue;
#define nl cout<<endl;
#define all(v) v.begin(),v.end()
#define pb push_back
#define ll long long
#define F first
#define S second

typedef vector<ll> vi;
typedef vector<pair<ll,ll>> vpi;
typedef pair<ll,ll> pi;

int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t = 1;
    //cin>>t;
    while(t--){
        
        int n;
        cin>>n;

        vi v(n+1);
        inp(v,n);
        map<int,int>mp;
        loop(n){
            mp[v[i]] = i;
        }

        vpi ans;
        for(int i=n;i>=1;i--){
            for(int j=0;j<=mp[i];j++){
                if(v[j] > i)ans.pb({v[j],i});
            }
        }

        int st = 0,en = 0;
        while(en<n){
            while(v[en+1] == v[en]+1 && en<n)en++;
            for(int i=st;i<=en;i++){
                for(int j=i+1;j<=en;j++){
                    ans.pb({v[j],v[i]});
                }
            }
            for(int i=en;i>=st;i--){
                for(int j=i-1;j>=st;j--){
                    ans.pb({v[j],v[i]});
                }
            }
            en++;
            st=en;
        }

        cout<<ans.size()<<endl;
        for(auto x : ans){
            cout<<x.F<<" "<<x.S<<endl;
        }

    }
    return 0;
}


/*

cd /Users/prajit/code/Demo/
rm -f demo
g++ -std=c++17 -o demo demo.cpp
./demo < input.txt > output.txt

*/



Submission (verdict: Accepted):
#include <iostream>
#include <numeric>
#include <cstring>
#include <algorithm>
#include <functional>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <utility>
#include <string>
#include <cmath>
#include <iomanip>
#include <climits>
#include <random>

using namespace std;

const long long int inf = LLONG_MAX-10000000000ll;
const long long mod = 1e9+7;
const long long mod2 = 998244353;

#define inp(a,n) for(int i=0;i<n;i++){cin>>a[i];}
#define inp1(a,n) for(int i=1;i<=n;i++){cin>>a[i];}
#define inp2(a,b,n) for(int i=0;i<n;i++){cin>>a[i]>>b[i];}
#define out(a,n) for(int i=0;i<n;i++){cout<<a[i]<<" ";}
#define out1(a,n) for(int i=1;i<=n;i++){cout<<a[i]<<" ";}
#define loop(n) for(int i=0;i<n;i++)
#define loop1(n) for(int i=1;i<=n;i++)
#define rloop(n) for(int i=n-1;i>=0;i--)
#define loop2(n) for(int j=0;j<n;j++)
#define neg(n) if(i==n)continue;
#define nl cout<<endl;
#define all(v) v.begin(),v.end()
#define pb push_back
#define ll long long
#define F first
#define S second

typedef vector<ll> vi;
typedef vector<pair<ll,ll>> vpi;
typedef pair<ll,ll> pi;

int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t = 1;
    //cin>>t;
    while(t--){
        
        int n;
        cin>>n;

        vi v(n+1);
        map<int,int>ind;
        inp1(v,n);
        loop1(n){
            ind[v[i]] = i;
        }

        set<pair<int,int>> st;

        map<int,int> mp;
        for(int i=n;i>=1;i--){
            for(int j=1;j<=ind[i];j++){
                if(v[j] > i){
                    st.insert({i,v[j]});
                }
            }
        }
        for(int i=1;i<=n;i++){
            mp[i] = i;
        }

        vpi ans;
        vi arr(n+1);
        iota(all(arr),0);
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++){
                if(st.count({j,i}))continue;
                int ov = j,i1 = mp[j];
                int fr = arr[mp[j]-1],i2 = mp[j]-1;
                ans.pb({ov,fr});
                swap(arr[i1],arr[i2]);
                mp[ov] = i2;mp[fr] = i1;
            }
        }
    
        for(int i=n-1;i>=1;i--){
            for(int j=n;j>i;j--){
                if(st.count({i,j}))continue;
                int ov = i,i1 = mp[i];
                int fr = arr[mp[i]-1],i2 = mp[i]-1;
                ans.pb({ov,fr});
                swap(arr[i1],arr[i2]);
                mp[ov] = i2;mp[fr] = i1;
            }
        }

        cout<<ans.size()<<endl;
        for(auto x : ans){
            cout<<x.F<<" "<<x.S<<endl;
        }


    }
    return 0;
}


/*

cd /Users/prajit/code/Demo/
rm -f demo
g++ -std=c++17 -o demo demo.cpp
./demo < input.txt > output.txt

*/