Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 2249 ms):
import sys


def readline(type):
    return list(map(type, sys.stdin.readline().split(' ')))


def add_ans(a, b, ans, check):
    if check[a - 1][b - 1] != 0:
        return

    ans.append((a, b))
    check[a - 1][b - 1] = 1

# 1 2 3 4
# 1 2 3 4


def apply_stacked(stacked, ans, check):
    for a, b in reversed(stacked):
        add_ans(b, a, ans, check)


def useless_swaps(i_a, i_b, order, ans, check):
    if i_a == i_b:
        return

    for i in range(i_a, i_b):
        first = []
        last = []

        for next_i in range(i + 1, i_b + 1):
            a = order[i]
            b = order[next_i]

            if (
                check[a - 1][b - 1] + check[b - 1][a - 1] == 0
            ):
                first.append((a, b))
                last.append((a, b))
            else:
                break

        for a, b in first:
            ans.append((a, b))
            check[a - 1][b - 1] = 1
        for b, a in reversed(last):
            ans.append((a, b))
            check[a - 1][b - 1] = 1
# 1 2 3 4
# 1 3 2 4
# 1 2 3 4
# 1 2 4 3
# 4 1 2 3

def main():
    n, = readline(int)
    order = readline(int)

    check = [[0] * n for _ in range(n)]
    ans = []

    flag = True

    while flag:
        i_a = n - 1
        i_b = n - 1
        flag = False

        for i in range(n - 1, 0, -1):
            a = order[i - 1]
            b = order[i]

            if a < b:
                i_a = i - 1
            else:
                useless_swaps(i_a, i_b, order, ans, check)
                flag = True
                order[i - 1] = b
                order[i] = a
                ans.append((a, b))
                check[a - 1][b - 1] = 1
                break
        if not flag:
            useless_swaps(i_a, i_b, order, ans, check)

    print(len(ans))
    for a, b in reversed(ans):
        print(a, b, sep=' ')

if __name__ == '__main__':
    main()


Solution 2 (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))