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


Submission (verdict: Runtime error on test 28):
#pragma GCC optimize("O3,unroll-loops")
#include<bits/stdc++.h>
// #define int long long
#define endl "\n"
#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
//less_equal to ordered multiset
const int N=1001;
int v[N],a[N];
bool vis[N][N];
int finish[N];
int n;
vector<pair<int,int>> ans;

void check()
{
    for(int i=0;i<n;i++)
    {
        int pos=i;
        int k=pos+1;
        for(;k<n;k++)
        {
            if(finish[a[k]]>finish[a[k-1]] && (!vis[min(a[k-1],a[k])][max(a[k-1],a[k])]))
            {
                vis[min(a[k-1],a[k])][max(a[k-1],a[k])]=true;
                ans.push_back({a[k],a[k-1]});
                swap(a[k],a[k-1]);
                check();
            }
            else
            break;
        }
        k--;
        for(;k>pos;k--)
        {
            swap(a[k],a[k-1]);
            ans.push_back({a[k-1],a[k]});
        }
    }

    for(int i=n-1;i>=0;i--)
    {
        int pos=i;
        int k=pos-1;
        for(;k>=0;k--)
        {
            if(finish[a[k]]<finish[a[k+1]] && (!vis[min(a[k+1],a[k])][max(a[k+1],a[k])]))
            {
                vis[min(a[k+1],a[k])][max(a[k+1],a[k])]=true;
                ans.push_back({a[k+1],a[k]});
                swap(a[k+1],a[k]);
                check();
            }
            else
            break;
        }
        k++;
        for(;k<pos;k++)
        {
            swap(a[k],a[k+1]);
            ans.push_back({a[k],a[k+1]});
        }
    }
}

void solve()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>v[i];
        finish[v[i]]=i;
        a[i]=i+1;
    }
    check();
    for(int i=0;i<n;i++)
    {
        int pos=-1;
        for(int j=0;j<n;j++)
        {
            if(a[j]==v[i])
            {
                pos=j;
                break;
            }
        }
        while(pos>i)
        {
            vis[min(a[pos],a[pos-1])][max(a[pos],a[pos-1])]=true;
            ans.push_back({a[pos],a[pos-1]});
            swap(a[pos],a[pos-1]);
            check();
            pos--;
        }
        int k=pos-1;
        for(;k>=0;k--)
        {
            if(!vis[min(a[k+1],a[k])][max(a[k+1],a[k])])
            {
                vis[min(a[k+1],a[k])][max(a[k+1],a[k])]=true;
                ans.push_back({a[k+1],a[k]});
                swap(a[k+1],a[k]);
                check();
            }
            else
            break;
        }
        k++;
        for(;k<pos;k++)
        {
            swap(a[k],a[k+1]);
            ans.push_back({a[k],a[k+1]});
            // check();
        }        
    }
    // for(int i=1;i<=n;i++)
    // {
    //     for(int j=1;j<=n;j++)
    //     {
    //         cout<<vis[i][j]<<" ";
    //     }
    //     cout<<endl;
    // }
    // for(int i=0;i<n;i++)
    // {
    //     cout<<a[i]<<" ";
    // }
    // cout<<endl;

    int inv = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (a[i] > a[j]) {
          inv++;
        }
      }
    }
    assert((int)ans.size() == n * (n - 1) - inv);
    vector<int> p(n), invp(n);
    for (int i = 0; i < n; i++) {
      p[i] = i + 1;
      invp[i + 1] = i;
    }
    for (auto [x, y] : ans) {
      assert(invp[x] == invp[y] + 1);
      swap(p[invp[x]], p[invp[y]]);
      swap(invp[x], invp[y]);
    }
    for (int i = 0; i < n; i++) {
      assert(p[i] == a[i]);
    }

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

signed main(){
    FastIO;
    // #ifndef ONLINE_JUDGE
    //     freopen("input.txt", "r", stdin);
    //     freopen("output.txt", "w", stdout);
    // #endif
    int t=1;
    // cin>>t;
    for(int i=1;i<=t;i++)
    {
        //cout<<"Case #"<<i<<": ";
        solve();
    }
    return 0;
}