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

Previous submission (verdict: Wrong answer on test 7):
#include <bits/stdc++.h>
using namespace std;
//unsigned long long mod = 998244353;
const int mod = (1e9) + 7;
#define MAXN 100000
#define int long long
#define sort(v) sort(v.begin(), v.end())
#define print_with_spaces(v, n) for (unsigned int i = 0; i < n; i++){cout << v[i] << " ";}
#define print_with_lines(v, n) for (unsigned int i = 0; i < n; i++){cout << v[i] << "\n";}
#define itr(i,a,b) for (unsigned int i = a; i < b; i++)
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define is_possible cout << ((possible) ? "YES\n": "NO\n")
#define ull unsigned long long
#define ll long long
#define pb push_back
#define pi pair<unsigned long long, unsigned long long>
#define pii pair<int,int>
#define vi vector<int>
#define mp make_pair
#define read(v, n) for (unsigned int i = 0; i < n; i++){cin>>v[i];}

int32_t main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);

	int n;cin >> n;
	vi a(n + 1);
	for (int i = 1;i <= n;i++)cin >> a[i];
	int c = 0;
	for (int i = 1;i <= n;i++)
		for (int j = i + 1;j <= n;j++){
			if (a[i] > a[j])c++;
}
	int ans = 0;
	for (int i = 1;i <= n;i++) {
		set<int, greater<int>> st;
		for (int j = i + 1;j <= n;j++) if (a[i] > a[j])st.insert(a[j]);
		for (auto& p : st) {
			//cout << a[i] << " " << p << '\n';
			ans++;
		}
	}
	vi temp;

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

		while (k >= 1 && a[k] < a[i]) {
			//cout << a[i] << " " << a[k] << '\n';
			temp.pb(a[k]);
			k--;
			ans += 1;
		}
		while (!temp.empty()) {
			//cout << temp.back() << " " << a[i] << '\n';
			temp.pop_back();
			ans++;
		}
		temp.clear();
	}

	cout << ans << endl;
	for (int i = 1;i <= n;i++) {
		set<int, greater<int>> st;
		for (int j = i + 1;j <= n;j++) if (a[i] > a[j])st.insert(a[j]);
		for (auto& p : st) {
			cout << a[i] << " " << p << '\n';
			//ans++;
		}
	}
	for (int i = n;i >= 2;i--) {
		int k = i-1;
		
		while (k>=1&& a[k ] < a[i]) {
			cout << a[i] << " " << a[k] << '\n';
			temp.pb(a[k]);
			k--;
			//ans += 1;
		}
		while (!temp.empty()) {
			cout << temp.back() << " " << a[i] << '\n';
			temp.pop_back();
			//ans++;
		}
		temp.clear();
	}
		

	return 0;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
//unsigned long long mod = 998244353;
const int mod = (1e9) + 7;
#define MAXN 100000
#define int long long
#define sort(v) sort(v.begin(), v.end())
#define print_with_spaces(v, n) for (unsigned int i = 0; i < n; i++){cout << v[i] << " ";}
#define print_with_lines(v, n) for (unsigned int i = 0; i < n; i++){cout << v[i] << "\n";}
#define itr(i,a,b) for (unsigned int i = a; i < b; i++)
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define is_possible cout << ((possible) ? "YES\n": "NO\n")
#define ull unsigned long long
#define ll long long
#define pb push_back
#define pi pair<unsigned long long, unsigned long long>
#define pii pair<int,int>
#define vi vector<int>
#define mp make_pair
#define read(v, n) for (unsigned int i = 0; i < n; i++){cin>>v[i];}

int32_t main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);

	int n;cin >> n;
	vi a(n + 1);
	for (int i = 1;i <= n;i++)cin >> a[i];
	int c = 0;
	for (int i = 1;i <= n;i++)
		for (int j = i + 1;j <= n;j++){
			if (a[i] > a[j])c++;
}
	int ans = 0;
	for (int i = 1;i <= n;i++) {
		set<int, greater<int>> st;
		for (int j = i + 1;j <= n;j++) if (a[i] > a[j])st.insert(a[j]);
		for (auto& p : st) {
			//cout << a[i] << " " << p << '\n';
			ans++;
		}
	}
	


	cout << n*(n-1)-c << endl;
	for (int i = 1;i <= n;i++) {
		set<int, greater<int>> st;
		for (int j = i + 1;j <= n;j++) if (a[i] > a[j])st.insert(a[j]);
		for (auto& p : st) {
			cout << a[i] << " " << p << '\n';
		}
	}
	auto dfs= [&](auto &&self) {
		int k = n;
		while (k >= 2 && a[k - 1] > a[k])k--;
		if (k == 1) return;

		if (k >= 2 && a[k-1] < a[k]) {
			cout << a[k] << " " << a[k-1] << '\n';
			swap(a[k], a[k-1]);
			self(self);
			swap(a[k], a[k-1]);
			cout << a[k-1] << " " << a[k] << '\n';

			
		} 
		
		};
	dfs(dfs);

		

	return 0;
}