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

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

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

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

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

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

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

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

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

Solution 2 (runtime: 2077 ms):
def count(li):
    su = 0
    for i in range(len(li)-1, 0, -1):
        for j in range(i):
            if li[j] > li[i]:
                su += 1
            else:
                su += 2
    return su


n = int(input())
firs = [_ for _ in range(1, n+1)]     #1 2 3 4
fina = list(map(int, input().split()))#2 1 4 3
fina.reverse()
reve = fina.copy()  #第一轮超车后的排序 # 3 4 1 2
fina.reverse()
ans = set() #已经排好的数组
print(count(fina))

for i in range(n):  #2 1 4 3
    for j in range(fina[i]+1, n+1):  #如果有大于他的，就超过他
        if j in ans:       #如果大于他的数已经排好了，就跳过他
            continue
        print(j,fina[i])

    ans.add(fina[i])
for k in range(n-1, 0, -1): # 3 4 1 2
    for t in range(1, k+1):
        print(reve[k], reve[k-t])
Problem: 2052A - Adrenaline Rush
Language: python

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

Solution 2 (runtime: 671 ms):
import bisect
from collections import deque
D = ((0, 1), (1, 0), (0, -1), (-1, 0))

# T = int(input())
T = 1



def solve():
    n = int(input())
    a = list(map(int, input().split()))
    a = list(reversed(a))
    p = [i+1 for i in range(n-1, -1, -1)]
    res = []
    # print(p)
    for idx, i in enumerate(a):
        l = bisect.bisect_right(p, -i, idx, n, key=lambda x: -x) - 1
        # print(f"searched {i} in {p[idx:n]} and found {p[l]} @ {l}")
        for jdx in range(l+1, n):
            res.append((p[jdx-1], p[jdx]))
            p[jdx-1], p[jdx] = p[jdx], p[jdx-1]
            # print(p)
        for jdx in range(n-1, idx, -1):
            res.append((p[jdx-1], p[jdx]))
            p[jdx-1], p[jdx] = p[jdx], p[jdx-1]
            # print(p)
    print(len(res))
    for i in res:
        print(i[0], i[1])

while(T):
    T-=1
    solve()
Problem: 2052A - Adrenaline Rush
Language: python

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

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

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

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