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

Previous submission (verdict: Time limit exceeded on test 29):
#include <bits/stdc++.h>
using namespace std;

#define mod 1000000007

// We will replace the unordered_set<pair<int,int>> with a 2D boolean array.
// st[x][y] = true means we have already seen an overtake (x,y).
static bool st[1001][1001];

struct PairHash {
    template <typename T1, typename T2>
    size_t operator()(const pair<T1, T2>& p) const {
        return hash<T1>()(p.first) ^ (hash<T2>()(p.second) << 1);
    }
};

void solve() {
    int n;
    cin >> n;
    vector<int> a(n + 1);
    vector<int> mp(n + 1);

    for (int i = 1; i <= n; i++)
        a[i] = i;

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

    // Initialize st array
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            st[i][j] = false;

    // Perform the given logic
    for (int i = 1; i <= n; i++) {
        int k = mp[a[i]] - i;
        int val2 = a[i];
        if (k <= 0) continue;

        int val = i;
        while (k--) {
            // a[val] = a[val+1]
            // We're effectively doing a swap with the next element
            int nextCar = a[val + 1];
            if (st[nextCar][val2]) {
                // If nextCar overtook val2 is already recorded, remove it
                // Means we had a pair (nextCar, val2) before
                st[nextCar][val2] = false;
            } else {
                // Record the pair (val2, nextCar)
                st[val2][nextCar] = true;
            }

            // Swap
            int temp = a[val];
            a[val] = a[val + 1];
            a[val + 1] = temp;

            val++;
        }
        a[mp[val2]] = val2;
        i--;
    }

    vector<int> vec(n + 1);
    for (int i = 1; i <= n; i++)
        vec[i] = i;

    vector<pair<int,int>> ans;
    for (int i = 2; i <= n; i++) {
        int val = vec[i];
        for (int j = i - 1; j >= 1; j--) {
            ans.push_back({val, vec[j]});
            vec[j + 1] = vec[j];
        }
        vec[1] = val;

        for (int j = 2; j <= i; j++) {
            if (st[vec[j]][val]) {
                vec[j - 1] = val;
                break;
            }
            ans.push_back({vec[j], val});
            vec[j - 1] = vec[j];
            if (j == i)
                vec[j] = val;
        }
    }

    cout << (int)ans.size() << "\n";
    for (auto &p : ans) {
        cout << p.first << " " << p.second << "\n";
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int test = 1;
    // cin >> test; // If multiple test cases are needed, uncomment this.
    while (test--) {
        solve();
    }
    return 0;
}


Submission (verdict: Accepted):

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

#define mod 1000000007
void solve() {
    int n;
    cin >> n;
    vector<int> vec(n + 1);
    vector<int> mp(n + 1);

    for (int i = 1; i <= n; i++)
        vec[i] = i;

    for (int i = 1; i <= n; i++) {
        int x; cin >> x;
        mp[x] = i;
    }
    map<int,int> curr;
    for(int i=1;i<=n;i++)
    curr[i]=i;
    vector<pair<int,int>> ans;
    for(int k=1;k<=n;k++)
    {
        int i=curr[k];
        int val = vec[i];
        for (int j = i - 1; j >= 1; j--) {
            ans.push_back({val, vec[j]});
            swap(vec[j],vec[j+1]);
        }
        for (int j = 2; j <= n; j++) {
            if(mp[vec[j]]>mp[val]) break;
            ans.push_back({vec[j], val});
            swap(vec[j-1],vec[j]);
        }
        for(int l=1;l<=n;l++)
        {
          curr[vec[l]]=l;
        }
    }

    cout << (int)ans.size() << "\n";
    for (auto &p : ans) {
        cout << p.first << " " << p.second << "\n";
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int test = 1;
    // cin >> test; // If multiple test cases are needed, uncomment this.
    while (test--) {
        solve();
    }
    return 0;
}