Problem: 2052A - Adrenaline Rush
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 7):
// bye bye bye

#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int N;
    cin >> N;
    vector<int> A(N);
    for (auto& a : A) {
        cin >> a;
    }
    vector<int> P(N);
    iota(P.begin(), P.end(), 1);
    int first = -1;
    int last = -1;
    vector<pair<int,int>> hmm;
    map<int,int> mp;
    for (int i = 1; i <= N; i++) {
        mp[i] = i - 1;
    }
    int ptr = 0;
    debug(ptr);
    vector<vector<bool>> chk(1001, vector<bool>(1001));
    while (ptr < N) {
        if (P[ptr] != A[ptr]) {
            if (first == -1) {
                first = ptr;
            }
            last = ptr + 1;
            for (int i = mp[A[ptr]]; i > ptr; i--) {
                swap(mp[P[i]], mp[P[i - 1]]);
                hmm.emplace_back(P[i], P[i - 1]);
                chk[P[i]][P[i - 1]] = true;
                chk[P[i - 1]][P[i]] = true;
                swap(P[i], P[i - 1]);
            }
        }
        ++ptr;
    }
    debug(P);
    vector<pair<int,int>> yup;
    ptr = 0;
    while (ptr < N) {
        int last = N - 1;
        int start = ptr;
        for (int i = ptr; i < N; i++) {
            if (i > last) {
                break;
            }
            for (int j = i + 1; j < N; j++) {
                if (j > last) {
                    break;
                }
                if (chk[P[i]][P[j]]) {
                    last = min(last, j - 1);
                }
            }
        }
        // internal swapping
        if (start != last) {
            for (int cnt = 0; cnt < last - start + 1; cnt++) {
                for (int i = last; i > start; i--) {
                    yup.emplace_back(P[i], P[i - 1]);
                    swap(P[i], P[i - 1]);
                }
            }
        }
        ptr = last + 1;
    }
    cout << hmm.size() + yup.size() << '\n';
    for (auto [x,y] : hmm) cout << x << ' ' << y << '\n';
    for (auto [x,y] : yup) cout << x << ' ' << y << '\n';
    return 0;
}

Submission (verdict: Accepted):
// bye bye bye

#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int N;
    cin >> N;
    vector<int> A(N);
    for (auto& a : A) {
        cin >> a;
    }
    vector<pair<int,int>> hmm;
    vector<int> P(N);
    iota(P.begin(), P.end(), 1);
    reverse(P.begin(), P.end());
    map<int,int> mp;
    for (int i = N; i >= 1; i--) {
        for (int j = i - 1; j >= 1; j--) {
            hmm.emplace_back(i, j);
        }
    }
    for (int i = 0; i < N; i++) {
        mp[P[i]] = i;
    }
    int ptr = 0;
    debug(A, P);
    debug(mp);
    while (ptr < N) {
        if (P[ptr] != A[ptr]) {
            for (int i = mp[A[ptr]]; i > ptr; i--) {
                swap(mp[P[i]], mp[P[i - 1]]);
                hmm.emplace_back(P[i], P[i - 1]);
                swap(P[i], P[i - 1]);
            }
        }
        ++ptr;
    }
    debug(hmm);
    cout << hmm.size() << '\n';
    for (auto [x,y] : hmm) cout << x << ' ' << y << '\n';
    return 0;
}