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

Previous submission (verdict: Wrong answer on test 7):
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cmath>
#include <queue>
#include <algorithm>
#include <sstream>
#include <functional>
#include <unordered_map>
#include <unordered_set>
#include <climits>

using namespace std;

#define pb push_back
#define int long long
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define LOG(n) 31 - __builtin_clz(n)
#define nl  "\n"
#define ok  cout<<"OK\n"
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define free freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
#define ff first
#define ss second
#define vi vector<int>
#define vvi vector<vector<int> >
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
typedef pair<int,int> pii;
typedef long double ld;
const long long INF = 1ll << 32;
const long long MAX_N = 1e6 + 5;
const long long MOD = 1e9 + 7;
const long long N = 9999999;
long double pi = 3.1415926535;

void show(vector<int> a) {for(auto it : a) cout<<it<<","; cout<<nl;}
int gcd(int a, int b){if(a == 0) return b; if(b == 0) return a;int r = min(a, b);while(r>0){if(a%r==0 && b%r==0){break;}r--;}return r;}

int sq(int n){if(n==0)return 0LL;int l=1,r=n;while(l<=r){int m=l+(r-l)/2;int p=m*m;if(p==n)return m;if(p<n)l=m+1;else r=m-1;}return l;}
int I;


void solve()
{
    int n;
    cin>>n;
    vector<int> c(n);
    for(int &it : c) cin>>it;
    unordered_map<int , int> mark;
    vector<pair<int , int> > res;
    for(int i = n - 1 ; i >= 0 ; i--) {
        for(int j = c[i] + 1 ; j <= n ; j++) {
            if(mark[j] != 1) {
                res.push_back({j , c[i]});
            }
        }
        mark[c[i]] = 1;
    }
    for(int i = 0 ; i < n ; i++) {
        int j;
        for(j = i + 1 ; j < n ; j++) {
            int p = c[i] , q = c[j];
            if(q > p) {
                res.push_back({c[j] , c[i]});
            }
            else break;
        }
        j--;
        for(j ; j > i ; j--) {
            res.push_back({c[i] , c[j]});
        }
    }
    cout<<res.size()<<endl;
    for(pair<int , int> overtakes : res) {
        cout<<overtakes.first<<" "<<overtakes.second<<endl;
    }
    
}

int32_t main()
{
    ios;
//    freopen("visitfj.in","r",stdin);
//    freopen("visitfj.out","w",stdout);
    int Test = 1;
//    cin>>Test;
    for(I = 1 ; I <= Test ; I++)
    {
        solve();
    }
 
 
}


Submission (verdict: Accepted):
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cmath>
#include <queue>
#include <algorithm>
#include <sstream>
#include <functional>
#include <unordered_map>
#include <unordered_set>
#include <climits>

using namespace std;

#define pb push_back
#define int long long
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define LOG(n) 31 - __builtin_clz(n)
#define nl  "\n"
#define ok  cout<<"OK\n"
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define free freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
#define ff first
#define ss second
#define vi vector<int>
#define vvi vector<vector<int> >
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
typedef pair<int,int> pii;
typedef long double ld;
const long long INF = 1ll << 32;
const long long MAX_N = 1e6 + 5;
const long long MOD = 1e9 + 7;
const long long N = 9999999;
long double pi = 3.1415926535;

void show(vector<int> a) {for(auto it : a) cout<<it<<","; cout<<nl;}
int gcd(int a, int b){if(a == 0) return b; if(b == 0) return a;int r = min(a, b);while(r>0){if(a%r==0 && b%r==0){break;}r--;}return r;}

int sq(int n){if(n==0)return 0LL;int l=1,r=n;while(l<=r){int m=l+(r-l)/2;int p=m*m;if(p==n)return m;if(p<n)l=m+1;else r=m-1;}return l;}
int I;


void solve()
{
    int n;
    cin>>n;
    vector<int> c(n);
    unordered_map<int , int> mark;
    for(int &it : c) cin>>it;
    vector<pair<int , int> > ans;
    for(int i = n - 1 ; i >= 0 ; i--) {
        stack<pair<int , int> > temp;
        for(int j = c[i] - 1 ; j > 0 ; j--) {
            if(mark[j] == 1) continue ;
            ans.push_back({c[i] , j});
            temp.push({j , c[i]});
        }
        while(!temp.empty()) {
            ans.push_back(temp.top());
            temp.pop();
        }
        
        for(int j = c[i] + 1 ; j <= n ; j++) {
            if(mark[j] == 1) continue ;
            ans.push_back({j , c[i]});
        }
        mark[c[i]] = 1;
    }
    cout<<ans.size()<<endl;
    for(pair<int , int> overtake : ans) {
        cout<<overtake.first<<" "<<overtake.second<<endl;
    }
    
}

int32_t main()
{
    ios;
//    freopen("visitfj.in","r",stdin);
//    freopen("visitfj.out","w",stdout);
    int Test = 1;
//    cin>>Test;
    for(I = 1 ; I <= Test ; I++)
    {
        solve();
    }
 
 
}