Problem: 2052A - Adrenaline Rush
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 7):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll MOD18 = 1e18;
const ll MOD9  = 1e9+7;
const ll MOD = 998244353;
#define _endl "\n"
#define p2(num) (1LL << (num))
#define _ceil(a, b) (ceil((double)(a) / (b)))
#define _floor(a, b) (floor((double)(a) / (b)))
#define all(a) a.begin(),a.end()
#define LL_MAX LLONG_MAX
#define LL_MIN LLONG_MIN
#define printVec(vec) for (auto &x : (vec)) cout << x << " "; cout << '\n';
#define printRev(vec) for (auto it = (vec).rbegin(); it != (vec).rend(); ++it) cout << *it << " "; cout << '\n';
#define f(i,start,end) for(int i=(start);i<(end);++i)
#define fe(i,start,end) for(int i=(start);i>=(end);--i)
#define CTB(num) ([](long long n) { vector<int> bin; while(n > 0) { bin.push_back(n % 2); n /= 2; } reverse(bin.begin(), bin.end()); return bin; }(num))
#define CTB32(num) ([](long long n) { vector<int> bin; while(n > 0) { bin.push_back(n % 2); n /= 2; } while(bin.size() < 33) bin.push_back(0); reverse(bin.begin(), bin.end()); return bin; }(num))
#define CTB64(num) ([](long long n) { vector<int> bin; while(n > 0) { bin.push_back(n % 2); n /= 2; } while(bin.size() < 64) bin.push_back(0); reverse(bin.begin(), bin.end()); return bin; }(num))
#define getUnique(arr) ([](auto& a) { sort(begin(a), end(a)); a.erase(unique(begin(a), end(a)), end(a)); return vector<long long>(begin(a), end(a)); })(arr)
#define checkPrime(num) ((num)<=1 ? false : (num)==2 || (num)==3 ? true : (num)%2==0 || (num)%3==0 ? false : ([](int n){for(int i=5;i*i<=n;i+=6){if(n%i==0||n%(i+2)==0)return false;}return true;}(num)))
#define intn(p1, p2) ((p2.first < p1.first ? swap(p1, p2), 0 : 0), (p2.first > p1.second ? make_pair(LL_MAX, LL_MIN) : make_pair(max(p1.first, p2.first), min(p1.second, p2.second))))
#define pof(b, exp, MOD) ({ long long ans = 1, base = (b) % (MOD), exp_copy = (exp); while (exp_copy) { if (exp_copy % 2) ans = (ans * base) % (MOD); base = (base * base) % (MOD); exp_copy /= 2; } ans; })
#define IS_POWER(n) ([](long long n)->bool{if(n<=1)return false;for(long long b=2;b<=sqrt(n);++b)for(long long k=2;pow(b,k)<=n;++k)if(pow(b,k)==n)return true;return false;}(n))
#define LARGEST_BASE(n) ([](long long n)->long long{if(n<=1)return -1;for(long long b=sqrt(n);b>=2;--b)for(long long k=2;pow(b,k)<=n;++k)if(pow(b,k)==n)return b;return -1;}(n))
#define SMALLEST_BASE(n) ([](long long n)->long long{if(n<=1)return -1;for(long long b=2;b<=sqrt(n);++b)for(long long k=2;pow(b,k)<=n;++k)if(pow(b,k)==n)return b;return -1;}(n))



// using a>=b is more favourable as compared to a-b>=0, one problem gave TLE
// vector's size is returned in unsigned int and hence in minimum and maximum functions subtracting int from size can give error
// xor donot follow distributive property over +  and - , but do follow over and and or operators
// in a multiset erasing a number directly using the ms.erase(num), will remove all the occurences, while  doing that ms.erase(it.find(num)) will remove just one of the occurences
// also dont remove and erase while iterating using the auto it : method in a set or multiset
void comp(vector<pair<int,int>>&ans,vector<int>&v,int num){
    if(v.size()<=1) return;
    vector<int>v1,v2;
    int idx=-1;
    set<int>s;
    f(i,0,v.size()){ 
        if(v[i]==num){
            idx=i+1;
            f(j,i+1,v.size()) v2.push_back(v[j]);
            break;
        }
        s.insert(v[i]);
        v1.push_back(v[i]);
    }
    for(auto it : s) ans.push_back({it,num});
    if(idx==1){
        comp(ans,v2,num+1);
        // v1.push_back(v[idx-1]);
        // f(j,0,v2.size()){
        //     v1.push_back(v2[j]);
        //     fe(k,v1.size()-2,0) ans.push_back({v1.back(),v1[k]});
        //     f(k,0,v1.size()-1) ans.push_back({v1[k],v1.back()});
        // }
    }
    else{
        comp(ans,v1,num+1);
        comp(ans,v2,v[idx-2]+1);

        // v1.push_back(v[idx-1]);
        // f(j,0,v2.size()){
        //     v1.push_back(v2[j]);
        //     fe(k,v1.size()-2,0) ans.push_back({v1.back(),v1[k]});
        //     f(k,0,v1.size()-1) ans.push_back({v1[k],v1.back()});
        // }
    }
}
void solve() {
    int n;cin>>n;
    vector<int>v(n);
    map<int,int>idx;
    f(i,0,n){
        cin>>v[i];
        idx[v[i]]=i+1;
    }
    
    vector<pair<int,int>>ans;
    comp(ans,v,1);
    vector<int>v1,v2;
    v1.push_back(v[0]);
        f(j,1,v.size()){
            v1.push_back(v[j]);
            fe(k,v1.size()-2,0) if(v1.back()>v1[k]) ans.push_back({v1.back(),v1[k]});
            f(k,0,v1.size()-1) if(v1.back()>v1[k]) ans.push_back({v1[k],v1.back()});
        }
    cout<<ans.size()<<_endl;
    for(auto it : ans){
        cout<<it.first<<" "<<it.second<<_endl;
    }
}



signed main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
#ifndef ONLINE_JUDGE
    freopen("lmaoinp.txt", "r", stdin); freopen("lmaooutput_.txt", "w", stdout);
#endif
    int test=1; //cin >> test; 
    while (test--) solve();
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll MOD18 = 1e18;
const ll MOD9  = 1e9+7;
const ll MOD = 998244353;
#define _endl "\n"
#define p2(num) (1LL << (num))
#define _ceil(a, b) (ceil((double)(a) / (b)))
#define _floor(a, b) (floor((double)(a) / (b)))
#define all(a) a.begin(),a.end()
#define LL_MAX LLONG_MAX
#define LL_MIN LLONG_MIN
#define printVec(vec) for (auto &x : (vec)) cout << x << " "; cout << '\n';
#define printRev(vec) for (auto it = (vec).rbegin(); it != (vec).rend(); ++it) cout << *it << " "; cout << '\n';
#define f(i,start,end) for(int i=(start);i<(end);++i)
#define fe(i,start,end) for(int i=(start);i>=(end);--i)
#define CTB(num) ([](long long n) { vector<int> bin; while(n > 0) { bin.push_back(n % 2); n /= 2; } reverse(bin.begin(), bin.end()); return bin; }(num))
#define CTB32(num) ([](long long n) { vector<int> bin; while(n > 0) { bin.push_back(n % 2); n /= 2; } while(bin.size() < 33) bin.push_back(0); reverse(bin.begin(), bin.end()); return bin; }(num))
#define CTB64(num) ([](long long n) { vector<int> bin; while(n > 0) { bin.push_back(n % 2); n /= 2; } while(bin.size() < 64) bin.push_back(0); reverse(bin.begin(), bin.end()); return bin; }(num))
#define getUnique(arr) ([](auto& a) { sort(begin(a), end(a)); a.erase(unique(begin(a), end(a)), end(a)); return vector<long long>(begin(a), end(a)); })(arr)
#define checkPrime(num) ((num)<=1 ? false : (num)==2 || (num)==3 ? true : (num)%2==0 || (num)%3==0 ? false : ([](int n){for(int i=5;i*i<=n;i+=6){if(n%i==0||n%(i+2)==0)return false;}return true;}(num)))
#define intn(p1, p2) ((p2.first < p1.first ? swap(p1, p2), 0 : 0), (p2.first > p1.second ? make_pair(LL_MAX, LL_MIN) : make_pair(max(p1.first, p2.first), min(p1.second, p2.second))))
#define pof(b, exp, MOD) ({ long long ans = 1, base = (b) % (MOD), exp_copy = (exp); while (exp_copy) { if (exp_copy % 2) ans = (ans * base) % (MOD); base = (base * base) % (MOD); exp_copy /= 2; } ans; })
#define IS_POWER(n) ([](long long n)->bool{if(n<=1)return false;for(long long b=2;b<=sqrt(n);++b)for(long long k=2;pow(b,k)<=n;++k)if(pow(b,k)==n)return true;return false;}(n))
#define LARGEST_BASE(n) ([](long long n)->long long{if(n<=1)return -1;for(long long b=sqrt(n);b>=2;--b)for(long long k=2;pow(b,k)<=n;++k)if(pow(b,k)==n)return b;return -1;}(n))
#define SMALLEST_BASE(n) ([](long long n)->long long{if(n<=1)return -1;for(long long b=2;b<=sqrt(n);++b)for(long long k=2;pow(b,k)<=n;++k)if(pow(b,k)==n)return b;return -1;}(n))



// using a>=b is more favourable as compared to a-b>=0, one problem gave TLE
// vector's size is returned in unsigned int and hence in minimum and maximum functions subtracting int from size can give error
// xor donot follow distributive property over +  and - , but do follow over and and or operators
// in a multiset erasing a number directly using the ms.erase(num), will remove all the occurences, while  doing that ms.erase(it.find(num)) will remove just one of the occurences
// also dont remove and erase while iterating using the auto it : method in a set or multiset
void solve() {
    int n;cin>>n;
    vector<int>v(n);
    set<int>s;
    f(i,0,n){
        cin>>v[i];
        s.insert(v[i]);
    }
    vector<pair<int,int>>ans;
   f(i,0,n){
    vector<int>v_;
     for(auto it : s){
         if(it==v[i]){
            vector<int>v1;
            int k=it+1;
            while(k<=*(s.rbegin())){
                if(s.find(k)==s.end()){k++;continue;}
                ans.push_back({k,v[i]});
                v1.push_back(k);
                k++;
            }
            fe(j,v1.size()-1,0) ans.push_back({v[i],v1[j]});
            break;
         }
         else v_.push_back(it);
     }
     fe(j,v_.size()-1,0) {ans.push_back({v[i],v_[j]});}
     s.erase(v[i]);
   }
    cout<<ans.size()<<_endl;
    for(auto it : ans){
        cout<<it.first<<" "<<it.second<<_endl;
    }
}



signed main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
#ifndef ONLINE_JUDGE
    freopen("lmaoinp.txt", "r", stdin); freopen("lmaooutput_.txt", "w", stdout);
#endif
    int test=1; //cin >> test; 
    while (test--) solve();
}