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: 952 ms):
import sys

N = int(sys.stdin.readline())

class Number:
    
    SWAPS = set()
    
    def __init__(self, value, index):
        self.value = value
        self.idx = value-1
        self.final_idx = index
        
    def __repr__(self):
        return str(self.value)
    
    @staticmethod
    def swap(a, b):
        if a == b: return
        if (a, b) in Number.SWAPS: return
        a, b = Number.dct[a], Number.dct[b]
        if a.idx < b.idx: return
        a.idx, b.idx = b.idx, a.idx
        Number.lst[a.idx], Number.lst[b.idx] = a, b
        Number.swaps.append((a.value, b.value))
        Number.SWAPS.add((a.value, b.value))

Number.dct = {int(x): Number(int(x), i) for i, x in enumerate(sys.stdin.readline().split())}
Number.lst = [Number.dct[i] for i in range(1, N + 1)]
Number.swaps = []
            
lst = Number.lst
dct = Number.dct
swaps = Number.swaps

def bring_to_front(n):
    if dct[n].idx == 0: return
    for i in range(n-1, 0, -1):
        Number.swap(n, lst[i-1].value)
        
def bring_to_back(n):
    idx = dct[n].final_idx
    if idx == 0: return
    for i in range(1, idx+1):
        Number.swap(lst[i].value, n)
        
for i in reversed(dct.keys()):
    bring_to_front(i)
    bring_to_back(i)

sys.stdout.write(str(len(swaps)) + '\n')
for swap in swaps:
    sys.stdout.write(str(swap[0]) + ' ' + str(swap[1]) + '\n')