Problem: 2052A - Adrenaline Rush
Language: c++17 

Solution 1 (runtime: 1155 ms):
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    vector<int> pos(n+1);
    vector<int> v(n+1);

    for (int i = 1; i <= n; i++)
    {
        int x;
        cin>>x;
        v[i] = x;
        pos[x] = i;
    }

    vector<pair<int,int> > ans;

    for(int i=1;i<=n;i++){
        int car = i;
        stack<pair<int,int> > st1;

        for(int j = pos[car] + 1; j<=n;j++){
            if(v[j] < car){
                // cout<<car<<" : "<<car<<" -> "<<j<<endl;
                st1.push(make_pair(car, v[j]));
            }
        }   

        while(!st1.empty()){
            pair<int,int> p = st1.top();
            st1.pop();
            // cout<<i<<" "<<car<<" -> "<<j<<endl;
            ans.push_back(make_pair(p.first, p.second));
        }



        stack<pair<int,int> > st;

        for(int j = pos[car]-1; j>=1; j--){
            if(v[j] < car){
                // cout<<i<<" "<<car<<" -> "<<j<<endl;
                ans.push_back(make_pair(car, v[j]));
                st.push(make_pair(car, v[j]));
            }
        }

        while(!st.empty()){
            pair<int,int> p = st.top();
            st.pop();
            // cout<<i<<" "<<car<<" -> "<<j<<endl;
            ans.push_back(make_pair(p.second, p.first));
        }

    }

    cout<<ans.size()<<endl;
    for (int i = 0; i < ans.size(); i++)
    {
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    }
    
    return 0;
}

Solution 2 (runtime: 328 ms):
#include <bits/stdc++.h>
using namespace std;

#define sws std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define int long long int
#define float long double
#define ld long double
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define vi vector<int>
#define vpii vector<pair<int, int>> 
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define in(v) for(auto & x : v) cin >> x;
#define out(v) for(auto x : v) cout << x << ' ';
#define tiii tuple<int, int, int>

const int MAXN = 1e6+1;
const int INF = INT64_MAX;
int MOD = 1e9+7;
const int LOG = 31;
const ld PI = acos(-1);
const int MINF = INT32_MIN;
vpii dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

void solve() {

    int n; cin >> n;

    set<int> racers;

    vi ord(n);

    for(int i = 1; i <= n; i++){

        racers.insert(i);

        cin >> ord[i-1];
    }

    reverse(all(ord));

    vpii ans;

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

        vpii mineOvertakes;

        racers.erase(ord[i]);
        for(auto x : racers){

            if(x < ord[i]){

                mineOvertakes.pb({ord[i], x});
            }
        }

        reverse(all(mineOvertakes));
        
        for(auto y : mineOvertakes) {

            ans.pb(y);
        }

        for(auto x : racers)
            ans.pb({x, ord[i]});
    }

    cout << ans.size() << '\n';

    for(auto [x, y] : ans){

        cout << x << " " << y << '\n';
    }
}

int32_t main() { 
    sws;

    int t = 1;
    // cin >> t;

    while(t--)
        solve();

return 0;
}