Problem: 2052A - Adrenaline Rush
Language: C++17 (GCC 7-32)

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

#define int long long
#define nl cout<<endl
#define vec vector<int>
#define vecs vector<string>
#define ddv vector<vec>
#define ub upper_bound 
#define lb lower_bound
#define prnt(v) for(auto i:v) cout<<i<<" "; nl
#define inp(v); for(auto &i:v) cin>>i;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define pinp(a) for(int i=1;i<=n;++i) { int p; cin>>p; a[i] = p; }

const int N = 2e5;
const int M = 1e9+7;

int factorials[N+1];
int factInv[N+1];
bool isPrime[N+1];

int gcd(int a,int b){ //O(logb)
    if(!b) return a;
    return gcd(b,a%b);
}

int lcm(int a,int b){ //O(logb)
    return (a*b/gcd(a,b)) % M;
}

int binExp(long long a, int b) { //O(logb)
    a %= M;
    int ans = 1;
    while (b) {
        if (b & 1) ans = (ans * 1LL * a) % M;
        a = (a * 1LL * a) % M;
        b >>= 1;
    }
    return ans;
}

int inv(int n){ //O(logM)
    return binExp(n,M-2);
}

void FACT(){ //O(N)
    factorials[0] = 1;
    for (int i = 1; i <= N; i++) {
        factorials[i] = factorials[i - 1] * 1LL * i % M;
    }
}

void FACTINV(){ //O(N)
    for (int i = 0; i <= N; i++) {
        factInv[i] = inv(factorials[i]);
    }
}

void SIEVE(){ //O(NloglogN)

    //Pre-compute

    fill(isPrime, isPrime+N, true);
    isPrime[0]=isPrime[1]=false;

    for(int i = 2; i <= N; ++i){
        if(isPrime[i]){
            for(int j=2*i ; j<=N ; j+=i)
                isPrime[j]=false;
        }
    }
}

long long nCr(int n, int r){ //O(1) excl. Pre-Comp which is O(N).

    // Pre-Compute before using this func.
    // FACT();
    // FACTINV();

    if (r > n) return 0;
    return (factorials[n] * 1LL * factInv[r] % M * factInv[n - r] % M) % M;
}

void solve(){
    int n;
    cin>>n;
    vec a(n+1);
    pinp(a);

    vec vis(n+1,0);
    ddv ans;

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

    for(int i=1;i<n;++i){
        if(a[i] < a[i+1]){
            ans.pb({a[i+1],a[i]});
            ans.pb({a[i],a[i+1]});
        }
    }

    cout<<ans.size()<<endl;
    for(auto i:ans){
        cout<<i[0]<<" "<<i[1]<<endl;
    }
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);

    // FACT();
    // FACTINV();
    // SIEVE();
    
    int t = 1;
    // cin>>t;
 
    while(t--){
        solve();
    }
 
    return 0;
}


Submission (verdict: Wrong answer on test 7):
#include <cstdio>
#include <cstring>
#include<bits/stdc++.h>
using namespace std;

#define int long long
#define nl cout<<endl
#define vec vector<int>
#define vecs vector<string>
#define ddv vector<vec>
#define ub upper_bound 
#define lb lower_bound
#define prnt(v) for(auto i:v) cout<<i<<" "; nl
#define inp(v); for(auto &i:v) cin>>i;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define pinp(a) for(int i=1;i<=n;++i) { int p; cin>>p; a[i] = p; }

const int N = 2e5;
const int M = 1e9+7;

int factorials[N+1];
int factInv[N+1];
bool isPrime[N+1];

int gcd(int a,int b){ //O(logb)
    if(!b) return a;
    return gcd(b,a%b);
}

int lcm(int a,int b){ //O(logb)
    return (a*b/gcd(a,b)) % M;
}

int binExp(long long a, int b) { //O(logb)
    a %= M;
    int ans = 1;
    while (b) {
        if (b & 1) ans = (ans * 1LL * a) % M;
        a = (a * 1LL * a) % M;
        b >>= 1;
    }
    return ans;
}

int inv(int n){ //O(logM)
    return binExp(n,M-2);
}

void FACT(){ //O(N)
    factorials[0] = 1;
    for (int i = 1; i <= N; i++) {
        factorials[i] = factorials[i - 1] * 1LL * i % M;
    }
}

void FACTINV(){ //O(N)
    for (int i = 0; i <= N; i++) {
        factInv[i] = inv(factorials[i]);
    }
}

void SIEVE(){ //O(NloglogN)

    //Pre-compute

    fill(isPrime, isPrime+N, true);
    isPrime[0]=isPrime[1]=false;

    for(int i = 2; i <= N; ++i){
        if(isPrime[i]){
            for(int j=2*i ; j<=N ; j+=i)
                isPrime[j]=false;
        }
    }
}

long long nCr(int n, int r){ //O(1) excl. Pre-Comp which is O(N).

    // Pre-Compute before using this func.
    // FACT();
    // FACTINV();

    if (r > n) return 0;
    return (factorials[n] * 1LL * factInv[r] % M * factInv[n - r] % M) % M;
}

void solve(){
    int n;
    cin>>n;
    vec a(n+1);
    pinp(a);

    vec vis(n+1,0);
    ddv ans;

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

    int l=1, r=1;
    while(r<=n){
        while(r<n && a[r+1]>a[r]){
            ++r;
        }

        for(int i=r;i>l;--i){
            for(int j=i-1;j>=l;--j){
                ans.pb({a[i],a[j]});
            }
            for(int j=l;j<i;++j){
                ans.pb({a[j],a[i]});
            }
        }

        ++r;
        l=r;
    }
    cout<<ans.size()<<endl;
    for(auto i:ans){
        cout<<i[0]<<" "<<i[1]<<endl;
    }
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);

    // FACT();
    // FACTINV();
    // SIEVE();
    
    int t = 1;
    // cin>>t;
 
    while(t--){
        solve();
    }
 
    return 0;
}