Submission details
Task:Pair sort
Sender:aalto26dw_009
Submission time:2026-09-23 16:27:17 +0300
Language:C++ (C++20)
Status:READY
Result:
Test results
testverdicttime
#10.01 sdetails
#20.01 sdetails
#30.01 sdetails
#40.01 sdetails
#50.01 sdetails
#60.01 sdetails
#70.01 sdetails
#80.01 sdetails
#90.01 sdetails
#100.01 sdetails
#110.01 sdetails
#120.01 sdetails
#130.01 sdetails
#140.01 sdetails

Code

#include<bits/stdc++.h>
//#include<unordered_set>
#pragma GCC optimize("Ofast")
using namespace std;
#define int long long
#define die(x) return cout << x << endl, 0
#define FI first
#define SE second
#define all(o) o.begin(), o.end()
#define endl '\n'
#define IOS ios::sync_with_stdio(0), cin.tie(0)
#define FILE freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define SZ(x) ((int)(x).size())
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define MP make_pair
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef map<int,int> mpi;
typedef set<int> sti;
typedef vector<pii> vii;
typedef map<pii,int> mpii;
typedef set<pii> stii;
typedef long double ld;
typedef long long ll;
int gcd(int x,int y){ return (!y ? x : gcd(y, x%y)); }
int power(int x, int y) { return (!y ? 1 : power(x, y / 2) * power(x, y / 2) * (y % 2 ? x : 1)); }
int to_int(string sconvert){stringstream geek(sconvert);int xconvert = 0; geek >> xconvert; return xconvert;}
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
int fastMin(int x, int y) { return (((y-x)>>(32-1))&(x^y))^x; }
const int MAXN=2e5+30,MAX_LOG=30,MOD=1e9+7,INF=1e9;
const double PI = acos(-1);
int mod(int x) { return (x % MOD + MOD) % MOD; }
int n, q;
// int s[MAXN*4+1],lazy[4*MAXN+1],rq[MAXN*4+1];
// int a[MAXN],ans[MAXN];
// void build(int id=1,int l=0,int r=n){
//     if(r-l<2){
//         s[id]=a[l];
//         rq[id]=a[l];
//         return;
//     }
//     int mid=(l+r)/2;
//     build(id*2,l,mid);build(id*2+1,mid,r);
//     s[id]=s[id*2]+s[id*2+1];
//     rq[id]=min(rq[id*2],rq[id*2+1]);
// }
// void upd(int id,int l,int r,int x){
//     lazy[id]+=x;
//     rq[id]+=x;
//     s[id]+=(r-l)*x;
// }
// void shift(int id,int l,int r){
//     int mid=(l+r)/2;
//     upd(id*2,l,mid,lazy[id]);
//     upd(id*2+1,mid,r,lazy[id]);
//     lazy[id]=0;
// }
// void add(int x,int y,int v,int id=1,int l=0,int r=n){
//     if(x>=r || l>=y)return;
//     if(x<=l && r<=y){
//         upd(id,l,r,v);
//         return;
//     }
//     shift(id,l,r);
//     int mid=(l+r)/2;
//     add(x,y,v,id*2,l,mid);
//     add(x,y,v,id*2+1,mid,r);
//     s[id]=s[id*2]+s[id*2+1];
//     rq[id]=min(rq[id*2],rq[id*2+1]);
// }
// int sum(int x,int y,int id=1,int l=0,int r=n){
//     if(x>=r || l>=y)return 0;
//     if(x<=l && r<=y)return s[id];
//     shift(id,l,r);
//     int mid=(l+r)/2;
//     return sum(x,y,id*2,l,mid)+sum(x,y,id*2+1,mid,r);
// }
// int rmq(int x,int y,int id=1,int l=0,int r=n){
//     if(x>=r || l>=y)return INF*INF;
//     if(x<=l && r<=y)return rq[id];
//     shift(id,l,r);
//     int mid=(l+r)/2;
//     return min(rmq(x,y,id*2,l,mid),rmq(x,y,id*2+1,mid,r));
// }

int a[MAXN];
vector<int> poses[MAXN];
int MAIN(){
    cin>>n;
    for (int i = 0; i < 2*n; i++){
        cin>>a[i];
        poses[a[i]].push_back(i + 1);
    }

    vector<pii> swaps;
    for (int i = 0; i < 2*n; i += 2){
        int val = a[i];
        int match_p = poses[a[i]][0] == i + 1 ? poses[a[i]][1] : poses[a[i]][0];
        if (match_p == i+2) continue;

        int ov = a[i+1];
        swaps.push_back({i + 1, match_p});
        a[i + 1] = a[i];
        a[match_p - 1] = ov;

        for (int j = 0; j < 2; j++){
            if (poses[val][j] == match_p) poses[val][j] = i +2;
            if (poses[ov][j] == i+2) poses[ov][j] = match_p;
        }
    }

    cout<<swaps.size()<<endl;
    for (int i = 0; i <(int)swaps.size(); i++){
        cout<<swaps[i].first<<' '<<swaps[i].second<<endl;
    }

    return 0;
}
int32_t main(){
    IOS;
    int t=1;
    //cin>>t;
    while(t--)MAIN();
    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
3
1 6
3 9
5 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
3
1 6
3 9
5 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
9
1 17
3 17
5 17
7 17
...

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
9
1 17
3 17
5 17
7 17
...

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
48
1 87
3 78
5 71
7 55
...

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
48
1 87
3 78
5 71
7 55
...

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
97
1 77
3 108
5 141
7 55
...

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
97
1 77
3 108
5 141
7 55
...

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
497
1 758
3 820
5 125
7 243
...

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
497
1 758
3 820
5 125
7 243
...

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
993
1 256
3 1534
5 816
7 1057
...

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
993
1 256
3 1534
5 816
7 1057
...

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
4993
1 1548
3 9062
5 6397
7 8296
...

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
4993
1 1548
3 9062
5 6397
7 8296
...