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

Previous submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define pb push_back
#define show(x) cout<<(x)<<endl;
#define sz(x) (int)(x).size()
#define int long long int
#define all(x) (x).begin(),(x).end()
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;

const int MAX_N = 2e5 + 1;
const int MOD = 1e9 + 7;


int modInverse(int A, int M) {
    int m0 = M;
    int y = 0, x = 1;

    if (M == 1)
        return 0;

    while (A > 1) {
        // q is quotient
        int q = A / M;
        int t = M;

        // m is remainder now, process same as
        // Euclid's algo
        M = A % M, A = t;
        t = y;

        // Update y and x
        y = x - q * y;
        x = t;
    }

    // Make x positive
    if (x < 0)
        x += m0;

    return x;
}

vi seg;
vi lazy;

void lazyUpdate(int ind,int start,int end){
    if(lazy[ind]!=INT64_MAX){
        seg[ind]  = min(seg[ind],lazy[ind]);
        if(start!=end){
            lazy[2*ind+1] = min(lazy[2*ind+1],lazy[ind]);
            lazy[2*ind+2] = min(lazy[2*ind+2],lazy[ind]);
        }
        lazy[ind] = INT64_MAX;
    }
}

void build(int ind, int start, int end,vi & arr) {
    if (start == end) {
        seg[ind] = arr[start];
        return;
    }
    int mid = (start + end) / 2;
    build(2 * ind+1, start, mid,arr);
    build(2 * ind + 2, mid + 1, end,arr);
    seg[ind] = min(seg[2 * ind+1], seg[2 * ind + 2]);
}

void rangeUpdate(int ind, int start, int end, int l,int r, int val) {
    lazyUpdate(ind,start,end);
    if(r<start||l>end) return;
    if(start>=l && end<=r){
        lazy[ind] = min(lazy[ind],val);
        lazyUpdate(ind,start,end);
        return;
    }
    int mid = (start+end)/2;
    rangeUpdate(2 * ind + 1, start, mid, l,r, val);
    rangeUpdate(2 * ind + 2, mid + 1, end, l,r, val);
    seg[ind] = min(seg[2 * ind+1], seg[2 * ind + 2]);

}

int query(int ind, int start, int end, int l, int r) {
    lazyUpdate(ind,start,end);
    if(r<start||l>end) return INT64_MAX;
    if(start>=l && end<=r){
        return seg[ind];
    }
    int mid = (start + end) / 2;
    return min(query(2 * ind+1, start, mid, l, r), query(2 * ind + 2, mid + 1, end, l, r)); 
}


void solve(){
    int n;
    cin>>n;
    vi c(n),b(n);
    rep(i,n){
    	cin>>c[i];
        b[i] = i+1;
    }	
    int res=0;
    vector<P> ans;
    vi visited(n+1,0);
    for(int i=n-1;i>=0;i--){
        for(int j=c[i]+1;j<=n;j++){
            if(!visited[j]){
                ans.pb({j,c[i]});
            }
        }
        for(int j=1;j<c[i];j++){
            if(!visited[j]){
                ans.pb({c[i],j});
                // ans.pb({j,c[i]});
            }
        }
        for(int j=1;j<c[i];j++){
            if(!visited[j]){
                ans.pb({j,c[i]});
                // ans.pb({j,c[i]});
            }
        }
        visited[c[i]]=1;
    }
    cout<<ans.size()<<endl;
    for(auto it:ans){
        cout<<it.fi<<" "<<it.se<<endl;
    }
}

int32_t main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

Submission (verdict: Wrong answer on test 7):
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define pb push_back
#define show(x) cout<<(x)<<endl;
#define sz(x) (int)(x).size()
#define int long long int
#define all(x) (x).begin(),(x).end()
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;

const int MAX_N = 2e5 + 1;
const int MOD = 1e9 + 7;


int modInverse(int A, int M) {
    int m0 = M;
    int y = 0, x = 1;

    if (M == 1)
        return 0;

    while (A > 1) {
        // q is quotient
        int q = A / M;
        int t = M;

        // m is remainder now, process same as
        // Euclid's algo
        M = A % M, A = t;
        t = y;

        // Update y and x
        y = x - q * y;
        x = t;
    }

    // Make x positive
    if (x < 0)
        x += m0;

    return x;
}

vi seg;
vi lazy;

void lazyUpdate(int ind,int start,int end){
    if(lazy[ind]!=INT64_MAX){
        seg[ind]  = min(seg[ind],lazy[ind]);
        if(start!=end){
            lazy[2*ind+1] = min(lazy[2*ind+1],lazy[ind]);
            lazy[2*ind+2] = min(lazy[2*ind+2],lazy[ind]);
        }
        lazy[ind] = INT64_MAX;
    }
}

void build(int ind, int start, int end,vi & arr) {
    if (start == end) {
        seg[ind] = arr[start];
        return;
    }
    int mid = (start + end) / 2;
    build(2 * ind+1, start, mid,arr);
    build(2 * ind + 2, mid + 1, end,arr);
    seg[ind] = min(seg[2 * ind+1], seg[2 * ind + 2]);
}

void rangeUpdate(int ind, int start, int end, int l,int r, int val) {
    lazyUpdate(ind,start,end);
    if(r<start||l>end) return;
    if(start>=l && end<=r){
        lazy[ind] = min(lazy[ind],val);
        lazyUpdate(ind,start,end);
        return;
    }
    int mid = (start+end)/2;
    rangeUpdate(2 * ind + 1, start, mid, l,r, val);
    rangeUpdate(2 * ind + 2, mid + 1, end, l,r, val);
    seg[ind] = min(seg[2 * ind+1], seg[2 * ind + 2]);

}

int query(int ind, int start, int end, int l, int r) {
    lazyUpdate(ind,start,end);
    if(r<start||l>end) return INT64_MAX;
    if(start>=l && end<=r){
        return seg[ind];
    }
    int mid = (start + end) / 2;
    return min(query(2 * ind+1, start, mid, l, r), query(2 * ind + 2, mid + 1, end, l, r)); 
}


void solve(){
    int n;
    cin>>n;
    vi c(n),b(n);
    rep(i,n){
    	cin>>c[i];
        b[i] = i+1;
    }	
    int res=0;
    vector<P> ans;
    vi visited(n+1,0);
    for(int i=n-1;i>=0;i--){
        for(int j=c[i]+1;j<=n;j++){
            if(!visited[j]){
                ans.pb({j,c[i]});
            }
        }
        for(int j=c[i]-1;j>=1;j--){
            if(!visited[j]){
                ans.pb({c[i],j});
                // ans.pb({j,c[i]});
            }
        }
        for(int j=1;j<c[i];j++){
            if(!visited[j]){
                ans.pb({j,c[i]});
                // ans.pb({j,c[i]});
            }
        }
        visited[c[i]]=1;
    }
    cout<<ans.size()<<endl;
    for(auto it:ans){
        cout<<it.fi<<" "<<it.se<<endl;
    }
}

int32_t main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}