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


Submission (verdict: Wrong answer on test 7):
#include <bits/stdc++.h>
using namespace std;

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

#define MOD 1000000007
#define HASH_MOD1 998244353
#define HASH_MOD2 1000000009
#define HASH 31
#define ll long long
#define int long long

typedef __gnu_pbds::tree<int, __gnu_pbds::null_type, less<int>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> ordered_set;

template <typename T>
istream& operator>> (istream& in, vector<T>& a)
{
    for (int i = 0; i < a.size(); ++i)
        in >> a[i];

    return in;
}

template <typename T>
ostream& operator<< (ostream& out, vector<T>& a)
{
    for (int i = 0; i < a.size(); ++i)
        out << a[i] << " ";

    out << "\n";

    return out;
}

template <typename T>
ostream& operator<< (ostream& out, set<T>& s)
{
    for (int x : s)
        out << x << " ";

    out << "\n";

    return out;
}

template <typename T>
ostream& operator<< (ostream& out, multiset<T>& s)
{
    for (int x : s)
        out << x << " ";

    out << "\n";

    return out;
}

template <typename T>
ostream& operator<< (ostream& out, pair<T, T>& p)
{
    out << p.first << " " << p.second << "\n";

    return out;
}

template <typename T>
ostream& operator<< (ostream& out, vector<pair<T, T>>& v)
{
    for (int i = 0; i < v.size(); ++i)
        out << v[i].first << " " << v[i].second << "\n";

    return out;
}

ll modinv(ll a)
{
    return a <= 1 ? a : MOD - (MOD / a) * modinv(MOD % a) % MOD; 
}

ll binpow(ll x, ll y)
{
    ll ans = 1;

    while (y > 0)
    {
        if (y % 2 == 1)
            ans *= x;

        x = x * x;
        y >>= 1;

        x %= MOD;
        ans %= MOD;
    }

    return ans;
}

void solve()
{
    int n, i;
    cin >> n;

    vector<int> target(n);
    cin >> target;

    vector<vector<bool>> visited(n + 5, vector<bool>(n + 5, false));
    vector<int> pos(n + 5, -1), a(n + 5);

    for (i = 0; i < n; ++i)
    {
        pos[target[i]] = i + 1;
        a[i + 1] = i + 1;
    }

    int moves = 0;
    vector<pair<int, int>> ans;

    for (i = n; i >= 1; --i)
    {
        int curr = -1;

        for (int j = 1; j <= n; ++j)
        {
            if (a[j] == i)
            {
                curr = j;
                break;
            }
        }

        while (curr > 1 && a[curr - 1] < i && pos[a[curr - 1]] < pos[i])
        {
            ans.push_back({i, a[curr - 1]});
            swap(a[curr], a[curr - 1]);
            --curr;

            ++moves;
        }

        while (curr < pos[i])
        {
            ans.push_back({a[curr + 1], i});
            swap(a[curr], a[curr + 1]);
            ++curr;

            ++moves;
        }

        while (curr > pos[i])
        {
            ans.push_back({i, a[curr - 1]});
            swap(a[curr], a[curr - 1]);
            --curr;

            ++moves;
        }

        // cout << "i = " << i << " completed ";
        // cout << "a = " << a;
        // cout.flush();
    }

   // cout << "a = " << a;

    cout << moves << "\n";
    cout << ans;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    //cin >> t;

    while (t--)
    {
        solve();
    }

    return 0;
}