Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #include<ext/pb_ds/assoc_container.hpp>
- #include<ext/pb_ds/tree_policy.hpp>
- using namespace __gnu_pbds;
- using namespace std;
- #define IO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
- #define pb push_back
- #define all(x) (x).begin(),(x).end()
- #define ll long long
- #define ull unsigned long long
- #define ld long double
- #define eps 1e-9
- #define sz(a) (ll)(a).size()
- #define ppc __builtin_popcount
- #define ppcll __builtin_popcountll
- #define mem1(a) memset(a,-1,sizeof(a))
- #define mem0(a) memset(a,0,sizeof(a))
- #define endl "\n"
- #define lb lower_bound
- #define ub upper_bound
- template<class T> using ordered_set = tree<T, null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
- template<class T> using ordered_multiset = tree<T, null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>;
- const ld PI = acos(-1.0);
- const int MOD = 1e9 +7;
- const ll INF = 1e18;
- // if(abs(a-b)<eps) --> if(a==b)
- // fixed << setprecision(n) -->printing decimal till n
- // hypot(a ,b) --> sqrt(a^2 + b^2)
- void solve(){
- int n; cin>>n;
- vector<int> a(n);
- for(int i=0;i<n;i++) cin>>a[i];
- set<pair<int,int>> s;
- for(int i=0;i<n;i++){
- s.insert({a[i],i});
- }
- vector<pair<int,int>> ans;
- int want =0;
- while(!s.empty()){
- int val = s.begin()->first;
- int ind_old = s.begin()->second;
- if(want==ind_old){
- s.erase(s.begin());
- want++;
- continue;
- }
- s.erase(s.begin());
- s.erase(s.find({a[want],want}));
- s.insert({a[want] , ind_old});
- ans.push_back({want , ind_old});
- swap(a[want] , a[ind_old]);
- want++;
- }
- cout<<3*ans.size()<<endl;
- for(auto& it: ans){
- int i = it.first+1;
- int j = it.second+1;
- cout<<i<<" "<<j<<endl;
- cout<<j<<" "<<i<<endl;
- cout<<i<<" "<<j<<endl;
- }
- }
- int main(){
- IO;
- ll t =1;
- cin>>t;
- for(int i=1;i<=t;i++){
- // cout<<"Case #"<<i<<": ";
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment