Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 374 ms):
I = lambda : list(map(int, input().split(' ')))
R = lambda : (int(input()))

import sys
import math
from collections import deque, defaultdict
from bisect import bisect_left, bisect_right

for kp in range(1):

    n = R()
    a = I()

    c = [-1 for i in range(n+1)]
    for i,x in enumerate(a): c[x] = i+1

    initial = [(c[i],i) for i in range(1,n+1)]
    overtakes = []

    all_overtakes = set()
    new_overtakes = []

    # for i in range(1,n+1):
    #     for j in range(i+1,n+1):
    #         all_overtakes.add((i,j))
    #         all_overtakes.add((j,i))

    final = []

    for i in range(n):

        pos, curr = initial[i]
        insert_pos = i

        for j in range(i-1,-1,-1):

            if final[j][0] > pos:
                insert_pos -= 1
                # all_overtakes.remove( (final[j][1], curr) )
                new_overtakes.append(f"{curr} {final[j][1]}")

            else: break

        for j in range(insert_pos-1, -1, -1):
            new_overtakes.append(f"{curr} {final[j][1]}")

        for j in range(0, insert_pos, 1):
            new_overtakes.append(f"{final[j][1]} {curr}")

        final.insert(insert_pos, (pos, curr))

    # print(len(all_overtakes))
    # print("\n".join([f"{x} {y}" for x,y in list(all_overtakes)]))

    print(len(new_overtakes))
    print("\n".join(new_overtakes))

Solution 2 (runtime: 2562 ms):
import os
import sys
from collections import *
from heapq import *
from math import gcd, floor, ceil, sqrt
from copy import deepcopy
from itertools import permutations, combinations, product
from bisect import bisect_left, bisect_right
from functools import lru_cache, reduce
import operator
from random import getrandbits
from itertools import accumulate
from io import BytesIO, IOBase
from types import GeneratorType

input = lambda: sys.stdin.readline().rstrip("\r\n")
flush = lambda: sys.stdout.flush()
print = lambda *args, **kwargs: sys.stdout.write(' '.join(map(str, args)) + kwargs.get("end", "\n")) and flush()

sys.setrecursionlimit(100000)

def bootstrap(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        to = f(*args, **kwargs)
        while True:
            if type(to) is GeneratorType:
                stack.append(to)
                to = next(to)
            else:
                stack.pop()
                if not stack:
                    break
                to = stack[-1].send(to)
        return to

    return wrappedfunc


def ints(): return map(int, input().split())
def strs(): return input().split()
def chars(): return list(input().strip())
def mat(n): return [list(ints()) for _ in range(n)]

INF = float('inf')
MOD = 1000000007
abcd = "abcdefghijklmnopqrstuvwxyz"

def add(x, y, mod=MOD): return (x + y) % mod
def sub(x, y, mod=MOD): return (x - y) % mod
def mul(x, y, mod=MOD): return (x * y) % mod

def invmod(a, mod=MOD): return powmod(a, mod - 2, mod)

def lcm(a, b): return a * b // gcd(a, b)

RANDOM = getrandbits(32)

class Wrapper(int):
    def __init__(self, x):
        int.__init__(x)
    def __hash__(self):
        return super(Wrapper, self).__hash__() ^ RANDOM

# wx = Wrapper(x)
# cnt[wx] = cnt.get(wx, 0) + 1



# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


#sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)


# endregion

def main():
    n = int(input())
    a = list(ints())
    index = {it: i for i, it in enumerate(a)}
    placed = 0
    ans = []
    aux = list(range(1, n + 1))
    index_aux = {i:i for i in range(1, n + 1)}
    for placed, i in enumerate(a[::-1]):
        start = index_aux[i]
        end = aux[0]
        for j in range(start - 1, 0, -1):
            ans.append((aux[index_aux[i] - 1], aux[j - 1]))
            aux[index_aux[i] - 1], aux[j - 1] = aux[j - 1], aux[index_aux[i] - 1]
            index_aux[aux[index_aux[i] - 1]], index_aux[aux[j - 1]] = index_aux[aux[j - 1]], index_aux[
                aux[index_aux[i] - 1]]
        start = index_aux[i]
        for j in range(start + 1, (n - placed) + 1):
            ans.append((aux[j - 1], aux[index_aux[i] - 1]))
            aux[index_aux[i] - 1], aux[j - 1] =aux[j - 1], aux[index_aux[i] - 1]
            index_aux[aux[index_aux[i] - 1]], index_aux[aux[j - 1]] = index_aux[aux[j - 1]], index_aux[aux[index_aux[i] - 1]]
    print(len(ans))
    for u, v in ans:
        print(u, v)
if __name__ == "__main__":
    main()