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

Previous submission (verdict: Wrong answer on test 5):
#include <bits/stdc++.h>
#define all(a) (a).begin(), (a).end()
#define ll long long
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define fi first
#define se second
#define pb(x) push_back(x)
#define vec vector<ll>
using namespace std;

void solve() {
    ll n;
    cin >> n;
    vec c(n + 1);
    fr(i, 1, n + 1) cin >> c[i];

    // Count inversions using multiset
    multiset<ll> s;
    ll inv_count = 0;

    for (ll i = n; i >= 1; i--) {
        // Count elements smaller than c[i]
        inv_count += distance(s.begin(), s.lower_bound(c[i]));
        // Insert the current element
        s.insert(c[i]);
    }

    // Maximum overtakes
    ll max_overtakes = n * (n - 1) - inv_count;
    cout << max_overtakes << endl;

    // Generate overtakes using set for uniqueness and vector for order
    set<pair<ll, ll>> overtakes_set;
    vector<pair<ll, ll>> overtakes_vector;

    for (ll i = n; i >= 1; i--) {
        ll current = c[i];
        
        // Current car overtakes all cars with numbers less than current
        for (ll j = current - 1; j >= 1; j--) {
            if (overtakes_set.insert({current, j}).second) {
                overtakes_vector.push_back({current, j}); // Store in vector if unique
            }
        }
        // All earlier cars overtake the current car
        for (ll j = 1; j < i; j++) {
            if (c[j] != current) {
                if (overtakes_set.insert({c[j], current}).second) {
                    overtakes_vector.push_back({c[j], current}); // Store in vector if unique
                }
            }
        }
    }

    // Output all unique overtakes in order
    for (auto &x : overtakes_vector) {
        cout << x.fi << " " << x.se << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    solve();
    return 0;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
#define all(a) (a).begin(), (a).end()
#define ll long long
#define fr(i,a,b) for (ll i = a; i < b; i++)
#define fr1(i,a,b) for (ll i = a-1; i>=b; i--)
#define fi first
#define se second
#define mp(j, k) make_pair(j, k)
#define pb(x) push_back(x)
#define in(x) insert(x)
#define vec vector<ll>
#define yes cout<<"YES\n";
#define no cout<<"NO\n";
#define ac 1000000007;
#define fauto(a) for(auto i:a) cout<<i<<" ";
#define fautop(a) for(auto i:a) cout<<i.fi<<" "<<i.se<<endl;
using namespace std;
bool comp(vec a,vec b){
  return a[1]>b[1];
}

ll dp[1010][1010];

void solve(){
ll n; cin>>n;
vec v(n+1);
for(ll i=1;i<=n;i++){cin>>v[i];}
vec a(n+1);
fr(i,1,n+1){a[i]=i;}
vector<pair<ll,ll>> p;
set<pair<ll,ll>> m;
vec vis(n+1,0);

for(ll i=n;i>=1;i--){
    ll z=v[i];
    for(ll i=z;i>=1;i--){
        if(i!=z && m.find({z,i})==m.end() && vis[i]==0){
            m.insert({z,i});
            p.push_back({z,i});

        }
        }

         for(ll i=1;i<=n;i++){
        if(i!=z && m.find({i,z})==m.end() && vis[i]==0){
            m.insert({i,z});
            p.push_back({i,z});

        }
        }
        vis[z]=1;


}
cout<<m.size()<<endl;
// reverse(all(p));
for(auto x:p){
    cout<<x.fi<<" "<<x.se<<endl;
}



}


int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    // ll tc;
    // cin >> tc;
    // while (tc--) {
        solve();
    // }
    
}