-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxorSort.cpp
More file actions
64 lines (53 loc) · 1.1 KB
/
xorSort.cpp
File metadata and controls
64 lines (53 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(a) a.begin(),a.end()
#define tr(c,it) for(typeof(c.begin()) it=c.begin();it!=c.end();it++)
#define multi ll t;cin>>t;while(t--)
#define present(c,i) (c.find(i)!=c.end())
#define cpresent(c,i) (find(all(c),i)!=c.end())
#define mod 1000000007
void solve()
{
ll n,i,j,k,u,v;
cin>>n;
vector<ll> a(n),b(n);
for(i=0;i<n;i++)
{
cin>>a[i];
b[i]=a[i];
}
sort(b.begin(),b.end());
unordered_map<ll,set<ll>> m;
for(i=0;i<n;++i)
m[a[i]].insert(i);
vector<pair<ll,ll> > ans;
for(i=0;i<n;++i)
{
j=*m[b[i]].begin();// position of actual value from sorted array in original array
m[b[i]].erase(j);
if(i==j) continue;
ans.push_back({i,j});
ans.push_back({j,i});
ans.push_back({i,j});
m[a[i]].erase(i);
m[a[i]].insert(j);
swap(a[i],a[j]);
}
cout<<ans.size()<<"\n";
for(auto x:ans)
{
cout<<x.first<<" "<<x.second<<"\n";
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--)
solve();
return 0;
}