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

Previous submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

template <typename T> 
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define endl "\n"
#define fo(i, n) for (i = 0; i < n; i++)
#define Fo(i, k, n) for (i = k; k < n; k++)
#define pb push_back
#define ll long long
#define mp make_pair
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, arr) for (auto it = arr.begin(); it != arr.end(); it++)
#define PI 3.1415926535897932384626
#define suprit ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
#define line cout << endl;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef map<ll, ll> ml;
typedef map<string, ll> msl;
typedef map<ll, string> mls;
typedef unordered_map<ll, ll> uml;
typedef unordered_map<string, ll> umsl;
typedef unordered_map<ll, string> umls;
typedef set<ll> sl;
typedef set<pair<ll, ll>> spl;
typedef ordered_set<ll> osl;
typedef ordered_set<pair<ll, ll>> ospl;

const ll mod = 1e9 + 7;

bool comp2(pair<ll, ll> &arr, pair<ll, ll> &b)
{
    if (arr.first == b.first)
        return arr.second < b.second;
    return arr.first < b.first;
};

template <typename T> void read(T i, T n, vector<T> &arr) { for(T j = i; j < n; j++) cin >> arr[j]; }
template <typename T> void read(vector<T> &arr) { for(auto &j : arr) cin>>j; }

#ifndef ONLINE_JUDGE

template <typename T, typename V> void print(set<pair<T, V>> &arr) { for(auto &it : arr) cout<<it.first<<" "<<it.second<<endl; line}
template <typename T, typename V> void print(pair<T, V>& pa) { cout<<pa.first<<" "<<pa.second<<endl; }
template <typename T> void print(T i, T last, vector<T> &arr) { for(T j = i; j < last; j++) cout<<arr[j]<<" "; line} 
template <typename T> void print(T i, vector<T> &arr) { for(T j = i; j < arr.size(); j++) cout<<arr[j]<<" "; line} 
template <typename T> void print(vector<T> &arr) { for(auto &i : arr) cout<<i<<" "; line}
template <typename T, typename V> void print(unordered_map<T, V>& arr) { for(auto &it : arr) cout<<it.first<<" "<<it.second<<endl; line}
template <typename T, typename V> void print(map<T, V>& arr) { for(auto &it : arr) cout<<it.first<<" "<<it.second<<endl;}
template <typename T> void print(unordered_set<T> &arr) { for(auto &it : arr) cout<<it<<" "; line }
template <typename T> void print(ordered_set<T> &arr) { for(auto &it : arr) cout<<it<<" "; line }
template <typename T> void print(set<T> &arr) { for(auto &it : arr) cout<<it<<" "; line }
template <typename T, typename... Args> void print(T t, Args... args) { cout << t << " "; print(args...); }
template <typename T> void print(T t) { cout<<t<<"\n"; }
#define debug(x) cout<<#x<<" "<<x<<endl;
#define debug2(x, y) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<endl;
#define debug3(x, y, z) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<endl;
#define debug4(x, y, z, a) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<" "<<#a<<" "<<a<<endl;

#else

#define print(x) 
#define debug(x)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x, y, z, a)

#endif


void solve()
{
    int n;
    cin >> n;
    vi arr(n);
    read(arr);
    int ma = arr[0];
    vpi ans;
    int ind = 0;
    for(int i = 1; i < n; i++) {
        if(arr[i] > arr[i-1]) {
            
        } else {
            int tem = arr[i] + 1;
            while(tem <= ma) ans.push_back({tem, arr[i]}), tem++;
            for(int j = ind; j < i; j++) {
                if(j + 1 == i) break;
                ans.push_back({arr[j+1], arr[j]});
                ans.push_back({arr[j], arr[j+1]});
            }
            ind = i;
        }
        ma = max(ma, arr[i]);
    }
    for(int j = ind; j < n - 1; j++) {
        ans.push_back({arr[j+1], arr[j]});
        ans.push_back({arr[j], arr[j+1]});
    }
    cout << ans.size() << endl;
    for(auto [fir, sec] : ans) cout << fir << " " << sec << endl;
}

int main()
{ 
    suprit;
    clock_t start = clock();

    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
    clock_t end = clock();
    
    #ifndef ONLINE_JUDGE
    double elapsed = double(end - start) / CLOCKS_PER_SEC;
    cout << setprecision(10) << elapsed << endl;
    #endif
    return 0;
}

Submission (verdict: Wrong answer on test 1):
#include <iostream>
#include <vector>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <numeric>
#include <math.h>
#include <cstring>
#include <time.h>
#include <chrono>
#include <random>
#include <ctime>
#include <iomanip>
using namespace std;
#define gc getchar_unlocked
#define fo(i, n) for (i = 0; i < n; i++)
#define Fo(i, k, n) for (i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define ll long long
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define ff second
#define ss first
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x));
#define sortall(x) sort(all(x))
#define tr(it, arr) for (auto it = arr.begin(); it != arr.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
const double pi = 3.141592653589793238;
const ll mod = 1e9+7;

void solve() {
    int n;
    cin >> n;

    vector <vector <int>> counter(n, vector <int> (n, 1));
    vector <int> positionToPlayer(n);
    vector <int> playerToPosition(n);
    vector <int> finalOrder;
    for (int i = 0; i < n; i++) {
        positionToPlayer[i] = i;
        playerToPosition[i] = i; 
    }

    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;

        finalOrder.push_back(x - 1);
    }

    vector <pair <int, int>> operations;

    for (int i = n - 1; i >= 0; i--) {
        int which = finalOrder[i];

        while (playerToPosition[which] != i) {
            int whichPosition = playerToPosition[which];
            operations.push_back({positionToPlayer[whichPosition + 1] + 1, which + 1});
            counter[positionToPlayer[whichPosition + 1]][which]--;
            playerToPosition[which] = whichPosition + 1;
            positionToPlayer[whichPosition] = positionToPlayer[whichPosition + 1];
            playerToPosition[positionToPlayer[whichPosition + 1]] = whichPosition;
            positionToPlayer[whichPosition + 1] = which;
        }

        if (i != n - 1) {
            if (counter[positionToPlayer[i]][positionToPlayer[i + 1]] && counter[positionToPlayer[i + 1]][positionToPlayer[i]]) {
                operations.push_back({positionToPlayer[i] + 1, positionToPlayer[i + 1] + 1});
                operations.push_back({positionToPlayer[i + 1] + 1, positionToPlayer[i] + 1});
                counter[positionToPlayer[i]][positionToPlayer[i + 1]]--;
                counter[positionToPlayer[i + 1]][positionToPlayer[i]]--;
            }
        }
    }

    cout << operations.size() << endl;
    for (auto it: operations) {
        cout << it.first << " " << it.second << endl;
    }
}

int main(){
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}