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


Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
#include <iostream>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
ll MOD = 998244353;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pll> vpll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define rep(i,a,b) for(ll i=(a);i<(b);i++)
#define maxv(v) *max_element(v.begin(), v.end())
#define minv(v) *min_element(v.begin(), v.end())
#define maxa(arr, n) *max_element(arr, arr + (n))
#define mina(arr, n) *min_element(arr, arr + (n))
#define ff first
#define ss second
#define pb push_back
#define pob pop_back()
#define sz size()
#define all(v) v.begin(), v.end()
#define srt(v) sort(all(v))
#define rev(v) reverse(all(v))
#define set_bits(x) __builtin_popcountll(x)
#define lead_zero(x) __builtin_clzll(x)

typedef tree<pll, null_type, less<pll>,
             rb_tree_tag, tree_order_statistics_node_update>
    ospll;
typedef tree<ll, null_type, less<ll>,
             rb_tree_tag, tree_order_statistics_node_update>
    osll;
typedef tree<pll, null_type, less_equal<pll>,
             rb_tree_tag, tree_order_statistics_node_update>
    omspll;
typedef tree<ll, null_type, less_equal<ll>,
             rb_tree_tag, tree_order_statistics_node_update>
    omsll;
class UnionFind
{
private:
  vector<int> p, rank;

public:
  UnionFind(int n)
  {
    rank.assign(n, 0);
    p.assign(n, 0);
    iota(p.begin(), p.end(), 0);
  }

  int findSet(int i) { return (p[i] == i) ? i : p[i] = findSet(p[i]); }

  bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }

  void unionSet(int i, int j)
  {
    if (!isSameSet(i, j))
    {
      int x = findSet(i), y = findSet(j);
      if (rank[x] > rank[y])
        p[y] = x;
      else
      {
        p[x] = y;
        if (rank[x] == rank[y])
          rank[y]++;
      }
    }
  }
};

ll hcf(ll a, ll b)
{
  if (b == 0)
    return a;
  return hcf(b, a % b);
}
vector<ll> fact, invFact;

ll power(ll a, ll b) {
    ll res = 1LL;
    while (b) {
        if (b & 1) {
            res = (res * a) % MOD;
        }
        a = (a * a) % MOD;
        b >>= 1;
    }
    return res;
}

void precomputeFactorials(int maxN) {
    fact.resize(maxN + 1, 1);
    invFact.resize(maxN + 1, 1);
    for (int i = 2; i <= maxN; i++) {
        fact[i] = (fact[i - 1] * i) % MOD;
    }
    invFact[maxN] = power(fact[maxN], MOD - 2);
    for (int i = maxN - 1; i >= 1; i--) {
        invFact[i] = (invFact[i + 1] * (i + 1)) % MOD;
    }
}

ll nCr(ll n, ll r) {
    if (n < r) return 0;
    return (((fact[n] * invFact[r]) % MOD) * invFact[n - r]) % MOD;
}



vector<ll> factors(ll n) // all factors of a number
{
  vector<ll> v;

  for (ll i = 1; i <= sqrtl(n); i++)
  {
    if (n % i == 0)
    {
      if (n / i == i)
      {
        v.push_back(i);
      }

      else
      {
        v.push_back(i);
        v.push_back(n / i);
      }
    }
  }

  sort(v.begin(), v.end());
  return v;
}

vector<ll> z_function(string s)
{
  int n = s.size();
  vector<ll> z(n);
  int l = 0, r = 0;
  for (int i = 1; i < n; i++)
  {
    if (i < r)
    {
      z[i] = min((ll)r - i, z[i - l]);
    }
    while (i + z[i] < n && s[z[i]] == s[i + z[i]])
    {
      z[i]++;
    }
    if (i + z[i] > r)
    {
      l = i;
      r = i + z[i];
    }
  }
  return z;
}

ll prime(ll n) // all prime factors of a number  TC = O(sqrt(n))
{
  map<ll, ll> s;
  while (n % 2 == 0)
  {
    s[2]++;
    n = n / 2;
  }
  for (ll i = 3; i <= sqrtl(n); i = i + 2)
  {
    while (n % i == 0)
    {
      s[i]++;
      n = n / i;
    }
  }
  if (n > 2)
  {
    s[n]++;
  }
  ll ans = 0;
  for (auto c : s)
  {
    ans += c.ss;
  }
  return ans;
}
// scc 
void dfs(vector<ll> adj[], vector<ll> &vis, ll node, stack<ll> &st)
{
    vis[node] = 1;
    for (auto &val : adj[node])
    {
        if (vis[val] == 0)
        {
            dfs(adj, vis, val, st);
        }
    }
 
    st.push(node);
}
 
stack<ll> toposort(vector<ll> adj[], ll n)
{
    stack<ll> st;
 
    vector<ll> vis(n + 1, 0);
 
    for (ll i = 1; i <= n; i++)
    {
        if (vis[i] == 0)
        {
            dfs(adj, vis, i, st);
        }
    }
 
    return st;
}
 
void DFS(vector<ll> trans[], ll node, vector<ll> &vis, vector<ll> &temp)
{
    vis[node] = 1;
    temp.push_back(node);
 
    for (auto &val : trans[node])
    {
        if (vis[val] == 0)
        {
            DFS(trans, val, vis, temp);
        }
    }
}
// {{1 , 2 , 3} , {4 , 5}}
vector<vector<ll>> scc(vector<ll> adj[], ll n)
{
    stack<ll> st = toposort(adj, n);
 
    vector<vector<ll>> v;
 
    vector<ll> trans[n + 1];
 
    for (ll i = 1; i <= n; i++)
    {
        for (auto &val : adj[i])
        {
            trans[val].push_back(i);
        }
    }
 
    vector<ll> vis(n + 1);
 
    while (!st.empty())
    {
        ll node = st.top();
        st.pop();
 
        if (vis[node] == 0)
        {
            vector<ll> temp;
 
            DFS(trans, node, vis, temp);
 
            v.push_back(temp);
        }
    }
 
    return v;
}
map<ll, ll> primeFactorCount(ll n) {
    map<ll, ll> primeFactors;

    // Factor out all 2's
    while (n % 2 == 0) {
        primeFactors[2]++;
        n /= 2;
    }

    // Check for odd factors
    for (ll i = 3; i * i <= n; i += 2) {
        while (n % i == 0) {
            primeFactors[i]++;
            n /= i;
        }
    }

    // If n is still greater than 1, it must be a prime number
    if (n > 1) {
        primeFactors[n]++;
    }
    
    return primeFactors;
}




void solve(){
    ll n;
    cin >> n;
    vl v(n);
    rep(i , 0 , n) cin >> v[i];
    map<pair<ll , ll>  , ll> mp;
    map<ll , ll> done;
    vpll ans;
    rep(i , 0 , n){
        ll j = 1 ;
        while(j < v[i]){
            if(!done[j]){
                ans.pb({v[i] , j});
            }
            j++;
        }
        j++;
        vpll tmp;
        while(j <= n){
            if(!done[j]){
                ans.pb({j , v[i]});
                tmp.pb({j , v[i]});
            }
            j++;
        }
        for(int k=tmp.size()-1 ; k>=0 ; k--){
            pair<ll , ll> p = tmp[k];
            swap(tmp[k].ss , tmp[k].ff);
            ans.pb(tmp[k]);
        }
        done[v[i]] = 1;
    }
    cout << ans.size() << endl;
    for(int i=0 ; i<ans.size() ; i++){
        cout << ans[i].ff << " " << ans[i].ss << endl;
    }
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t=1;
//   cin >> t;
  while (t--)
  {
    solve();
  }
}