CSES - Aalto Competitive Programming 2024 - wk4 - Wed - Results
Submission details
Task:Pair sort
Sender:aalto2024d_007
Submission time:2024-09-25 17:40:29 +0300
Language:C++ (C++20)
Status:READY
Result:
Test results
testverdicttime
#10.00 sdetails
#20.00 sdetails
#30.00 sdetails
#40.00 sdetails
#50.00 sdetails
#60.00 sdetails
#70.00 sdetails
#80.00 sdetails
#90.01 sdetails
#100.01 sdetails
#110.01 sdetails
#120.01 sdetails
#130.02 sdetails
#140.02 sdetails

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:78:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |     for (int i = 0; i < swaps.size(); i++)
      |                     ~~^~~~~~~~~~~~~~

Code

// Online C++ compiler to run C++ program online
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>

using namespace std;

int main() {
    int n;
    cin >> n;
    
    vector<int> nums(2 * n);
    vector<int> orig(2 * n);
    for (int i = 0; i < 2 * n; i++)
    {
        int t;
        cin >> t;
        nums[i] = t;
        orig[i] = t;
    }
    
    unordered_map<int, pair<int, int>> beforePosses;
    for (int i = 0; i < 2 * n; i++)
    {
        if (beforePosses.find(nums[i]) == beforePosses.end())
            beforePosses[nums[i]] = {i, -1};
        else
            beforePosses[nums[i]].second = i;
    }
    sort(nums.begin(), nums.end());
    
    vector<pair<int, int>> swaps;
    
    for (int i = 0; i < 2 * n; i += 2)
    {
        auto [p1, p2] = beforePosses[nums[i]];
        if (p1 > p2)
            swap(p1, p2);
            
        //cout << "They are at: " << p1 << " " << p2 << endl;
        
        if (i != p1)
        {
            swaps.push_back({i+1, p1+1});
            if (beforePosses[orig[i]].first == i)
            {
                //cout << "Changed " << beforePosses[orig[i]].first << " to " << p1;
                beforePosses[orig[i]].first = p1;
                orig[p1] = orig[i];
            }
            else if (beforePosses[orig[i]].second == i)
            {
                //cout << "Changed " << beforePosses[orig[i]].second << " to " << p1;
                beforePosses[orig[i]].second = p1;
                orig[p1] = orig[i];
            }
        }
        if (i + 1 != p2)
        {
            swaps.push_back({i+1+1, p2+1});
            if (beforePosses[orig[i + 1]].first == i + 1)
            {
                //cout << "Changed " << beforePosses[orig[i+1]].first << " to " << p2;
                beforePosses[orig[i + 1]].first = p2;
                orig[p2] = orig[i+1];
            }
            else if (beforePosses[orig[i + 1]].second == i + 1)
            {
                //cout << "Changed " << beforePosses[orig[i+1]].second << " to " << p2;
                beforePosses[orig[i + 1]].second = p2;
                orig[p2] = orig[i+1];
            }
        }
    }
    
    cout << swaps.size() << endl;
    for (int i = 0; i < swaps.size(); i++)
    {
        cout << swaps[i].first << " " << swaps[i].second << endl;
    }

    

    return 0;
}

Test details

Test 1

Verdict:

input
5
3 2 4 5 1 3 2 1 4 5 

correct output
3
2 6
4 9
6 8

user output
5
1 5
2 8
3 7
4 8
...

Test 2

Verdict:

input
5
3 2 4 5 1 3 2 1 4 5 

correct output
3
2 6
4 9
6 8

user output
5
1 5
2 8
3 7
4 8
...

Test 3

Verdict:

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
15
1 8
2 15
3 11
4 14
...

Test 4

Verdict:

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
15
1 8
2 15
3 11
4 14
...

Test 5

Verdict:

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
94
1 48
2 90
3 14
4 35
...
Truncated

Test 6

Verdict:

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
94
1 48
2 90
3 14
4 35
...
Truncated

Test 7

Verdict:

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
191
1 48
2 90
3 14
4 131
...
Truncated

Test 8

Verdict:

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
191
1 48
2 90
3 14
4 131
...
Truncated

Test 9

Verdict:

input
500
56 146 351 35 281 235 354 449 ...

correct output
497
2 758
4 820
6 125
8 243
...

user output
995
1 622
2 873
3 14
4 635
...
Truncated

Test 10

Verdict:

input
500
56 146 351 35 281 235 354 449 ...

correct output
497
2 758
4 820
6 125
8 243
...

user output
995
1 622
2 873
3 14
4 635
...
Truncated

Test 11

Verdict:

input
1000
603 596 351 885 530 235 354 56...

correct output
993
2 256
4 1534
6 816
8 1057
...

user output
1990
1 1614
2 1703
3 635
4 1842
...
Truncated

Test 12

Verdict:

input
1000
603 596 351 885 530 235 354 56...

correct output
993
2 256
4 1534
6 816
8 1057
...

user output
1990
1 1614
2 1703
3 635
4 1842
...
Truncated

Test 13

Verdict:

input
5000
1594 596 1797 3776 1201 235 35...

correct output
4993
2 1548
4 9062
6 6397
8 8296
...

user output
9995
1 5181
2 6211
3 1842
4 3180
...
Truncated

Test 14

Verdict:

input
5000
1594 596 1797 3776 1201 235 35...

correct output
4993
2 1548
4 9062
6 6397
8 8296
...

user output
9995
1 5181
2 6211
3 1842
4 3180
...
Truncated