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

Previous submission (verdict: Wrong answer on test 1):
// Problem: A. Adrenaline Rush
// Contest: Codeforces - 2024-2025 ICPC, NERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams
// Preferred) URL: https://codeforces.com/problemset/problem/2052/A Memory Limit: 1024 MB Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <assert.h>
#include <bits/stdc++.h>
using namespace std;
#ifndef __DEBUG__
#define dbg(...) 42
#endif
template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vi = vector<int>;
using a2l = array<ll, 2>;

void solve1()
{
    ll n;
    cin >> n;
    vl a(n), p(n + 1), z(n + 1);
    for (auto &x : a)
        cin >> x;
    for (ll i = 0; i < n; ++i)
        p[a[i]] = i;
    iota(z.begin(), z.end(), 0ll);
    stack<a2l> sa;
    /*
    vector<a2l> ans;
    for (ll i = 1; i <= n; ++i) {
        for (ll j = 1; j + i <= n; ++j) {
            if (p[j] > p[j + i])
                ans.push_back({j, j + i});
            else
                ans.push_back({j, j + i}), ans.push_back({j + i, j});
        }
    }
    */
    function<void(ll, ll)> swap_ele = [&](ll x, ll y) {
        if (x == y)
            return;
        auto &curr = z[y], &prev = z[y - 1];
        if (p[curr] < p[prev]) {
            sa.push({curr, prev});
            swap(curr, prev);
            swap_ele(x, y - 1);
        } else {
            sa.push({y, y - 1});
            swap(curr, prev);
            swap_ele(x, y - 1);

            sa.push({y - 1, y});
            swap(curr, prev);
            // swap_ele(y - 1, y);
        }
    };
    swap_ele(1, n);
    cout << sa.size() << '\n';
    while (sa.empty() == false) {
        auto [x, y] = sa.top();
        sa.pop();
        cout << x << ' ' << y << '\n';
    }
}

void solve()
{
    ll n;
    cin >> n;
    vl a(n), p(n + 1);
    for (auto &x : a)
        cin >> x;
    iota(p.rbegin(), p.rend() - 1, 1ll);
    vector<a2l> ans;

    for (ll i = n; i >= 1; --i)
        for (ll j = i - 1; j >= 1; --j)
            ans.push_back({i, j});

    for (ll i = 1; i <= n; ++i) {
        ll tg = a[i - 1];
        auto id = find(p.begin() + 1, p.end(), tg) - p.begin();
        for (ll i = id; i > tg; --i)
            ans.push_back({p[i], p[i - 1]}), swap(p[i], p[i - 1]);
    }

    cout << ans.size() << '\n';
    for (auto [x, y] : ans)
        cout << x << ' ' << y << '\n';
}

int main(int argc, char **argv)
{
    solve();
    return 0;
};


Submission (verdict: Accepted):
// Problem: A. Adrenaline Rush
// Contest: Codeforces - 2024-2025 ICPC, NERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams
// Preferred) URL: https://codeforces.com/problemset/problem/2052/A Memory Limit: 1024 MB Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <assert.h>
#include <bits/stdc++.h>
using namespace std;
#ifndef __DEBUG__
#define dbg(...) 42
#endif
template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vi = vector<int>;
using a2l = array<ll, 2>;

void solve1()
{
    ll n;
    cin >> n;
    vl a(n), p(n + 1), z(n + 1);
    for (auto &x : a)
        cin >> x;
    for (ll i = 0; i < n; ++i)
        p[a[i]] = i;
    iota(z.begin(), z.end(), 0ll);
    stack<a2l> sa;
    /*
    vector<a2l> ans;
    for (ll i = 1; i <= n; ++i) {
        for (ll j = 1; j + i <= n; ++j) {
            if (p[j] > p[j + i])
                ans.push_back({j, j + i});
            else
                ans.push_back({j, j + i}), ans.push_back({j + i, j});
        }
    }
    */
    function<void(ll, ll)> swap_ele = [&](ll x, ll y) {
        if (x == y)
            return;
        auto &curr = z[y], &prev = z[y - 1];
        if (p[curr] < p[prev]) {
            sa.push({curr, prev});
            swap(curr, prev);
            swap_ele(x, y - 1);
        } else {
            sa.push({y, y - 1});
            swap(curr, prev);
            swap_ele(x, y - 1);

            sa.push({y - 1, y});
            swap(curr, prev);
            // swap_ele(y - 1, y);
        }
    };
    swap_ele(1, n);
    cout << sa.size() << '\n';
    while (sa.empty() == false) {
        auto [x, y] = sa.top();
        sa.pop();
        cout << x << ' ' << y << '\n';
    }
}

void solve()
{
    ll n;
    cin >> n;
    vl a(n), p(n + 1);
    for (auto &x : a)
        cin >> x;
    iota(p.rbegin(), p.rend() - 1, 1ll);
    vector<a2l> ans;

    for (ll i = n; i >= 1; --i)
        for (ll j = i - 1; j >= 1; --j)
            ans.push_back({i, j});

    for (ll i = 1; i <= n; ++i) {
        ll tg = a[i - 1];
        auto id = find(p.begin() + 1, p.end(), tg) - p.begin();

        for (ll j = id; j > i; --j) {
            ans.push_back({p[j], p[j - 1]});
            swap(p[j], p[j - 1]);
        }
    }

    cout << ans.size() << '\n';
    for (auto [x, y] : ans)
        cout << x << ' ' << y << '\n';
}

int main(int argc, char **argv)
{
    solve();
    return 0;
};