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

Previous submission (verdict: Wrong answer on test 39):
#include <bits/stdc++.h>
using namespace std;
#define int long long

signed main() {
    int n;
    cin >> n;

    vector<int> final_order(n);
    for (int i = 0; i < n; ++i) {
        cin >> final_order[i];
    }

    // vector<int> initial_order(n);
    // for (int i = 0; i < n; ++i) {
        // initial_order[i] = i + 1;
    // }

    vector<pair<int, int>> overtakes;
	vector<bool> mask(n,false);
    for (int i = 0; i < n; ++i) {
    	mask[final_order[i]]=true;
    	for(int j=final_order[i]+1;j<=n;j++){
    		if(mask[j]==false){
    			overtakes.emplace_back(j, final_order[i]);
    		}
    	}
    	for(int j=n;j;j--){
    		if(mask[j]==false){
    			overtakes.emplace_back(final_order[i], j);
    		}
    	}
        // for (int j = 0; j < n - 1; ++j) {
            // if(final_order[j] < final_order[j + 1] && !(st.count({final_order[j] , final_order[j + 1]}))){
            	// overtakes.emplace_back(final_order[j], final_order[j + 1]);
            	// //overtakes.emplace_back(final_order[j + 1], final_order[j]);
            	// st.insert({final_order[j], final_order[j + 1]});
            	// swap(final_order[j], final_order[j + 1]);
            // }
            // else if (final_order[j] > final_order[j + 1] && !(st.count({final_order[j] , final_order[j + 1]}))) {
                // overtakes.emplace_back(final_order[j], final_order[j + 1]);
                // st.insert({final_order[j], final_order[j + 1]});
                // swap(final_order[j], final_order[j + 1]);
            // }
        // }
    }

    cout << overtakes.size() << endl;
    
    for (auto& overtake : overtakes) {
        cout << overtake.first << " " << overtake.second << endl;
    }

    return 0;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
#define int long long

signed main() {
    int n;
    cin >> n;

    vector<int> final_order(n);
    for (int i = 0; i < n; ++i) {
        cin >> final_order[i];
    }

    // vector<int> initial_order(n);
    // for (int i = 0; i < n; ++i) {
        // initial_order[i] = i + 1;
    // }

    vector<pair<int, int>> overtakes;
	vector<bool> mask(n+1,false);
	int cnt=0;
    for (int i = 0; i < n; ++i) {
    	mask[final_order[i]]=true;
    	for(int j=final_order[i]+1;j<=n;j++){
    		if(mask[j]==false){
    			cnt++;
    			overtakes.emplace_back(j, final_order[i]);
    		}
    	}
    	for(int j=n;j;j--){
    		if(mask[j]==false){
    			cnt++;
    			overtakes.emplace_back(final_order[i], j);
    		}
    	}
        // for (int j = 0; j < n - 1; ++j) {
            // if(final_order[j] < final_order[j + 1] && !(st.count({final_order[j] , final_order[j + 1]}))){
            	// overtakes.emplace_back(final_order[j], final_order[j + 1]);
            	// //overtakes.emplace_back(final_order[j + 1], final_order[j]);
            	// st.insert({final_order[j], final_order[j + 1]});
            	// swap(final_order[j], final_order[j + 1]);
            // }
            // else if (final_order[j] > final_order[j + 1] && !(st.count({final_order[j] , final_order[j + 1]}))) {
                // overtakes.emplace_back(final_order[j], final_order[j + 1]);
                // st.insert({final_order[j], final_order[j + 1]});
                // swap(final_order[j], final_order[j + 1]);
            // }
        // }
    }

    cout << cnt << endl;
    
    for (auto& overtake : overtakes) {
        cout << overtake.first << " " << overtake.second << endl;
    }

    return 0;
}