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

Previous submission (verdict: Wrong answer on test 1):
#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[i],v[j]});
                }
            }
            for(int i=en;i>=st;i--){
                for(int j=i-1;j>=st;j--){
                    ans.pb({v[i],v[j]});
                }
            }
            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: 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

*/