| Task: | Pair sort |
| Sender: | aalto26dw_006 |
| Submission time: | 2026-09-23 17:18:07 +0300 |
| Language: | Python3 (PyPy3) |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.04 s | details |
| #2 | ACCEPTED | 0.04 s | details |
| #3 | ACCEPTED | 0.04 s | details |
| #4 | ACCEPTED | 0.04 s | details |
| #5 | ACCEPTED | 0.04 s | details |
| #6 | ACCEPTED | 0.04 s | details |
| #7 | ACCEPTED | 0.04 s | details |
| #8 | ACCEPTED | 0.04 s | details |
| #9 | ACCEPTED | 0.05 s | details |
| #10 | ACCEPTED | 0.05 s | details |
| #11 | ACCEPTED | 0.06 s | details |
| #12 | ACCEPTED | 0.06 s | details |
| #13 | ACCEPTED | 0.09 s | details |
| #14 | ACCEPTED | 0.09 s | details |
Code
import sys
input_d = sys.stdin.read().split()
n = int(input_d[0])
arr = [int(i) for i in input_d[1:]]
swaps = []
idx = 0
#have dic with position for each key before iterating so i avoid for i in range(n) everytime using the number as key :D
swapcount = 0
pos = {}
for i in range(len(arr)) :
if not arr[i] in pos.keys():
pos[arr[i]] = [i]
else :
pos[arr[i]].append(i)
#print(pos)
idx = 0
for i in range(n-1):
elem = arr[idx]
a = pos.get(elem)[0]
b = pos.get(elem)[1]
toswap = b if a == idx else a
ind = 1 if a == idx else 0
elemswapped = arr[idx+1]
indswapped = 0 if idx+1 == pos.get(elemswapped)[0] else 1
#print(toswap)
if idx < len(arr) -1 :
swaps.append([toswap+1,idx+1+1])
pos.get(elem)[ind] = idx+1
pos.get(elemswapped)[indswapped]= toswap
#print("elem we swap for",pos.get(elem))
#print("elem we sawpped out",pos.get(elemswapped))
arr[toswap], arr[idx+1] = arr[idx+1], arr[toswap] #swap them
idx+=2
swapcount+=1
#print(arr)
print(swapcount)
for s in swaps:
print(*s)
'''for i in range(n-1):
elem = arr[idx]
print("elem is",elem)
print("idx is ",idx)
if idx + 1 < len(arr):
if arr[idx]==arr[idx+1]:
idx+=2
else :
toswap = arr[idx+1:].index(elem)
swaps.append([idx+1,toswap])
#swapping
temp = arr[idx+1]
arr[idx+1] = arr[toswap]
arr[toswap] = temp
swapcount +=1
idx+=2'''
'''
print(swapcount)
print(arr)
print(swaps)'''
#print(arr)Test details
Test 1
Verdict: ACCEPTED
| input |
|---|
| 5 3 2 4 5 1 3 2 1 4 5 |
| correct output |
|---|
| 3 2 6 4 9 6 8 |
| user output |
|---|
| 4 6 2 9 4 8 6 8 8 |
Test 2
Verdict: ACCEPTED
| input |
|---|
| 5 3 2 4 5 1 3 2 1 4 5 |
| correct output |
|---|
| 3 2 6 4 9 6 8 |
| user output |
|---|
| 4 6 2 9 4 8 6 8 8 |
Test 3
Verdict: ACCEPTED
| input |
|---|
| 10 3 6 6 8 8 9 9 1 4 5 2 4 10 2 1... |
| correct output |
|---|
| 9 2 17 4 17 6 17 8 17 ... |
| user output |
|---|
| 9 17 2 17 4 17 6 17 8 ... |
Test 4
Verdict: ACCEPTED
| input |
|---|
| 10 3 6 6 8 8 9 9 1 4 5 2 4 10 2 1... |
| correct output |
|---|
| 9 2 17 4 17 6 17 8 17 ... |
| user output |
|---|
| 9 17 2 17 4 17 6 17 8 ... |
Test 5
Verdict: ACCEPTED
| input |
|---|
| 50 47 26 6 35 13 18 9 19 14 50 34... |
| correct output |
|---|
| 48 2 87 4 78 6 71 8 55 ... |
| user output |
|---|
| 49 87 2 78 4 71 6 55 8 ... |
Test 6
Verdict: ACCEPTED
| input |
|---|
| 50 47 26 6 35 13 18 9 19 14 50 34... |
| correct output |
|---|
| 48 2 87 4 78 6 71 8 55 ... |
| user output |
|---|
| 49 87 2 78 4 71 6 55 8 ... |
Test 7
Verdict: ACCEPTED
| input |
|---|
| 100 56 26 6 35 60 72 9 55 83 51 58... |
| correct output |
|---|
| 97 2 77 4 108 6 141 8 55 ... |
| user output |
|---|
| 99 77 2 108 4 141 6 55 8 ... |
Test 8
Verdict: ACCEPTED
| input |
|---|
| 100 56 26 6 35 60 72 9 55 83 51 58... |
| correct output |
|---|
| 97 2 77 4 108 6 141 8 55 ... |
| user output |
|---|
| 99 77 2 108 4 141 6 55 8 ... |
Test 9
Verdict: ACCEPTED
| input |
|---|
| 500 56 146 351 35 281 235 354 449 ... |
| correct output |
|---|
| 497 2 758 4 820 6 125 8 243 ... |
| user output |
|---|
| 499 758 2 820 4 125 6 243 8 ... |
Test 10
Verdict: ACCEPTED
| input |
|---|
| 500 56 146 351 35 281 235 354 449 ... |
| correct output |
|---|
| 497 2 758 4 820 6 125 8 243 ... |
| user output |
|---|
| 499 758 2 820 4 125 6 243 8 ... |
Test 11
Verdict: ACCEPTED
| input |
|---|
| 1000 603 596 351 885 530 235 354 56... |
| correct output |
|---|
| 993 2 256 4 1534 6 816 8 1057 ... |
| user output |
|---|
| 999 256 2 1534 4 816 6 1057 8 ... |
Test 12
Verdict: ACCEPTED
| input |
|---|
| 1000 603 596 351 885 530 235 354 56... |
| correct output |
|---|
| 993 2 256 4 1534 6 816 8 1057 ... |
| user output |
|---|
| 999 256 2 1534 4 816 6 1057 8 ... |
Test 13
Verdict: ACCEPTED
| input |
|---|
| 5000 1594 596 1797 3776 1201 235 35... |
| correct output |
|---|
| 4993 2 1548 4 9062 6 6397 8 8296 ... |
| user output |
|---|
| 4999 1548 2 9062 4 6397 6 8296 8 ... |
Test 14
Verdict: ACCEPTED
| input |
|---|
| 5000 1594 596 1797 3776 1201 235 35... |
| correct output |
|---|
| 4993 2 1548 4 9062 6 6397 8 8296 ... |
| user output |
|---|
| 4999 1548 2 9062 4 6397 6 8296 8 ... |
