ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 1124 ms):
n = int(input())
a = list(map(int, input().split()))

res = []
pom = [ i for i in range(1, n+1)]
vis = set()
for x in a:
    k = a.index(x)
    l = pom.index(x)
    for j in range(l, n-1):
        if (pom[j+1], pom[j]) not in vis:
            res.append((pom[j+1], pom[j]))
            vis.add((pom[j+1], pom[j]))
            pom[j], pom[j+1] = pom[j+1], pom[j]
        else:
            break
    j = pom.index(x)
    for j in range(j, k, -1):
        res.append((pom[j], pom[j-1]))
        vis.add((pom[j], pom[j-1]))
        pom[j], pom[j-1] = pom[j-1], pom[j]

for i in range(n-1, 0, -1):
    j = i - 1
    if (pom[i],pom[j]) not in vis and (pom[j],pom[i]) not in vis:
        res.append((pom[i],pom[j]))
        res.append((pom[j],pom[i]))
        vis.add((pom[i],pom[j]))
        vis.add((pom[j],pom[i]))


print(len(res))
for x, y in res:
    print(x, y)

Solution 2 (runtime: 2265 ms):
n=int(input())

l=list(map(int,input().split()))

liste=[]

if n==1:

    print(0)

else:

    lcurrent=[]

    lchange=[0]*n

    for i in range(n):

        lcurrent.append(i+1)

    for  i in range(n):

        j=n-1-i

        val=l[j]

        y=lcurrent.index(val)

        if y<j:

            lchange[j]=1

            for k in range(y+1,j+1):

                liste.append((lcurrent[k],val))

                lcurrent[k-1]=lcurrent[k]

            lcurrent[j]=val

        if y>j:

            lchange[j]=1

            for k in range(j,y):

                h=y-k

                liste.append((val,lcurrent[h]))

                lcurrent[h+1]=lcurrent[h]

            lcurrent[j]=val

    

    for  i in range(1,n):

     

        j=n-1-i+1

        

        if  lchange[j]==0:

            liste.append((lcurrent[j],lcurrent[j-1]))

            liste.append((lcurrent[j-1],lcurrent[j]))

    print(len(liste))

    for i in liste:

        a,b=i[0],i[1]

        print(a,b)
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 1124 ms):
n = int(input())
a = list(map(int, input().split()))

res = []
pom = [ i for i in range(1, n+1)]
vis = set()
for x in a:
    k = a.index(x)
    l = pom.index(x)
    for j in range(l, n-1):
        if (pom[j+1], pom[j]) not in vis:
            res.append((pom[j+1], pom[j]))
            vis.add((pom[j+1], pom[j]))
            pom[j], pom[j+1] = pom[j+1], pom[j]
        else:
            break
    j = pom.index(x)
    for j in range(j, k, -1):
        res.append((pom[j], pom[j-1]))
        vis.add((pom[j], pom[j-1]))
        pom[j], pom[j-1] = pom[j-1], pom[j]

for i in range(n-1, 0, -1):
    j = i - 1
    if (pom[i],pom[j]) not in vis and (pom[j],pom[i]) not in vis:
        res.append((pom[i],pom[j]))
        res.append((pom[j],pom[i]))
        vis.add((pom[i],pom[j]))
        vis.add((pom[j],pom[i]))


print(len(res))
for x, y in res:
    print(x, y)

Solution 2 (runtime: 1093 ms):

n=int(input())
a=[int(i) for i in input().split()]
res=n*(n-1)//2
j=n
r=[]
for i in range(2,n+1):
    for j in range(1,i):
        r.append([i,j])
b=[None]*n
for i in range(n):
    b[a[i]-1]=i+1
c=[n-i for i in range(n)]

for i in range(n-1):
    for j in range(1,n-i):
        if b[c[j]-1]<b[c[j-1]-1]:
            res+=1
            r.append([c[j],c[j-1]])
            c[j],c[j-1]=c[j-1],c[j]
print(res)
for i in r:
    print(i[0],i[1])
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 1124 ms):
n = int(input())
a = list(map(int, input().split()))

res = []
pom = [ i for i in range(1, n+1)]
vis = set()
for x in a:
    k = a.index(x)
    l = pom.index(x)
    for j in range(l, n-1):
        if (pom[j+1], pom[j]) not in vis:
            res.append((pom[j+1], pom[j]))
            vis.add((pom[j+1], pom[j]))
            pom[j], pom[j+1] = pom[j+1], pom[j]
        else:
            break
    j = pom.index(x)
    for j in range(j, k, -1):
        res.append((pom[j], pom[j-1]))
        vis.add((pom[j], pom[j-1]))
        pom[j], pom[j-1] = pom[j-1], pom[j]

for i in range(n-1, 0, -1):
    j = i - 1
    if (pom[i],pom[j]) not in vis and (pom[j],pom[i]) not in vis:
        res.append((pom[i],pom[j]))
        res.append((pom[j],pom[i]))
        vis.add((pom[i],pom[j]))
        vis.add((pom[j],pom[i]))


print(len(res))
for x, y in res:
    print(x, y)

Solution 2 (runtime: 1311 ms):
import math
mod=1000000007
mod1=998244353
inf=10**18+10

def swap(l1,x,y):
    a=l1[x]
    l1[x]=l1[y]
    l1[y]=a

t=1
for i in range(t):
    n=int(input())
    l1=list(map(int,input().split()))

    ind=[-1]*(n+1)
    for j in range(n):
        ind[l1[j]]=j

    l2=[j+1 for j in range(n)]
    st=set()

    ans=[]
    flag=1
    # print(l1)
    while flag:
        flag=0
        x=-1
        y=-1
        for j in range(n):
            for j1 in range(n-1):
                if l1[j1]<l1[j1+1]:
                    ans.append([l1[j1], l1[j1+1]])
                    swap(l1, j1, j1+1)
                if ind[l2[j1+1]]<ind[l2[j1]] and flag==0 and (l2[j1], l2[j1+1]) not in st:
                    # flag=1
                    x,y=j1, j1+1
                if ind[l2[j1+1]]>ind[l2[j1]]:
                    if (l2[j1], l2[j1+1]) not in st:
                        st.add((l2[j1+1],l2[j1]))
                        st.add((l2[j1],l2[j1+1]))
        # print(l1)
        for j in range(n):
            for j1 in range(n-1-j):
                if l1[j1]>l1[j1+1]:
                    ans.append([l1[j1], l1[j1+1]])
                    swap(l1, j1+1, j1)
        ans.reverse()
        if x!=-1:
            st.add((l2[y], l2[x]))
            # print(l2, ans)
            swap(l2, x, y)

    print(len(ans))
    for j in ans:
        print(*j)
    # print(ans)
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 764 ms):
n = int(input())
b = list(map(int, input().split()))
d = dict()
cnt = 0
res = []
for i in range(n):
    d[b[i]] = i

a = [i+1 for i in range(n)]
for i in range(n):
    for j in range(n-1):
        if a[j] < a[j+1]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

for i in range(n):
    for j in range(n-1):
        if d[a[j]] > d[a[j+1]]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

print(len(res))
for t in res:
    print(t[0], t[1])



Solution 2 (runtime: 843 ms):
def solve():
    n=int(input())
    a=list(map(int,input().split()))
    que=[]
    res=[]
    for i in range(n-1,-1,-1):
        for j in range(a[i]-1,0,-1):
            if j not in que:
                res.append((a[i],j))
        for j in range(len(que)-1,-1,-1):
            res.append((a[i],que[j]))
        que.insert(0,a[i])

    print(len(res))
    for el in res:
        print(*el)
solve()
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 764 ms):
n = int(input())
b = list(map(int, input().split()))
d = dict()
cnt = 0
res = []
for i in range(n):
    d[b[i]] = i

a = [i+1 for i in range(n)]
for i in range(n):
    for j in range(n-1):
        if a[j] < a[j+1]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

for i in range(n):
    for j in range(n-1):
        if d[a[j]] > d[a[j+1]]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

print(len(res))
for t in res:
    print(t[0], t[1])



Solution 2 (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()
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 764 ms):
n = int(input())
b = list(map(int, input().split()))
d = dict()
cnt = 0
res = []
for i in range(n):
    d[b[i]] = i

a = [i+1 for i in range(n)]
for i in range(n):
    for j in range(n-1):
        if a[j] < a[j+1]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

for i in range(n):
    for j in range(n-1):
        if d[a[j]] > d[a[j+1]]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

print(len(res))
for t in res:
    print(t[0], t[1])



Solution 2 (runtime: 749 ms):
len_, lst, op = int(input()), list(map(int, input().split())), []
if len_ == 1: print(0); exit()
for i in range(1, len_ + 1):
    if (index_ := lst.index(i)) != len_ - 1:
        for j in lst[index_ + 1:]:
            if j > i: op.append(f'{i} {j}')
    for k in range(1, i): op.append(f'{i} {k}')
print(len(op), *op[::-1], sep='\n')
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 764 ms):
n = int(input())
b = list(map(int, input().split()))
d = dict()
cnt = 0
res = []
for i in range(n):
    d[b[i]] = i

a = [i+1 for i in range(n)]
for i in range(n):
    for j in range(n-1):
        if a[j] < a[j+1]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

for i in range(n):
    for j in range(n-1):
        if d[a[j]] > d[a[j+1]]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

print(len(res))
for t in res:
    print(t[0], t[1])



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))
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 764 ms):
n = int(input())
b = list(map(int, input().split()))
d = dict()
cnt = 0
res = []
for i in range(n):
    d[b[i]] = i

a = [i+1 for i in range(n)]
for i in range(n):
    for j in range(n-1):
        if a[j] < a[j+1]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

for i in range(n):
    for j in range(n-1):
        if d[a[j]] > d[a[j+1]]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

print(len(res))
for t in res:
    print(t[0], t[1])



Solution 2 (runtime: 515 ms):
n=int(input())
L=list(map(int,input().split()))
s=set()
OUT=[]
m=0
for i in L :
    for j in range(i+1,n+1):
        if not j in s :
            
            OUT.append(str(j)+" "+str(i))
            m+=1
    for k in range(n,i,-1):
        if not k in s :
            
            OUT.append(str(i)+" "+str(k))
            m+=1
    for r in range(i-1,0,-1):
        if not r in s :
            
            OUT.append(str(i)+" "+str(r))
            m+=1
    s.add(i)

print(m)
for i in OUT :
    print(i)
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 764 ms):
n = int(input())
b = list(map(int, input().split()))
d = dict()
cnt = 0
res = []
for i in range(n):
    d[b[i]] = i

a = [i+1 for i in range(n)]
for i in range(n):
    for j in range(n-1):
        if a[j] < a[j+1]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

for i in range(n):
    for j in range(n-1):
        if d[a[j]] > d[a[j+1]]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

print(len(res))
for t in res:
    print(t[0], t[1])



Solution 2 (runtime: 593 ms):
a = int(input())
b = list(map(int, input().split()))
s= [int(i) for i in range(1,a+1)]
co = 0
obg = []
if a == 1:
    print(0)
else:
    for i in range(a-1):
        for j in range(a-1-i):
            if s[j] < s[j+1]:
                s[j],s[j+1]=s[j+1],s[j]
                co += 1
                obg.append(s[j])
                obg.append(s[j+1])
    for i in range(a-1):
        for j in range(a-1-i):
            if s[j] == b[a-1-i]:
                s[j],s[j+1]=s[j+1],s[j]
                co += 1
                obg.append(s[j])
                obg.append(s[j+1])
    print(co)   
    for i in range(0,co*2,2):
        print(obg[i], obg[i+1])
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 764 ms):
n = int(input())
b = list(map(int, input().split()))
d = dict()
cnt = 0
res = []
for i in range(n):
    d[b[i]] = i

a = [i+1 for i in range(n)]
for i in range(n):
    for j in range(n-1):
        if a[j] < a[j+1]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

for i in range(n):
    for j in range(n-1):
        if d[a[j]] > d[a[j+1]]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

print(len(res))
for t in res:
    print(t[0], t[1])



Solution 2 (runtime: 655 ms):
import io,os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline




def main():
    n = int(input())
    arr = list(map(int,input().split()))
    for i in range(n):
        arr[i] -= 1


    curr = [i for i in range(n)]

    ans = []


    for i in range(n):
        index = -1
        for j in range(n):
            if curr[j]==arr[i]:  
                index = j   
                break

        for j in range(index,n-1):
            curr[j],curr[j+1] = curr[j+1],curr[j]
            ans.append((curr[j]+1,curr[j+1]+1))
        for j in range(n-2,i-1,-1):
            curr[j],curr[j+1] = curr[j+1],curr[j]
            ans.append((curr[j]+1,curr[j+1]+1))        



    print(len(ans))
    for ele in ans:
        print(*ele)



main()
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 764 ms):
n = int(input())
b = list(map(int, input().split()))
d = dict()
cnt = 0
res = []
for i in range(n):
    d[b[i]] = i

a = [i+1 for i in range(n)]
for i in range(n):
    for j in range(n-1):
        if a[j] < a[j+1]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

for i in range(n):
    for j in range(n-1):
        if d[a[j]] > d[a[j+1]]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

print(len(res))
for t in res:
    print(t[0], t[1])



Solution 2 (runtime: 2484 ms):
import sys

def max_num(order, n):
    counted = set()
    m = 0
    string = ""
    for i in order:
        before = ""
        after = ""
        m += n - len(counted)-1
        for j in reversed(range(1,n+1)):
            if j not in counted and j != i:
                if i < j:
                    m += 1
                    before = f"\n{j} {i}"+before
                before += f"\n{i} {j}"
        string += before + after
        counted.add(i)
    print(f"{m}{string}")
    return m, string

i = 0
for line in sys.stdin.readlines():
    if i==0:
        n = int(line)
    else:
        order = [int(x) for x in line.split(" ")]
        max_num(order, n)
    i+=1
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 764 ms):
n = int(input())
b = list(map(int, input().split()))
d = dict()
cnt = 0
res = []
for i in range(n):
    d[b[i]] = i

a = [i+1 for i in range(n)]
for i in range(n):
    for j in range(n-1):
        if a[j] < a[j+1]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

for i in range(n):
    for j in range(n-1):
        if d[a[j]] > d[a[j+1]]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

print(len(res))
for t in res:
    print(t[0], t[1])



Solution 2 (runtime: 702 ms):
def sol():
    n = int(input())
    c = [0] + list(map(int, input().split()))
    mp = [i for i in range(n + 1)]
    pos = [i for i in range(n + 1)]
    ans = []
    for i in range(1, n + 1):
        for j in range(mp[c[i]] + 1, n + 1):
            ans.append((pos[j], c[i]))
        for j in range(n, mp[c[i]], -1):
            ans.append((c[i], pos[j]))
        for j in range(mp[c[i]] - 1, i - 1, -1):
            ans.append((c[i], pos[j]))
            mp[pos[j]] = j + 1
            pos[j + 1] = pos[j]
    
    print(len(ans))
    for a, b in ans:
        print(a, b)

sol()
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 764 ms):
n = int(input())
b = list(map(int, input().split()))
d = dict()
cnt = 0
res = []
for i in range(n):
    d[b[i]] = i

a = [i+1 for i in range(n)]
for i in range(n):
    for j in range(n-1):
        if a[j] < a[j+1]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

for i in range(n):
    for j in range(n-1):
        if d[a[j]] > d[a[j+1]]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

print(len(res))
for t in res:
    print(t[0], t[1])



Solution 2 (runtime: 437 ms):
num_cars = int(input())
arr = [int(a) for a in input().split()]
arr.reverse()
running_sum = 0
overtake_list = []
car_list = [i for i in range(1, num_cars + 1)]
car_list.reverse()
for n, i in enumerate(arr):
    curr_index = car_list.index(i)
    car_list.pop(curr_index)
    for elem in (range(curr_index, len(car_list))):
        overtake_list.append(f"{i} {car_list[elem]}")
    for elem in reversed(range(0, len(car_list))):
        overtake_list.append(f"{car_list[elem]} {i}")
    
print(len(overtake_list))
for overtake in overtake_list:
    print(overtake)
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 764 ms):
n = int(input())
b = list(map(int, input().split()))
d = dict()
cnt = 0
res = []
for i in range(n):
    d[b[i]] = i

a = [i+1 for i in range(n)]
for i in range(n):
    for j in range(n-1):
        if a[j] < a[j+1]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

for i in range(n):
    for j in range(n-1):
        if d[a[j]] > d[a[j+1]]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

print(len(res))
for t in res:
    print(t[0], t[1])



Solution 2 (runtime: 640 ms):
def II():
    return(int(input()))
def LMI():
    return(list(map(int,input().split())))
def I():
    return(input())
def MII():
    return(map(int,input().split()))
# import sys
# input=sys.stdin.readline
# import io,os
# input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
# from collections import Counter
# int(math.log(len(L)))
# import math
# from collections import defaultdict
# mod=10**9+7
# from collections import deque
# import  math



# from functools import cache
# @cache
def t():
    n=II()
    L=LMI()
    ans=[]
    cur=[i for i in range(1,n+1)]
    for i in range(n):
        ind=cur.index(L[i])
        for j in range(ind+1,n):
            ans.append((cur[j],cur[ind]))
        for j in range(n-1,i-1,-1):
            if j==ind:continue 
            ans.append((cur[ind],cur[j]))
        del cur[ind]
        cur=cur[:i]+[L[i]]+cur[i:]
    print(len(ans))
    for i in ans:
        print(*i)           
    return



if __name__=="__main__":

    # for _ in range(II()):
    #     t()
    t()
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 764 ms):
n = int(input())
b = list(map(int, input().split()))
d = dict()
cnt = 0
res = []
for i in range(n):
    d[b[i]] = i

a = [i+1 for i in range(n)]
for i in range(n):
    for j in range(n-1):
        if a[j] < a[j+1]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

for i in range(n):
    for j in range(n-1):
        if d[a[j]] > d[a[j+1]]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

print(len(res))
for t in res:
    print(t[0], t[1])



Solution 2 (runtime: 781 ms):
n = int(input())
final_order = [int(x) - 1 for x in input().split()]
cur_order = list(range(n))

swaps = []
for i, target in enumerate(final_order):
    cur_pos = cur_order.index(target)
    for j in range(cur_pos + 1, n):
        swaps.append((cur_order[j], target))
    for j in range(n - 1, i - 1, -1):
        if j == cur_pos:
            continue
        swaps.append((target, cur_order[j]))
    cur_order.pop(cur_pos)
    cur_order.insert(i, target)

print(len(swaps))
for x, y in swaps:
    print(x + 1, y + 1)
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 764 ms):
n = int(input())
b = list(map(int, input().split()))
d = dict()
cnt = 0
res = []
for i in range(n):
    d[b[i]] = i

a = [i+1 for i in range(n)]
for i in range(n):
    for j in range(n-1):
        if a[j] < a[j+1]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

for i in range(n):
    for j in range(n-1):
        if d[a[j]] > d[a[j+1]]:
            res.append((a[j+1], a[j]))
            a[j], a[j+1] = a[j+1], a[j]

print(len(res))
for t in res:
    print(t[0], t[1])



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')