Problem: 2052A - Adrenaline Rush
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 7):
#include<bits/stdc++.h>
using namespace std; 
#define int long long
#define endl '\n'
#define inf 0x3f3f3f3f3f3f3f3f
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N = 1e6+10;
const int mod=998244353;
int h[N];
int a[N],b[N];
void solve() {
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		b[i]=i;
	}
	vector<PII>v;
	map<PII,int>mp;
	if(a[n]==n){
		for(int i=1;i<=n;i++){
			if(a[i]!=b[i]){
//				int j=a[i];
//				for(int l=1;l<=(j-i+1);l++){
//					v.push_back({b[j],b[j-1]});
//					swap(b[j],b[j-1]);
//					j--;
//				}
				int j=n;
				while(1){
					if(b[j]==a[i]){
						break;
					}
					j--;
				}
				for(int l=1;l<=(j-i+1);l++){
					v.push_back({b[j],b[j-1]});
					mp[{b[j],b[j-1]}]++;
					swap(b[j],b[j-1]);
					j--;
				}
			}
		}
		for(int i=n;i>1;i--){
			int j=i;
			while(1){
				PII x = {b[j],b[j-1]};
				PII y = {b[j-1],b[j]};
				if(mp.find(x)!=mp.end()||mp.find(y)!=mp.end()){
					break;
				}
				v.push_back({b[j],b[j-1]});
				swap(b[j],b[j-1]);
				j--;
				if(j==1){
					break;
				}
			}
//			cout<<b[j]<<" ----\n";
			while(1){
				if(j==i){
					break;
				}
				v.push_back({b[j+1],b[j]});
				
				swap(b[j],b[j+1]);
				j++;
				if(j==i){
					break;
				}
			}
		}
	}
	else{
		for(int i=1;i<=n;i++){
			if(a[i]!=b[i]){
				int j=n;
				while(1){
					if(b[j]==a[i]){
						break;
					}
					j--;
				}
				for(int l=1;l<=(j-i+1);l++){
					v.push_back({b[j],b[j-1]});
					mp[{b[j],b[j-1]}]++;
					swap(b[j],b[j-1]);
					j--;
				}
			}
		}
		
		for(int i=n-1;i>1;i--){
			int j=i;
			while(1){
				PII x = {b[j],b[j-1]};
				PII y = {b[j-1],b[j]};
				if(mp.find(x)!=mp.end()||mp.find(y)!=mp.end()){
					break;
				}
				v.push_back({b[j],b[j-1]});
				swap(b[j],b[j-1]);
				j--;
				if(j==1){
					break;
				}
			}
			while(1){
				if(j==i){
					break;
				}
				v.push_back({b[j+1],b[j]});
				swap(b[j],b[j+1]);
				j++;
				if(j==i){
					break;
				}
			}
		}
		
	}
	cout<<v.size()<<endl;
	for(auto l : v){
		cout<<l.first<<" "<<l.second<<endl;
	}
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int T=1;
	//cin>>T;
	while(T--) {
		solve();
	}
	return 0;
}

Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std; 
#define int long long
#define endl '\n'
#define inf 0x3f3f3f3f3f3f3f3f
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N = 1e6+10;
const int mod=998244353;
int h[N];
int a[N],b[N];
void solve() {
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		b[i]=i;
	}
	vector<PII>ans;
	for(int i=1;i<=n;i++){
		vector<int>big,sml;
		for(int j=i+1;j<=n;j++){
			if(a[j]>a[i]){
				big.push_back(a[j]);
			}
			else{
				sml.push_back(a[j]);
			}
		}
		sort(big.begin(),big.end());
		sort(sml.begin(),sml.end(),greater<int>());
		for(int l=0;l<(int)big.size();l++){
			ans.push_back({big[l],a[i]});
		}
		for(int l=big.size()-1;l>=0;l--){
			ans.push_back({a[i],big[l]});
		}
		for(int l=0;l<(int)sml.size();l++){
			ans.push_back({a[i],sml[l]});
		}
	}
	cout<<ans.size()<<endl;
	for(auto w : ans){
		cout<<w.first<<" "<<w.second<<endl;
	}
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int T=1;
	//cin>>T;
	while(T--) {
		solve();
	}
	return 0;
}