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

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> ns;
vector<int> cur;
vector<int> pos;
vector<int> x;
vector<vector<bool>> v;
bool d = false;

void f(int it, int y, int j){
    int co = 0;
    it--;
    if(it != - 1){
        while((cur[it] <= j && !v[cur[it]][cur[it+1]] && !v[cur[it+1]][cur[it]]) || (d && it >= y)){
            v[cur[it+1]][cur[it]] = true;
            ns.push_back({cur[it+1], cur[it]});
            swap(cur[it], cur[it+1]);
            it--;
            if(it == -1){
                break;
            }
        }
    }
    it++;
    while(it < y){
        v[cur[it+1]][cur[it]] = true;
        ns.push_back({cur[it+1], cur[it]});
        swap(cur[it], cur[it+1]);
        it++;
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n; cin >> n;
    v.resize(n+1, vector<bool>(n+1));
    x.resize(n);
    pos.resize(n+1);
    for(int i = 0; i < n; i++){
        cin >> x[i];
        pos[x[i]] = i;
    }
    cur.resize(n);
    for(int i = 0; i < n; i++){
        cur[i] = i+1;
    }
    for(int y = 0; y < n; y++){
        int it = 0;
        for(int i = 0; i < n; i++){
            if(cur[i] == x[y]){
                it = i;
                break;
            }
        }
        for(int i = y; i <= it; i++){
            d = false;
            if(i == it){
                d = true;
            }
            f(i, y, cur[i]);
        }
        cout << ns.size() << "\n";
        for(int i = 0; i < n; i++){
            cout << cur[i] << " ";
        }cout << "\n";
    }
    cout << ns.size() << "\n";
    for(int y = 0; y < ns.size(); y++){
        cout << ns[y].first << " " << ns[y].second << "\n";
    }
    return 0;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> ns;
vector<int> cur;
vector<int> pos;
vector<int> x;
vector<vector<bool>> v;
bool d = false;

void f(int it, int y, int j){
    int co = 0;
    it--;
    if(it != - 1){
        while((cur[it] <= j && !v[cur[it]][cur[it+1]] && !v[cur[it+1]][cur[it]]) || (d && it >= y)){
            v[cur[it+1]][cur[it]] = true;
            ns.push_back({cur[it+1], cur[it]});
            swap(cur[it], cur[it+1]);
            it--;
            if(it == -1){
                break;
            }
        }
    }
    it++;
    while(it < y){
        v[cur[it+1]][cur[it]] = true;
        ns.push_back({cur[it+1], cur[it]});
        swap(cur[it], cur[it+1]);
        it++;
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n; cin >> n;
    v.resize(n+1, vector<bool>(n+1));
    x.resize(n);
    pos.resize(n+1);
    for(int i = 0; i < n; i++){
        cin >> x[i];
        pos[x[i]] = i;
    }
    cur.resize(n);
    for(int i = 0; i < n; i++){
        cur[i] = i+1;
    }
    for(int y = 0; y < n; y++){
        int it = 0;
        for(int i = 0; i < n; i++){
            if(cur[i] == x[y]){
                it = i;
                break;
            }
        }
        for(int i = y; i <= it; i++){
            d = false;
            if(i == it){
                d = true;
            }
            f(i, y, cur[i]);
        }
    }
    cout << ns.size() << "\n";
    for(int y = 0; y < ns.size(); y++){
        cout << ns[y].first << " " << ns[y].second << "\n";
    }
    return 0;
}

//3 1 4 2
//1 2 3 4
//2 1 3 4
//2 3 1 4
//3 2 1 4
//3 1 2 4
//3 1 4 2
//3 4 1 2
//4 3 1 2
//3 4 1 2
//3 1 4 2