Problem: 2052A - Adrenaline Rush
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 7):

// Author : Sarbojit Rana
// LinkTree : https://linktr.ee/sarbojit_rana


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
typedef vector<vector<pll>> vvpll;
typedef vector<pll> vpll;
typedef vector<pii> vpii;
typedef map<int, int> mii;
typedef map<ll,ll> mll;

const ll MOD = 998244353;
const double eps = 1e-12;

#define forn(i,e) for(ll i = 0; i < e; i++)
#define forsn(i,s,e) for(ll i = s; i < e; i++)
#define rforn(i,s) for(ll i = s; i >= 0; i--)
#define rforsn(i,s,e) for(ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define yes cout << "YES\n" ;
#define no cout << "NO\n" ;
#define iv(v,n) vector<ll> v(n); forn(i,n) cin >> v[i] ;


// unique sort :
// sort(all(v));
// v.resize(unique(all(v)) - v.begin()) ;

// Mathematical functions
ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);} //__gcd 


ll lcm(ll a, ll b){return (a/gcd(a,b)*b);}

ll moduloMultiplication(ll a,ll b,ll mod){ll res = 0;a %= mod;while (b){if (b & 1)res = (res + a) % mod;b >>= 1;}return res;}

ll powermod( ll x, ll y, ll mod){ ll ans = 1;if( y == 0) return 1;while(y){   if(y%2) ans = (1ll*ans*x)%mod;    x = (1ll*x*x)%mod;    y /= 2;}return ans;}

ll binexp(ll a , ll b , ll m){ll result = 1;while(b > 0){if( b& 1){result = ((result%m )*( a%m))%m ;}a = ((a%m)*(a%m))%m ;b >>= 1;}return result;}


vector<vector<long long>> matrixmultiply(const vector<vector<long long>>& matrix1, const vector<vector<long long>>& matrix2) {
    int rows = matrix1.size();
    int cols = matrix2[0].size();
    int common = matrix2.size();
    vector<vector<long long>> result(rows, vector<long long>(cols, 0));

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            for (int k = 0; k < common; k++) {
                result[i][j] += matrix1[i][k] * matrix2[k][j];
            }
        }
    }
    return result;
}



//Factorial by vector
//vll fact;
//void factorials(ll mod, ll maxi){fact.resize(maxi + 1, 1); for(int i = 2; i<= maxi ; i++) fact[i] = (fact[i-1]*i)%mod ;}
//ll nCr(ll n, ll r, ll mod){ if(r > n) return 0; ll x = (fact[r]*fact[n-r])%mod ;ll ans = (fact[n] * (powermod(x,mod-2, mod)))%mod;return ans;}
bool isPrime(ll n){if(n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(int i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;}

bool isPowerOfTwo(int n){if(n==0)return false;return (ceil(log2(n)) == floor(log2(n)));}

bool isPerfectSquare(ll x){if (x >= 0) {ll sr = sqrt(x);return (sr * sr == x);}return false;}




int main() {
    fast_cin();
    ll n;
    cin >> n;
    vi v(n, 0), og(n, 0);
    vvi rec(n+1, vi(n+1, 0));
    vpii ans;
    map<int,int> mp;
    forn(i, n){
        og[i] = i+ 1;
        mp[og[i]] = i;
    }
    forn(i, n){
        cin >> v[i];
        int pos = mp[v[i]] ;
        for(int j = pos ; j> i ; j--){
            rec[og[j]][og[j-1]]++;
            rec[og[j-1]][og[j]]++;
            ans.push_back({og[j], og[j-1]});
            mp[og[j]] = j-1;
            mp[og[j-1]] = j;
            swap(og[j], og[j-1]);
        }
        pos = i;
        while((pos > 0) && (rec[og[pos]][og[pos-1]] == 0)){
            ans.push_back({og[pos], og[pos-1]});
            rec[og[pos]][og[pos-1]]++;
            rec[og[pos-1]][og[pos]]++;
            mp[og[pos]] = pos-1;
            mp[og[pos-1]] = pos;
            swap(og[pos], og[pos-1]);
            pos--;
        }
        while(pos < i ){
            ans.push_back({og[pos+1], og[pos]});
            rec[og[pos]][og[pos+1]]++;
            rec[og[pos+1]][og[pos]]++;
            mp[og[pos]] = pos+1;
            mp[og[pos+1]] = pos;
            swap(og[pos], og[pos+1]);
            pos++;
        }
    }
    cout << ans.size() << endl;
    forn(i, ans.size()) cout << ans[i].first << " " << ans[i].second << endl;
    return 0;
}

Submission (verdict: Accepted):

// Author : Sarbojit Rana
// LinkTree : https://linktr.ee/sarbojit_rana


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
typedef vector<vector<pll>> vvpll;
typedef vector<pll> vpll;
typedef vector<pii> vpii;
typedef map<int, int> mii;
typedef map<ll,ll> mll;

const ll MOD = 998244353;
const double eps = 1e-12;

#define forn(i,e) for(ll i = 0; i < e; i++)
#define forsn(i,s,e) for(ll i = s; i < e; i++)
#define rforn(i,s) for(ll i = s; i >= 0; i--)
#define rforsn(i,s,e) for(ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define yes cout << "YES\n" ;
#define no cout << "NO\n" ;
#define iv(v,n) vector<ll> v(n); forn(i,n) cin >> v[i] ;


// unique sort :
// sort(all(v));
// v.resize(unique(all(v)) - v.begin()) ;

// Mathematical functions
ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);} //__gcd 


ll lcm(ll a, ll b){return (a/gcd(a,b)*b);}

ll moduloMultiplication(ll a,ll b,ll mod){ll res = 0;a %= mod;while (b){if (b & 1)res = (res + a) % mod;b >>= 1;}return res;}

ll powermod( ll x, ll y, ll mod){ ll ans = 1;if( y == 0) return 1;while(y){   if(y%2) ans = (1ll*ans*x)%mod;    x = (1ll*x*x)%mod;    y /= 2;}return ans;}

ll binexp(ll a , ll b , ll m){ll result = 1;while(b > 0){if( b& 1){result = ((result%m )*( a%m))%m ;}a = ((a%m)*(a%m))%m ;b >>= 1;}return result;}


vector<vector<long long>> matrixmultiply(const vector<vector<long long>>& matrix1, const vector<vector<long long>>& matrix2) {
    int rows = matrix1.size();
    int cols = matrix2[0].size();
    int common = matrix2.size();
    vector<vector<long long>> result(rows, vector<long long>(cols, 0));

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            for (int k = 0; k < common; k++) {
                result[i][j] += matrix1[i][k] * matrix2[k][j];
            }
        }
    }
    return result;
}



//Factorial by vector
//vll fact;
//void factorials(ll mod, ll maxi){fact.resize(maxi + 1, 1); for(int i = 2; i<= maxi ; i++) fact[i] = (fact[i-1]*i)%mod ;}
//ll nCr(ll n, ll r, ll mod){ if(r > n) return 0; ll x = (fact[r]*fact[n-r])%mod ;ll ans = (fact[n] * (powermod(x,mod-2, mod)))%mod;return ans;}
bool isPrime(ll n){if(n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(int i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;}

bool isPowerOfTwo(int n){if(n==0)return false;return (ceil(log2(n)) == floor(log2(n)));}

bool isPerfectSquare(ll x){if (x >= 0) {ll sr = sqrt(x);return (sr * sr == x);}return false;}




int main() {
    fast_cin();
    ll n;
    cin >> n;
    vi v(n, 0), og(n, 0);
    vvi rec(n+1, vi(n+1, 0));
    vpii ans;
    map<int,int> mp;
    forn(i, n){
        og[i] = i+ 1;
        mp[og[i]] = i;
    }
    forn(i, n){
        cin >> v[i];
        int pos = mp[v[i]] ;
        int p = pos ;
        while((p < n-1) && (rec[og[p]][og[p+1]] == 0)){
            ans.push_back({og[p+1], og[p]});
            rec[og[p]][og[p+1]]++;
            rec[og[p+1]][og[p]]++;
            mp[og[p]] = p+1;
            mp[og[p+1]] = p;
            swap(og[p], og[p+1]);
            p++;
        }
        while(p > pos ){
            ans.push_back({og[p], og[p-1]});
            rec[og[p]][og[p-1]]++;
            rec[og[p-1]][og[p]]++;
            mp[og[p]] = p-1;
            mp[og[p-1]] = p;
            swap(og[p], og[p-1]);
            p--;
        }
        for(int j = pos ; j> i ; j--){
            rec[og[j]][og[j-1]]++;
            rec[og[j-1]][og[j]]++;
            ans.push_back({og[j], og[j-1]});
            mp[og[j]] = j-1;
            mp[og[j-1]] = j;
            swap(og[j], og[j-1]);
        }
        pos = i;
        while((pos > 0) && (rec[og[pos]][og[pos-1]] == 0)){
            ans.push_back({og[pos], og[pos-1]});
            rec[og[pos]][og[pos-1]]++;
            rec[og[pos-1]][og[pos]]++;
            mp[og[pos]] = pos-1;
            mp[og[pos-1]] = pos;
            swap(og[pos], og[pos-1]);
            pos--;
        }
        while(pos < i ){
            ans.push_back({og[pos+1], og[pos]});
            rec[og[pos]][og[pos+1]]++;
            rec[og[pos+1]][og[pos]]++;
            mp[og[pos]] = pos+1;
            mp[og[pos+1]] = pos;
            swap(og[pos], og[pos+1]);
            pos++;
        }
    }
    cout << ans.size() << endl;
    forn(i, ans.size()) cout << ans[i].first << " " << ans[i].second << endl;
    return 0;
}