Problem: 2052A - Adrenaline Rush
Language: PyPy 3-64

Previous submission (verdict: Wrong answer on test 7):
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


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

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

    flag = True

    while (flag):
        i_a = 0
        i_b = 0
        flag = False

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

            if a < b:
                i_b = i
            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()


Submission (verdict: Accepted):
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()