CSES - Aalto Competitive Programming 2024 - wk4 - Wed - Results
Submission details
Task:Pair sort
Sender:aalto2024d_002
Submission time:2024-09-25 16:42:54 +0300
Language:Python3 (CPython3)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.02 sdetails
#2ACCEPTED0.02 sdetails
#3ACCEPTED0.02 sdetails
#4ACCEPTED0.02 sdetails
#5ACCEPTED0.02 sdetails
#6ACCEPTED0.02 sdetails
#7ACCEPTED0.02 sdetails
#8ACCEPTED0.02 sdetails
#9ACCEPTED0.03 sdetails
#10ACCEPTED0.02 sdetails
#11ACCEPTED0.02 sdetails
#12ACCEPTED0.02 sdetails
#13ACCEPTED0.04 sdetails
#14ACCEPTED0.04 sdetails

Code

def solve():
    n = int(input())
    arr = list(map(int, input().split()))
    from collections import defaultdict

    positions = defaultdict(list)
    for idx, val in enumerate(arr):
        positions[val].append(idx)

    swaps = []
    pivot = arr[0]  
    skip_pivot = False

    for i in range(0, 2 * n, 2):
        x = arr[i]
        if arr[i + 1] == x:
            continue
        else:
            if not skip_pivot and x == pivot:
                skip_pivot = True
                continue
            idx_list = positions[x]
        
            if idx_list[0] == i:
                other_index = idx_list[1]
            else:
                other_index = idx_list[0]

            val_i1 = arr[i + 1]
            val_other = arr[other_index]

            arr[i + 1], arr[other_index] = arr[other_index], arr[i + 1]
            swaps.append((i + 2, other_index + 1))  

            positions[val_other].remove(other_index)
            positions[val_other].append(i + 1)
            positions[val_i1].remove(i + 1)
            positions[val_i1].append(other_index)

    print(len(swaps))
    for swap in swaps:
        print(swap[0], swap[1])
solve()

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
3
4 9
6 8
8 2

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
3
4 9
6 8
8 2

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
4 2
6 2
8 2
10 12
...

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
4 2
6 2
8 2
10 12
...

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
48
4 78
6 71
8 55
10 27
...
Truncated

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
48
4 78
6 71
8 55
10 27
...
Truncated

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
97
4 108
6 141
8 55
10 52
...
Truncated

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
97
4 108
6 141
8 55
10 52
...
Truncated

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
497
4 820
6 125
8 243
10 52
...
Truncated

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
497
4 820
6 125
8 243
10 52
...
Truncated

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
993
4 1534
6 816
8 1057
10 1835
...
Truncated

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
993
4 1534
6 816
8 1057
10 1835
...
Truncated

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
4993
4 9062
6 6397
8 8296
10 8203
...
Truncated

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
4993
4 9062
6 6397
8 8296
10 8203
...
Truncated