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

Solution 1 (runtime: 702 ms):
def sol():
    n = int(input())
    a = list(map(int, input().split()))
    rev_a = [None] * n
    for i, x in enumerate(a):
        rev_a[x-1] = i+1
    # print(rev_a)
    steps = []
    orig = list(range(1, n+1))
    for i in range(n):
        x = i + 1
        for p, w in enumerate(rev_a):
            if w == x:
                x_index = p
                break
        
        for j in range(x_index, n-1):
            steps.append((orig[j+1], orig[j]))
            orig[j+1], orig[j] = orig[j], orig[j+1]
            rev_a[j+1], rev_a[j] = rev_a[j], rev_a[j+1]
        for j in range(n-2, x_index-1, -1):
            steps.append((orig[j+1], orig[j]))
            orig[j+1], orig[j] = orig[j], orig[j+1]
            rev_a[j+1], rev_a[j] = rev_a[j], rev_a[j+1]
        if x_index > i:
            for j in range(x_index-1, x-2, -1):
                steps.append((orig[j+1], orig[j]))
                orig[j+1], orig[j] = orig[j], orig[j+1]
                rev_a[j+1], rev_a[j] = rev_a[j], rev_a[j+1]

    print(len(steps))
    for x in steps:
        print(*x)


sol()

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: 702 ms):
def sol():
    n = int(input())
    a = list(map(int, input().split()))
    rev_a = [None] * n
    for i, x in enumerate(a):
        rev_a[x-1] = i+1
    # print(rev_a)
    steps = []
    orig = list(range(1, n+1))
    for i in range(n):
        x = i + 1
        for p, w in enumerate(rev_a):
            if w == x:
                x_index = p
                break
        
        for j in range(x_index, n-1):
            steps.append((orig[j+1], orig[j]))
            orig[j+1], orig[j] = orig[j], orig[j+1]
            rev_a[j+1], rev_a[j] = rev_a[j], rev_a[j+1]
        for j in range(n-2, x_index-1, -1):
            steps.append((orig[j+1], orig[j]))
            orig[j+1], orig[j] = orig[j], orig[j+1]
            rev_a[j+1], rev_a[j] = rev_a[j], rev_a[j+1]
        if x_index > i:
            for j in range(x_index-1, x-2, -1):
                steps.append((orig[j+1], orig[j]))
                orig[j+1], orig[j] = orig[j], orig[j+1]
                rev_a[j+1], rev_a[j] = rev_a[j], rev_a[j+1]

    print(len(steps))
    for x in steps:
        print(*x)


sol()

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: 702 ms):
def sol():
    n = int(input())
    a = list(map(int, input().split()))
    rev_a = [None] * n
    for i, x in enumerate(a):
        rev_a[x-1] = i+1
    # print(rev_a)
    steps = []
    orig = list(range(1, n+1))
    for i in range(n):
        x = i + 1
        for p, w in enumerate(rev_a):
            if w == x:
                x_index = p
                break
        
        for j in range(x_index, n-1):
            steps.append((orig[j+1], orig[j]))
            orig[j+1], orig[j] = orig[j], orig[j+1]
            rev_a[j+1], rev_a[j] = rev_a[j], rev_a[j+1]
        for j in range(n-2, x_index-1, -1):
            steps.append((orig[j+1], orig[j]))
            orig[j+1], orig[j] = orig[j], orig[j+1]
            rev_a[j+1], rev_a[j] = rev_a[j], rev_a[j+1]
        if x_index > i:
            for j in range(x_index-1, x-2, -1):
                steps.append((orig[j+1], orig[j]))
                orig[j+1], orig[j] = orig[j], orig[j+1]
                rev_a[j+1], rev_a[j] = rev_a[j], rev_a[j+1]

    print(len(steps))
    for x in steps:
        print(*x)


sol()

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: 999 ms):
import sys
from queue import deque
from bisect import bisect_left, bisect_right
from heapq import *
from collections import *

sys.setrecursionlimit(10**5)

input = lambda: sys.stdin.readline().rstrip('\r\n')
iint = lambda: int(input())
mint = lambda: map(int, input().split())
aint = lambda: list(map(int, input().split()))

MOD = 10**9 + 7

def solve():
    n = iint()
    a = aint()
    rank = [0] * (n + 1)
    for i in range(n):
        rank[a[i]] = i+1
   
    mx_len = n * (n-1) // 2
    curr = list(range(n, 0, -1))
    sq = []
    for i in range(n):
        j = i
        while j>0 and rank[curr[j]] < rank[curr[j-1]]:
            sq.append([curr[j], curr[j-1]])
            curr[j], curr[j-1] = curr[j-1], curr[j]
            mx_len += 1
            j -= 1

    print(mx_len)
    for i in range(1, n+1):
        for j in range(i+1, n+1):
            print(j, i)
    for a, b in sq:
        print(a, b)

solve()

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

a = [int(i) for i in input().split()]

inds = [0 for i in range(n+1)]

for i in range(n):

    inds[a[i]] = i



total = 0

changes = []



l = [i+1 for i in range(n)]

for i in range(n):

    ind = l.index(a[i])



    for j in range(ind+1, n):

        changes.append([l[j], a[i]])

    for j in range(n-1, ind, -1):

        changes.append([a[i], l[j]])



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

        changes.append([a[i], l[j]])



    l = [l[ind]] + l[:ind] + l[ind+1:]





print(len(changes))

for i in changes:

    print(*i)
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 999 ms):
import sys
from queue import deque
from bisect import bisect_left, bisect_right
from heapq import *
from collections import *

sys.setrecursionlimit(10**5)

input = lambda: sys.stdin.readline().rstrip('\r\n')
iint = lambda: int(input())
mint = lambda: map(int, input().split())
aint = lambda: list(map(int, input().split()))

MOD = 10**9 + 7

def solve():
    n = iint()
    a = aint()
    rank = [0] * (n + 1)
    for i in range(n):
        rank[a[i]] = i+1
   
    mx_len = n * (n-1) // 2
    curr = list(range(n, 0, -1))
    sq = []
    for i in range(n):
        j = i
        while j>0 and rank[curr[j]] < rank[curr[j-1]]:
            sq.append([curr[j], curr[j-1]])
            curr[j], curr[j-1] = curr[j-1], curr[j]
            mx_len += 1
            j -= 1

    print(mx_len)
    for i in range(1, n+1):
        for j in range(i+1, n+1):
            print(j, i)
    for a, b in sq:
        print(a, b)

solve()

Solution 2 (runtime: 483 ms):
from sys import stdin

input = lambda: stdin.readline().strip()



n = int(input())

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

ans = []



for _ in range(n):

    for i in range(n - 1):

        if a[i] < a[i + 1]:

            ans.append((a[i], a[i + 1]))

            a[i], a[i + 1] = a[i + 1], a[i]

for _ in range(n):

    for i in range(n - 1):

        if a[i] > a[i + 1]:

            ans.append((a[i], a[i + 1]))

            a[i], a[i + 1] = a[i + 1], a[i]

ans.reverse()

print(len(ans))

for x, y in ans:

    print(f'{x} {y}')
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 999 ms):
import sys
from queue import deque
from bisect import bisect_left, bisect_right
from heapq import *
from collections import *

sys.setrecursionlimit(10**5)

input = lambda: sys.stdin.readline().rstrip('\r\n')
iint = lambda: int(input())
mint = lambda: map(int, input().split())
aint = lambda: list(map(int, input().split()))

MOD = 10**9 + 7

def solve():
    n = iint()
    a = aint()
    rank = [0] * (n + 1)
    for i in range(n):
        rank[a[i]] = i+1
   
    mx_len = n * (n-1) // 2
    curr = list(range(n, 0, -1))
    sq = []
    for i in range(n):
        j = i
        while j>0 and rank[curr[j]] < rank[curr[j-1]]:
            sq.append([curr[j], curr[j-1]])
            curr[j], curr[j-1] = curr[j-1], curr[j]
            mx_len += 1
            j -= 1

    print(mx_len)
    for i in range(1, n+1):
        for j in range(i+1, n+1):
            print(j, i)
    for a, b in sq:
        print(a, b)

solve()

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: 999 ms):
import sys
from queue import deque
from bisect import bisect_left, bisect_right
from heapq import *
from collections import *

sys.setrecursionlimit(10**5)

input = lambda: sys.stdin.readline().rstrip('\r\n')
iint = lambda: int(input())
mint = lambda: map(int, input().split())
aint = lambda: list(map(int, input().split()))

MOD = 10**9 + 7

def solve():
    n = iint()
    a = aint()
    rank = [0] * (n + 1)
    for i in range(n):
        rank[a[i]] = i+1
   
    mx_len = n * (n-1) // 2
    curr = list(range(n, 0, -1))
    sq = []
    for i in range(n):
        j = i
        while j>0 and rank[curr[j]] < rank[curr[j-1]]:
            sq.append([curr[j], curr[j-1]])
            curr[j], curr[j-1] = curr[j-1], curr[j]
            mx_len += 1
            j -= 1

    print(mx_len)
    for i in range(1, n+1):
        for j in range(i+1, n+1):
            print(j, i)
    for a, b in sq:
        print(a, b)

solve()

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: 999 ms):
import sys
from queue import deque
from bisect import bisect_left, bisect_right
from heapq import *
from collections import *

sys.setrecursionlimit(10**5)

input = lambda: sys.stdin.readline().rstrip('\r\n')
iint = lambda: int(input())
mint = lambda: map(int, input().split())
aint = lambda: list(map(int, input().split()))

MOD = 10**9 + 7

def solve():
    n = iint()
    a = aint()
    rank = [0] * (n + 1)
    for i in range(n):
        rank[a[i]] = i+1
   
    mx_len = n * (n-1) // 2
    curr = list(range(n, 0, -1))
    sq = []
    for i in range(n):
        j = i
        while j>0 and rank[curr[j]] < rank[curr[j-1]]:
            sq.append([curr[j], curr[j-1]])
            curr[j], curr[j-1] = curr[j-1], curr[j]
            mx_len += 1
            j -= 1

    print(mx_len)
    for i in range(1, n+1):
        for j in range(i+1, n+1):
            print(j, i)
    for a, b in sq:
        print(a, b)

solve()

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: 1186 ms):
n = int(input())

a = [int(i) for i in input().split()]

inds = [0 for i in range(n+1)]

for i in range(n):

    inds[a[i]] = i



total = 0

changes = []



l = [i+1 for i in range(n)]

for i in range(n):

    ind = l.index(a[i])



    for j in range(ind+1, n):

        changes.append([l[j], a[i]])

    for j in range(n-1, ind, -1):

        changes.append([a[i], l[j]])



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

        changes.append([a[i], l[j]])



    l = [l[ind]] + l[:ind] + l[ind+1:]





print(len(changes))

for i in changes:

    print(*i)

Solution 2 (runtime: 483 ms):
from sys import stdin

input = lambda: stdin.readline().strip()



n = int(input())

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

ans = []



for _ in range(n):

    for i in range(n - 1):

        if a[i] < a[i + 1]:

            ans.append((a[i], a[i + 1]))

            a[i], a[i + 1] = a[i + 1], a[i]

for _ in range(n):

    for i in range(n - 1):

        if a[i] > a[i + 1]:

            ans.append((a[i], a[i + 1]))

            a[i], a[i + 1] = a[i + 1], a[i]

ans.reverse()

print(len(ans))

for x, y in ans:

    print(f'{x} {y}')
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 1186 ms):
n = int(input())

a = [int(i) for i in input().split()]

inds = [0 for i in range(n+1)]

for i in range(n):

    inds[a[i]] = i



total = 0

changes = []



l = [i+1 for i in range(n)]

for i in range(n):

    ind = l.index(a[i])



    for j in range(ind+1, n):

        changes.append([l[j], a[i]])

    for j in range(n-1, ind, -1):

        changes.append([a[i], l[j]])



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

        changes.append([a[i], l[j]])



    l = [l[ind]] + l[:ind] + l[ind+1:]





print(len(changes))

for i in changes:

    print(*i)

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: 1186 ms):
n = int(input())

a = [int(i) for i in input().split()]

inds = [0 for i in range(n+1)]

for i in range(n):

    inds[a[i]] = i



total = 0

changes = []



l = [i+1 for i in range(n)]

for i in range(n):

    ind = l.index(a[i])



    for j in range(ind+1, n):

        changes.append([l[j], a[i]])

    for j in range(n-1, ind, -1):

        changes.append([a[i], l[j]])



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

        changes.append([a[i], l[j]])



    l = [l[ind]] + l[:ind] + l[ind+1:]





print(len(changes))

for i in changes:

    print(*i)

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: 1186 ms):
n = int(input())

a = [int(i) for i in input().split()]

inds = [0 for i in range(n+1)]

for i in range(n):

    inds[a[i]] = i



total = 0

changes = []



l = [i+1 for i in range(n)]

for i in range(n):

    ind = l.index(a[i])



    for j in range(ind+1, n):

        changes.append([l[j], a[i]])

    for j in range(n-1, ind, -1):

        changes.append([a[i], l[j]])



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

        changes.append([a[i], l[j]])



    l = [l[ind]] + l[:ind] + l[ind+1:]





print(len(changes))

for i in changes:

    print(*i)

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: 483 ms):
from sys import stdin

input = lambda: stdin.readline().strip()



n = int(input())

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

ans = []



for _ in range(n):

    for i in range(n - 1):

        if a[i] < a[i + 1]:

            ans.append((a[i], a[i + 1]))

            a[i], a[i + 1] = a[i + 1], a[i]

for _ in range(n):

    for i in range(n - 1):

        if a[i] > a[i + 1]:

            ans.append((a[i], a[i + 1]))

            a[i], a[i + 1] = a[i + 1], a[i]

ans.reverse()

print(len(ans))

for x, y in ans:

    print(f'{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: 483 ms):
from sys import stdin

input = lambda: stdin.readline().strip()



n = int(input())

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

ans = []



for _ in range(n):

    for i in range(n - 1):

        if a[i] < a[i + 1]:

            ans.append((a[i], a[i + 1]))

            a[i], a[i + 1] = a[i + 1], a[i]

for _ in range(n):

    for i in range(n - 1):

        if a[i] > a[i + 1]:

            ans.append((a[i], a[i + 1]))

            a[i], a[i + 1] = a[i + 1], a[i]

ans.reverse()

print(len(ans))

for x, y in ans:

    print(f'{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])