| Task: | Pair sort |
| Sender: | aalto26dw_009 |
| Submission time: | 2026-09-23 16:31:29 +0300 |
| Language: | C++ (C++20) |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.00 s | details |
| #2 | ACCEPTED | 0.00 s | details |
| #3 | ACCEPTED | 0.00 s | details |
| #4 | ACCEPTED | 0.00 s | details |
| #5 | ACCEPTED | 0.00 s | details |
| #6 | ACCEPTED | 0.00 s | details |
| #7 | ACCEPTED | 0.00 s | details |
| #8 | ACCEPTED | 0.00 s | details |
| #9 | ACCEPTED | 0.00 s | details |
| #10 | ACCEPTED | 0.00 s | details |
| #11 | ACCEPTED | 0.01 s | details |
| #12 | ACCEPTED | 0.00 s | details |
| #13 | ACCEPTED | 0.01 s | details |
| #14 | ACCEPTED | 0.01 s | details |
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];
int first_pos[MAXN], second_pos[MAXN];
int MAIN(){
cin>>n;
for (int i = 0; i < 2*n; i++){
cin>>a[i];
if (first_pos[a[i]] == 0) first_pos[a[i]] = i + 1;
else second_pos[a[i]] = i + 1;
}
vector<pii> swaps;
for (int i = 0; i < 2*n; i += 2){
int val = a[i];
int match_p = first_pos[val] == i + 1 ? second_pos[val] : first_pos[val];
if (match_p == i+2) continue;
int ov = a[i+1];
swaps.push_back({i + 2, match_p});
a[i + 1] = a[i];
a[match_p - 1] = ov;
if (first_pos[val] == match_p) first_pos[val] = i + 2;
else second_pos[val] = i + 2;
if (first_pos[ov] == i + 2) first_pos[ov] = match_p;
else second_pos[ov] = 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: 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 2 6 4 9 6 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 |
|---|
| 3 2 6 4 9 6 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 2 17 4 17 6 17 8 17 ... |
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 2 17 4 17 6 17 8 17 ... |
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 2 87 4 78 6 71 8 55 ... |
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 2 87 4 78 6 71 8 55 ... |
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 2 77 4 108 6 141 8 55 ... |
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 2 77 4 108 6 141 8 55 ... |
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 2 758 4 820 6 125 8 243 ... |
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 2 758 4 820 6 125 8 243 ... |
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 2 256 4 1534 6 816 8 1057 ... |
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 2 256 4 1534 6 816 8 1057 ... |
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 2 1548 4 9062 6 6397 8 8296 ... |
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 2 1548 4 9062 6 6397 8 8296 ... |
