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

Previous submission (verdict: Wrong answer on test 4):
///131111129..........
#include<bits/stdc++.h>
#include <sstream>
using namespace std;
#define ll          long long int
#define pi          acos(-1)
#define pb          push_back
#define mp          make_pair
#define fi          first
#define se          second
#define nl          '\n'
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//#define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update>
//Chess moves.........
 int dx[] = {+0, +0, -1, +1}; ///Up-down, Left-Right// int dy[] = {+1, -1, +0, +0};
 ///int dx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's Move// int dy[] = {-1,+1,+0,+0,+1,+1,-1,-1};
 ///int dx[] = {-2, -2, -1, -1,  1,  1,  2,  2}; ///Knight's Move// int dy[] = {-1,  1, -2,  2, -2,  2, -1,  1};
//Constants........
const ll mod = 998244353;
const ll Max = 3e5 + 5;
const ll inf = 1e18+5;
ll fact[Max];
ll gcd(ll a, ll b){ while (b){ a %= b; swap(a, b);} return a;}
ll lcm(ll a, ll b){ return (a/gcd(a, b)*b);}
ll bigmod(ll a,ll b, ll mod){
    if(b==0)return 1;
    ll x=bigmod(a,b/2, mod);
    x=(x*x)%mod;
    if(b&1)x=(x*(a%mod))%mod;
    return x;
}
ll inverse(ll a, ll M){if(gcd(a, M)==1) return bigmod(a, M-2, M) % M;return 1;}
ll ncr(ll a, ll b, ll mod){ ll x = max(a-b, b), ans=1; for(ll K=a, L=1; K>=x+1; K--, L++){ ans = ans * K % mod; ans = ans * inverse(L, mod) % mod;} return ans;}
ll ncrwithk(ll n,ll k){if(k>n-k) k=n-k;ll p=1,pp=1;if(k!=0){while(k){p*=n;pp*=k;ll m=__gcd(p,pp);p/=m;pp/=m;n--;k--;}}else p=1;return p;}
ll mul(ll a,ll b){ll ans=0ll;while(b){if(b&1) ans+=a;b>>=1;a+=a;if(a>inf) a=inf;if(ans>inf) ans=inf;}return ans;}
void fac(){ fact[0]=fact[1]=1; for(ll i=2;i<Max;i++){ fact[i]=(fact[i-1]*i)%mod; } }
void urmi()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}


int main() {
    //urmi();
    int test = 1, tc = 1;
    //cin >> test;
    for (int i1 = 1; i1 <= test; i1++) {
        int n;
        cin >> n;
        int a[n + 5], p[n + 5];
        for(int i = 1; i <= n ;i++) cin >> a[i];
        for(int i = 1;i <= n; i++) p[i] = i;
        map<pair<int, int>, int> m;
        vector<pair<int, int>>ans;
        for(int i = 1; i <= n; i++){
            if(p[i] == a[i]) continue;
            else{
                int c = 0;
                for(int j = 1; j <= n; j++){
                    if(a[i] == p[j]){
                        c = j;
                        break;
                    }
                }
                for(int j = c; j > i; j--){
                    m[{p[j], p[j - 1]}] = 1;
                    ans.pb({p[j], p[j - 1]});
                    swap(p[j], p[j - 1]);
                }
            }
        }
        for(int i = n; i > 1; i--){
            if(m[{p[i - 1], p[i]}]) continue;
            ans.pb({p[i], p[i - 1]});
            ans.pb({p[i - 1], p[i]});
        }
        cout << ans.size() << nl;
        for(auto i : ans) cout << i.fi << " " << i.se << nl;
    }
    return 0;
}




Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define Fast_IO() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const int N = 1000 + 10;
const int M = 1e9 + 7;
const int inf = 1e12;

int a[N];

void gg(){
    int n; cin >> n;
    vector<int>v(n);
    for(int i=0;i<n;i++){
        cin >> v[i];
        a[v[i]] = i + 1;
    }

    vector<int>ord;
    for(int i=1;i<=n;i++) ord.push_back(i);
    vector<pair<int,int>>ans;
    for(int i=n-1;i>=0;i--){
        int x = v[i];
        int pos;
        for(int j=0;j<n;j++){
            if(ord[j] == x){
                pos = j;
            }
        }

        // overtake fronts
        for(int j=pos-1;j>=0;j--){
            ans.push_back({x, ord[j]});
        }
        // reverse overtake.
        vector<int>nord;
        vector<int>vis(n, 0);
        for(int j=0;j<n;j++){
            if(j == pos) continue;
            int y = ord[j];
            int xf = a[x];
            int yf = a[y];

            if(xf > yf){
                ans.push_back({y, x});
                nord.push_back(y);
                vis[j] = 1;
            }
        }
        nord.push_back(x);
        for(int j=0;j<n;j++){
            if(j == pos or vis[j] == 1) continue;
            nord.push_back(ord[j]);
        }
        // cout << x << " " << ans.size() << " -- ";
        // for(auto i : nord) {
        //     cout << i << " ";
        // } cout << endl;
        ord = nord;
    }

    cout << ans.size() << endl;
    for(auto i : ans){
        cout << i.first << " " << i.second << endl;
    }
}

/* void solve(){

    int n; cin >> n;
    vector<int>v(n);
    for(int i=1;i<=n;i++){
        cin >> v[i];
        a[v[i]] = i;
    }
    vector<pair<int,int>>ans;
    // for(int i=n;i>=1;i--){
    //     int x = v[i];
    //     for(int y=x-1;y>=1;y--){
    //         ans.push_back({x, y});
    //     }
    // }
    // vector<pair<int,int>>ans;
    for(int i=n;i>=1;i--){
        for(int j=i-1;j>=1;j--){
            ans.push_back({i, j});
        }
        for(int j=i-1;j>=1;j--){
            int x = a[i];
            int y = a[j];

            if(y < x){
                ans.push_back({j, i});
            }
        }
    }

    cout << ans.size() << endl;
    for(auto i : ans){
        cout << i.first << " " << i.second << endl;
    }

} */


signed main(){
    Fast_IO()
    int t = 1;
    // cin >> t;
    for(int i=1;i<=t;i++){
        // cout << "Case #" << i << ": ";
        gg();
    }
}