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

Previous submission (verdict: Wrong answer on test 7):
#include <bits/stdc++.h>
using namespace std;
 
#ifdef LOCAL
#include "debug.h"
#else
#define dbg(...)
#endif


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

//find_by_order(k): It returns to an iterator to the kth element (counting from zero)
//order_of_key(k): order_of_key(k) : It returns to the number of items that are strictly smaller than our item k
template<typename T>
using ordered_set = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;

// https://codeforces.com/problemset/problem/2052/A
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> c(n+1), idx(n+1), at(n+1), limit(n+1, n+1);
    ordered_set<int> os;
    for (int i = 1; i <= n; i++){
        cin >> c[i];
        at[c[i]] = i;
    }
    iota(idx.begin(), idx.end(), 0);
    set<pair<int,int>> seen;
    vector<pair<int,int>> ans;
    for (int i = n; i >= 1;i--){
        int curr = c[i] - os.order_of_key(c[i]);
        assert(curr <= at[c[i]] && idx[curr] == c[i]);
        int it = at[c[i]] - curr;
        //move to correct position
        while(it--){
            limit[idx[curr+1]] = min(limit[idx[curr+1]], at[c[i]]);
            ans.push_back({idx[curr+1], idx[curr]});
            swap(idx[curr], idx[curr+1]);
            curr++;
        }
        //at correct position
        assert(curr == at[c[i]]);
        //swap with values that we haven't swapped with before
        for (int j = curr; j + 1 <= limit[c[i]]-1; j++){
            ans.push_back({idx[j+1], idx[j]});
            swap(idx[j+1], idx[j]);
        }
        for (int j = limit[c[i]]-1; j-1 >= curr; j--){
            ans.push_back({idx[j], idx[j-1]});
            swap(idx[j], idx[j-1]);
        }
        os.insert(c[i]);
    }
    cout << ans.size() << '\n';
    for (auto &[x,y] : ans){
        cout << x << " " << y << '\n';
    }
    return 0;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
 
#ifdef LOCAL
#include "debug.h"
#else
#define dbg(...)
#endif


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

//find_by_order(k): It returns to an iterator to the kth element (counting from zero)
//order_of_key(k): order_of_key(k) : It returns to the number of items that are strictly smaller than our item k
template<typename T>
using ordered_set = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;

// https://codeforces.com/problemset/problem/2052/A
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> c(n+1), idx(n+1), at(n+1), limit(n+1, n+1);
    ordered_set<int> os;
    for (int i = 1; i <= n; i++){
        cin >> c[i];
        at[c[i]] = i;
    }
    iota(idx.begin(), idx.end(), 0);
    set<pair<int,int>> seen;
    vector<pair<int,int>> ans;
    for (int i = n; i >= 1;i--){
        int curr = c[i] - os.order_of_key(c[i]);
        assert(curr <= at[c[i]] && idx[curr] == c[i]);
        
        //move all the way up
        for (int j = curr; j - 1 >= 1; j--){
            ans.push_back({idx[j], idx[j-1]});
            swap(idx[j], idx[j-1]);
        }
        int it = at[c[i]] - 1;
        curr = 1;
        //move to correct position
        while(it--){
            limit[idx[curr+1]] = min(limit[idx[curr+1]], at[c[i]]);
            ans.push_back({idx[curr+1], idx[curr]});
            swap(idx[curr], idx[curr+1]);
            curr++;
        }
        //at correct position
        assert(curr == at[c[i]]);
        //swap with values that we haven't swapped with before
        for (int j = curr; j + 1 <= limit[c[i]]-1; j++){
            ans.push_back({idx[j+1], idx[j]});
            swap(idx[j+1], idx[j]);
        }
        for (int j = limit[c[i]]-1; j-1 >= curr; j--){
            ans.push_back({idx[j], idx[j-1]});
            swap(idx[j], idx[j-1]);
        }
        os.insert(c[i]);
    }
    cout << ans.size() << '\n';
    for (auto &[x,y] : ans){
        cout << x << " " << y << '\n';
    }
    return 0;
}