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

Previous submission (verdict: Wrong answer on test 1):
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <climits>
#include <numeric>
#include <iomanip>
#include <fstream>
#include <string>
#include <algorithm>
#include <cmath>
#include <bitset>
#include <utility>
#include <array>
#include <vector>
#include <deque>
#include <forward_list>
#include <list>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <functional>
#include <popcntintrin.h>
#include <ext/pb_ds/assoc_container.hpp>

// LLONG_MAX is 9,223,372,036,854,775,807 (19 digits)
// ULLONG_MAX is 18,446,744,073,709,551,615 (20 digits)

using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_multiset = tree<std::pair<T, T>, null_type, std::less<std::pair<T, T>>, rb_tree_tag, tree_order_statistics_node_update>;

// use a map to keep track of duplicates in ordered multiset, like 2-1, 2-2, 2-3, and so on

using ll = long long;
using ld = long double;
using namespace std;

void solve()
{
    ll n;
    cin >> n;
    
    vector<ll> c(n);
    set<ll> st;
    
    for (ll i = 0; i < n; i++)
    {
        cin >> c[i];
        st.insert(i + 1);
    }
    
    vector<pair<ll, ll>> events;
    
    while (!c.empty())
    {
        ll a = c.back();
        c.pop_back();
        
        for (auto &x : st)
        {
            if (x == a)
            {
                break;
            }
            
            events.emplace_back(a, x);
        }
        
        st.erase(a);
        
        auto it = st.end();
        
        while (it != st.begin())
        {
            it = prev(it);
            
            events.emplace_back(*it, a);
        }
    }
    
    cout << (ll) events.size() << '\n';
    
    for (auto &[x, y] : events)
    {
        cout << x << " " << y << '\n';
    }
}

// Increase Stack Size for Meta HackerCup

// static void run_with_stack_size(void (*func)(void), size_t stsize)
// {
//     char *stack, *send;
//     stack = (char *)malloc(stsize);
//     send = stack + stsize - 16;
//     send = (char *)((uintptr_t)send / 16 * 16);
//     asm volatile(
//         "mov %%rsp, (%0)\n"
//         "mov %0, %%rsp\n"
//         :
//         : "r"(send));
//     func();
//     asm volatile("mov (%0), %%rsp\n"
//                  :
//                  : "r"(send));
//     free(stack);
// }

int main()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(nullptr);

	// USACO
	// freopen("whereami.in", "r", stdin);
	// freopen("whereami.out", "w", stdout);

	// Codeforces, AtCoder

	ll ncases = 1;
// 	cin >> ncases;

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

	// Meta HackerCup

	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);

	// int ncases = 1;
	// cin >> ncases;

	// for (int i = 1; i <= ncases; i++)
	// {
	//     std::cout << "Case #" << i << ": ";
	//     run_with_stack_size(solve, 1024 * 1024 * 1024); // run with a 1 GiB stack
	// }

	return 0;
}

Submission (verdict: Accepted):
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <climits>
#include <numeric>
#include <iomanip>
#include <fstream>
#include <string>
#include <algorithm>
#include <cmath>
#include <bitset>
#include <utility>
#include <array>
#include <vector>
#include <deque>
#include <forward_list>
#include <list>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <functional>
#include <popcntintrin.h>
#include <ext/pb_ds/assoc_container.hpp>

// LLONG_MAX is 9,223,372,036,854,775,807 (19 digits)
// ULLONG_MAX is 18,446,744,073,709,551,615 (20 digits)

using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_multiset = tree<std::pair<T, T>, null_type, std::less<std::pair<T, T>>, rb_tree_tag, tree_order_statistics_node_update>;

// use a map to keep track of duplicates in ordered multiset, like 2-1, 2-2, 2-3, and so on

using ll = long long;
using ld = long double;
using namespace std;

void solve()
{
    ll n;
    cin >> n;
    
    vector<ll> c(n);
    set<ll> st;
    
    for (ll i = 0; i < n; i++)
    {
        cin >> c[i];
        st.insert(i + 1);
    }
    
    vector<pair<ll, ll>> events;
    
    while (!c.empty())
    {
        ll a = c.back();
        c.pop_back();
        
        auto it = st.find(a);
        
        while (it != st.begin())
        {
            it = prev(it);
            events.emplace_back(a, *it);
        }
        
        st.erase(a);
        
        for (auto &x : st)
        {
            events.emplace_back(x, a);
        }
    }
    
    cout << (ll) events.size() << '\n';
    
    for (auto &[x, y] : events)
    {
        cout << x << " " << y << '\n';
    }
}

// Increase Stack Size for Meta HackerCup

// static void run_with_stack_size(void (*func)(void), size_t stsize)
// {
//     char *stack, *send;
//     stack = (char *)malloc(stsize);
//     send = stack + stsize - 16;
//     send = (char *)((uintptr_t)send / 16 * 16);
//     asm volatile(
//         "mov %%rsp, (%0)\n"
//         "mov %0, %%rsp\n"
//         :
//         : "r"(send));
//     func();
//     asm volatile("mov (%0), %%rsp\n"
//                  :
//                  : "r"(send));
//     free(stack);
// }

int main()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(nullptr);

	// USACO
	// freopen("whereami.in", "r", stdin);
	// freopen("whereami.out", "w", stdout);

	// Codeforces, AtCoder

	ll ncases = 1;
// 	cin >> ncases;

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

	// Meta HackerCup

	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);

	// int ncases = 1;
	// cin >> ncases;

	// for (int i = 1; i <= ncases; i++)
	// {
	//     std::cout << "Case #" << i << ": ";
	//     run_with_stack_size(solve, 1024 * 1024 * 1024); // run with a 1 GiB stack
	// }

	return 0;
}