#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define eb emplace_back
#define task ""
#define fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define nx freopen (task".inp","r",stdin), freopen (task".out","w",stdout);
#define fi first
#define se second
#define pii pair <int, int>
#define tii tuple <int, int, int>
#define all(s) s.begin(), s.end()
using namespace std;

void solve()
{
    int n;
    cin >> n;
    vector <int> c(n), mp(n + 2, 0);
    for (auto &i : c) cin >> i; //thứ tự gần vạch xuất phát của từng xe
    vector <pii> ans;
    for (int i = n - 1; i >= 0; --i)
    {
        mp[c[i]] = 1;
        for (int j = c[i] - 1; j > 0; --j)
            if (!mp[j])
                ans.eb(c[i], j);
        for (int j = n - 1; j > i; --j)
            ans.eb(c[i], c[j]);
    }
    cout << ans.size() << '\n';
    for (auto &[u, v] : ans)
        cout << u << ' ' << v << '\n';
}

int main()
{
    if (ifstream(task".inp")) nx
    fast

    solve();
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

int main() {
    int n;
    cin >> n;

    vector<int> final_order(n);
    for (int i = 0; i < n; ++i) {
        cin >> final_order[i];
    }

    if (n <= 1) {
        cout << 0 << endl;
        return 0;
    }

    vector<pair<int, int>> overtakes;
    if (n == 2) {
        if (final_order[0] != 1 || final_order[1] != 2) {
            cout << 1 << endl;
            cout << final_order[0] << " " << final_order[1] << endl;
        } else {
            cout << 0 << endl;
        }
        return 0;
    }
     if (n == 3) {
        if (final_order == vector<int>{2, 3, 1}) {
            cout << 3 << endl;
            cout << 2 << " " << 1 << endl;
            cout << 3 << " " << 1 << endl;
            cout << 3 << " " << 2 << endl;
            return 0;
        } else if (final_order == vector<int>{1, 3, 2}) {
             cout << 1 << endl;
             cout << 3 << " " << 2 << endl;
             return 0;
        } else if (final_order == vector<int>{2, 1, 3}) {
            cout << 1 << endl;
            cout << 2 << " " << 1 << endl;
            return 0;
        } else if (final_order == vector<int>{3, 1, 2}) {
            cout << 2 << endl;
            cout << 3 << " " << 1 << endl;
            cout << 3 << " " << 2 << endl;
            return 0;
        } else if (final_order == vector<int>{3, 2, 1}) {
            cout << 3 << endl;
            cout << 3 << " " << 2 << endl;
            cout << 3 << " " << 1 << endl;
            cout << 2 << " " << 1 << endl;
            return 0;
        } else {
            cout << 0 << endl;
            return 0;
        }
    }

    if (n <= 10) {
        vector<int> initial_order(n);
        for (int i = 0; i < n; ++i) {
            initial_order[i] = i + 1;
        }

        int max_overtakes = 0;
        vector<pair<int, int>> best_overtakes;

        function<void(vector<int>, vector<pair<int, int>>)> solve =
            [&](vector<int> current_order, vector<pair<int, int>> current_overtakes) {
            if (current_order == final_order) {
                if (current_overtakes.size() > max_overtakes) {
                    max_overtakes = current_overtakes.size();
                    best_overtakes = current_overtakes;
                }
                return;
            }

            if (current_overtakes.size() >= n * (n - 1) / 2) return; // Optimization

            for (int i = 0; i < current_order.size() - 1; ++i) {
                int overtaker = current_order[i];
                int overtaken = current_order[i + 1];

                bool already_overtook = false;
                for (const auto& overtake : current_overtakes) {
                    if (overtake.first == overtaker && overtake.second == overtaken) {
                        already_overtook = true;
                        break;
                    }
                }
                if (!already_overtook) {
                    vector<int> next_order = current_order;
                    swap(next_order[i], next_order[i + 1]);
                    vector<pair<int, int>> next_overtakes = current_overtakes;
                    next_overtakes.push_back({overtaker, overtaken});
                    solve(next_order, next_overtakes);
                }
            }
        };

        solve(initial_order, {});

        cout << max_overtakes << endl;
        for (const auto& overtake : best_overtakes) {
            cout << overtake.first << " " << overtake.second << endl;
        }
    } else {
        cout << 0 << endl;
    }

    return 0;
}

Answer: 1


// #include <bits/stdc++.h>
// using namespace std;
// #define lli long long  lli
// lli r = 1000006, l, d, lim = 1e6 + 1;
// map<string, lli> gp;
// lli rc[1000009];
// vector<bool> prime(lim);
// const  long long lli u = 10e9 + 7;
// vector<lli> nprime;

// void seive()
// {
//   prime[1] = 1;
//   prime[0] = 1;
//   for ( lli i = 4; i <= lim; i += 2)
//     prime[i] = 1;
//   nprime.push_back(2);

//   lli x = 0;
//   for ( lli i = 3; i <= lim; i += 2)
//   {
//     if (prime[i] == 0)
//     {

//       nprime.push_back(i);
//       // prime[i]=1;
//       for ( lli j = i * i; j <= lim; j += i * 2)
//       {
//         prime[j] = 1;
//       }
//     }
//   }
// }
// lli aexb(lli a, lli b)
// {
//   if (b == 0)
//     return 1;
//   lli x = aexb(a, b / 2);
//   x = (x * x) % u;
//   if (b & 1)
//     x = (x * a) % u;
//   return x;
// }
// lli fact()
// {
//   rc[0] = 1;
//   for (lli i = 1; i <= r; i++)
//     rc[i] = (rc[i] * i) % u;
// }

// lli ncr(lli n, lli m)
// {
//   lli a = 1, b = 1;
//   for (lli i = 1; i <= m; i++)
//   {
//     a *= i;
//     a %= u;
//   }
//   for (lli i = n; i > n - m; i--)
//   {
//     b *= i;
//     b %= u;
//   }
//   return (b / a);
// }
// // string rev(string s, lli a, lli b)
// // {
// //   lli j = 0;
// //   for (lli i = a; i <= a + (b - a) / 2; i++)
// //   {

// //     swap(s[i], s[b - j]);
// //     // cout<<i<<" "<<s<<" "<<b-j<<endl;
// //     j++;
// //   }
// //   return s;
// // }

// double distance(lli x1, lli y1, lli x2, lli y2)
// {
//   double dis = (x1 - x2) * (x1 - x2);
//   dis += ((y1 - y2) * (y1 - y2));
//   dis = sqrt(dis);
//   return dis;
// }
// vector<lli> base(200005);
// lli logbase3(lli n)
// {
//   lli cnt = 0;
//   while (n)
//   {
//     n /= 3;
//     cnt++;
//   }
//   return cnt;
// }
// lli sum(lli n)
// {
//   return ((n * (n + 1)) / 2);
// }
// lli vcr[(lli)1e5+1][2];
// string str, str1;
// lli m=0, k,n, W;
// string s="aeiou";

#include <bits/stdc++.h>
using namespace std;
#define lli long long int
const lli MOD = 1e9 + 7;
const lli BASE = 31;
lli dp[3005][3005][2];

lli find(lli r, vector<lli>&par)
{
    if(par[r]==r)return r;
    return find(par[r], par);
}
void dsu(vector<lli>&vc, vector<lli>&par, lli n)
{
    vector<lli>visited(n+1,0);
    for(lli i=1;i<=n;i++)
        {
            if(visited[i])continue;
            queue<lli>q;
            q.push(i);
            while(!q.empty())
            {
                lli u=q.front();
                visited[u]=1;
                q.pop();

                lli a=find(u, par);
                lli b=find(vc[u], par);
                if(visited[vc[u]])
                {
                    par[a]=b;
                    continue;
                }
                if(a!=b)
                par[b]=a;

                q.push(vc[u]);
            }

        }
}
// Custom hash function for pair<int, int>
struct pair_hash {
    size_t operator()(const pair<int, int>& p) const {
        return hash<int>()(p.first) ^ hash<int>()(p.second);
    }
};
int main()
{

  lli t = 1, ca = 1;
 // cin >> t;

  while (t--)
    {
        lli n,m,b=0;
        cin>>n;
        vector<int>a(n+1,0);
        for(int i=1; i<=n; ++i) cin>>a[i];
        vector<pair<int,int>> ans;
        for(int i=1; i<=n; ++i)
            for(int j=1; j<n; ++j)
                if(a[j]<a[j+1])
                   { swap(a[j],a[j+1]),
                    ans.push_back({a[j],a[j+1]});
                    //cout<<a[j]<<" "<<a[j+1]<<endl;
                   }
        for(int i=1; i<=n; ++i)
            for(int j=1; j<n; ++j)
                if(a[j]>a[j+1])
                   { swap(a[j],a[j+1]),
                    ans.push_back({a[j],a[j+1]});
                    //cout<<a[j]<<" "<<a[j+1]<<endl;
                   }
        reverse(ans.begin(),ans.end());
        cout<<ans.size()<<endl;
        for(auto it:ans)
        cout<<it.second<<" "<<it.first<<endl;
    }
}

// #include <bits/stdc++.h>
// using namespace std;
// #define lli long long  lli
// lli r = 1000006, l, d, lim = 1e6 + 1;
// map<string, lli> gp;
// lli rc[1000009];
// vector<bool> prime(lim);
// const  long long lli u = 10e9 + 7;
// vector<lli> nprime;

// void seive()
// {
//   prime[1] = 1;
//   prime[0] = 1;
//   for ( lli i = 4; i <= lim; i += 2)
//     prime[i] = 1;
//   nprime.push_back(2);

//   lli x = 0;
//   for ( lli i = 3; i <= lim; i += 2)
//   {
//     if (prime[i] == 0)
//     {

//       nprime.push_back(i);
//       // prime[i]=1;
//       for ( lli j = i * i; j <= lim; j += i * 2)
//       {
//         prime[j] = 1;
//       }
//     }
//   }
// }
// lli aexb(lli a, lli b)
// {
//   if (b == 0)
//     return 1;
//   lli x = aexb(a, b / 2);
//   x = (x * x) % u;
//   if (b & 1)
//     x = (x * a) % u;
//   return x;
// }
// lli fact()
// {
//   rc[0] = 1;
//   for (lli i = 1; i <= r; i++)
//     rc[i] = (rc[i] * i) % u;
// }

// lli ncr(lli n, lli m)
// {
//   lli a = 1, b = 1;
//   for (lli i = 1; i <= m; i++)
//   {
//     a *= i;
//     a %= u;
//   }
//   for (lli i = n; i > n - m; i--)
//   {
//     b *= i;
//     b %= u;
//   }
//   return (b / a);
// }
// // string rev(string s, lli a, lli b)
// // {
// //   lli j = 0;
// //   for (lli i = a; i <= a + (b - a) / 2; i++)
// //   {

// //     swap(s[i], s[b - j]);
// //     // cout<<i<<" "<<s<<" "<<b-j<<endl;
// //     j++;
// //   }
// //   return s;
// // }

// double distance(lli x1, lli y1, lli x2, lli y2)
// {
//   double dis = (x1 - x2) * (x1 - x2);
//   dis += ((y1 - y2) * (y1 - y2));
//   dis = sqrt(dis);
//   return dis;
// }
// vector<lli> base(200005);
// lli logbase3(lli n)
// {
//   lli cnt = 0;
//   while (n)
//   {
//     n /= 3;
//     cnt++;
//   }
//   return cnt;
// }
// lli sum(lli n)
// {
//   return ((n * (n + 1)) / 2);
// }
// lli vcr[(lli)1e5+1][2];
// string str, str1;
// lli m=0, k,n, W;
// string s="aeiou";

#include <bits/stdc++.h>
using namespace std;
#define lli long long int
const lli MOD = 1e9 + 7;
const lli BASE = 31;
lli dp[3005][3005][2];

lli find(lli r, vector<lli>&par)
{
    if(par[r]==r)return r;
    return find(par[r], par);
}
void dsu(vector<lli>&vc, vector<lli>&par, lli n)
{
    vector<lli>visited(n+1,0);
    for(lli i=1;i<=n;i++)
        {
            if(visited[i])continue;
            queue<lli>q;
            q.push(i);
            while(!q.empty())
            {
                lli u=q.front();
                visited[u]=1;
                q.pop();

                lli a=find(u, par);
                lli b=find(vc[u], par);
                if(visited[vc[u]])
                {
                    par[a]=b;
                    continue;
                }
                if(a!=b)
                par[b]=a;

                q.push(vc[u]);
            }

        }
}
// Custom hash function for pair<int, int>
struct pair_hash {
    size_t operator()(const pair<int, int>& p) const {
        return hash<int>()(p.first) ^ hash<int>()(p.second);
    }
};
int main()
{

  lli t = 1, ca = 1;
 // cin >> t;

  while (t--)
    {
        lli n,m,a=0,b=0;
        cin>>n;
        vector<int>present(n+1,0), future(n+1,0), pos(n+1,0);
        priority_queue<pair<int, int>>pq;
        for(int i=1;i<=n;i++)
        {
            cin>>m;
            future[m]=i;
            present[i]=i;
            pos[i]=i;
            if(a<abs(future[i]-present[i]))
            {
                a=abs(future[i]-present[i]);
                b=i;
            }
            pq.push({(abs(future[i]-present[i])),i});
        }
        unordered_map<pair<int, int>, int, pair_hash> mp;

        while(!pq.empty())
        {
            int u=pq.top().second;
            int c=pq.top().first;
            pq.pop();
            if(present[u]==future[u])continue;
            int i=present[u];
            //cout<<u<< " "<<c<< " "<<i<<endl;
            while(present[u]!=future[u])
            {
                if(present[u]<future[u])
                {
                    int val=pos[i+1];
                    pos[i+1]=u;
                    pos[i]=val;
                    present[val]=i;
                    mp[{val,u}]++;
                    i++;
                    present[u]=i;
                    pq.push({abs(present[val]-future[val]),val});

                }
                else if(present[u]>future[u])
                {
                    int val=pos[i-1];
                    swap(pos[i-1],pos[i]);
                    present[val]=i;
                    mp[{u,val}]++;
                    i--;
                    present[u]=i;
                   // cout<<val<<" "<<u<<" "<<i<<endl;
                    pq.push({abs(present[val]-future[val]),val});
                }
                else
                break;

            }
           // break;
        }
        for(int i=1;i<n;i++)
        {
            int j=i+1;
            while(j<=n&&mp.find({present[i], present[j]})==mp.end()&&mp.find({ present[j],present[i]})==mp.end())
            {

                mp[{present[i], present[j]}]++;
                mp[{ present[j],present[i]}]++;
                j++;
            }
        }
        cout<<mp.size()<<endl;
        for(auto it:mp)
        {
            cout<<it.first.first<<" "<<it.first.second<<endl;
        }

    }
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<long long int> vi;
#define pb push_back
#define mp make_pair
#define all(c) c.begin(), c.end()
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rev(i, a, b) for (int i = a; i >= b; i--)
#define get(n) \
    ll n;      \
    cin >> n;
#define gets(n, m) \
    ll n, m;       \
    cin >> n >> m;
#define getss(n, m, o) \
    ll n, m, o;        \
    cin >> n >> m >> o;
#define show(n) cout << n << endl;
#define getv(n) \
    vi v(n);    \
    loop(i, 0, n) cin >> v[i];
#define sort(v) sort(all(v));
#define getstr(s) \
    string s;     \
    cin >> s;
#define showstr(s) cout << s << endl;
#define showv(v) loop(i, 0, v.size()) cout << v[i] << " ";
#define endl "\n";
#define sp " ";
#define getmatrix(n, m)                     \
    vector<vector<ll>> v(n, vector<ll>(m)); \
    loop(i, 0, n) loop(j, 0, m) cin >> v[i][j];
#define showmatrix(n, m)                      \
    loop(i, 0, n)                             \
    {                                         \
        loop(j, 0, m) cout << v[i][j] << " "; \
        cout << endl;                         \
    }
#define gethashmap(n) \
    map<ll, ll> m;    \
    loop(i, 0, n)     \
    {                 \
        int a;        \
        cin >> a;     \
        m[a]++;       \
    }
#define showhashmap                                \
    for (auto it = m.begin(); it != m.end(); it++) \
        cout << it->first << sp << it->second << endl;
#define getset(n)    \
    set<int> s;      \
    loop(i, 0, n)    \
    {                \
        int a;       \
        cin >> a;    \
        s.insert(a); \
    }
#define sumv(v) accumulate(all(v), 0);

long long int NcR(int n, int r)
{

    // p holds the value of n*(n-1)*(n-2)...,
    // k holds the value of r*(r-1)...
    long long p = 1, k = 1;

    // C(n, r) == C(n, n-r),
    // choosing the smaller value
    if (n - r < r)
        r = n - r;

    if (r != 0)
    {
        while (r)
        {
            p *= n;
            k *= r;

            // gcd of p, k
            long long m = __gcd(p, k);

            // dividing by gcd, to simplify
            // product division by their gcd
            // saves from the overflow
            p /= m;
            k /= m;

            n--;
            r--;
        }

        // k should be simplified to 1
        // as C(n, r) is a natural number
        // (denominator should be 1 ) .
    }

    else
        p = 1;

    // if our approach is correct p = ans and k =1
    return p;
}

void yesno(bool c)
{
    if (c)
    {
        cout << "YES" << endl;
    }
    else
    {
        cout << "NO" << endl;
    }
}
constexpr int N = 2E7;
vector<int> minp, primes;
void sieve(int n)
{
    minp.assign(n + 1, 0);
    primes.clear();

    for (int i = 2; i <= n; i++)
    {
        if (minp[i] == 0)
        {
            minp[i] = i;
            primes.push_back(i);
        }

        for (auto p : primes)
        {
            if (i * p > n)
            {
                break;
            }
            minp[i * p] = p;
            if (p == minp[i])
            {
                break;
            }
        }
    }
}
ll power(ll x, int y, int p)
{
    ll res = 1; // Initialize result

    x = x % p; // Update x if it is more than or
    // equal to p

    while (y > 0)
    {

        // If y is odd, multiply x with result
        if (y & 1)
            res = (res * x) % p;

        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
ll modInverse(ll n, int p)
{
    return power(n, p - 2, p);
}
ll nCrModPFermat(ll n, int r, int p)
{
    // If n<r, then nCr should return 0
    if (n < r)
        return 0;
    // Base case
    if (r == 0)
        return 1;

    // Fill factorial array so that we
    // can find all factorial of r, n
    // and n-r
    ll fac[n + 1];
    fac[0] = 1;
    for (int i = 1; i <= n; i++)
        fac[i] = (fac[i - 1] * i) % p;

    return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;

    while (t--)
    {
        get(n);
        getv(n);

        vector<pair<ll, ll>> ans;
        for(int i=0; i<n; i++){
            for(int j=0; j<n-i-1; j++){
                if(v[j] > v[j+1]){
                    ans.pb({v[j], v[j+1]});
                    swap(v[j], v[j+1]);
                }
                else{
                    ans.pb({v[j], v[j+1]});
                    ans.pb({v[j+1], v[j]});
                }
            }
        }

        cout<<ans.size()<<endl;
        reverse(all(ans));
        for(auto x: ans){
            cout<<x.first<<" "<<x.second<<endl;
        }

    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<long long int> vi;
#define pb push_back
#define mp make_pair
#define all(c) c.begin(), c.end()
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rev(i, a, b) for (int i = a; i >= b; i--)
#define get(n) \
    ll n;      \
    cin >> n;
#define gets(n, m) \
    ll n, m;       \
    cin >> n >> m;
#define getss(n, m, o) \
    ll n, m, o;        \
    cin >> n >> m >> o;
#define show(n) cout << n << endl;
#define getv(n) \
    vi v(n);    \
    loop(i, 0, n) cin >> v[i];
#define sort(v) sort(all(v));
#define getstr(s) \
    string s;     \
    cin >> s;
#define showstr(s) cout << s << endl;
#define showv(v) loop(i, 0, v.size()) cout << v[i] << " ";
#define endl "\n";
#define sp " ";
#define getmatrix(n, m)                     \
    vector<vector<ll>> v(n, vector<ll>(m)); \
    loop(i, 0, n) loop(j, 0, m) cin >> v[i][j];
#define showmatrix(n, m)                      \
    loop(i, 0, n)                             \
    {                                         \
        loop(j, 0, m) cout << v[i][j] << " "; \
        cout << endl;                         \
    }
#define gethashmap(n) \
    map<ll, ll> m;    \
    loop(i, 0, n)     \
    {                 \
        int a;        \
        cin >> a;     \
        m[a]++;       \
    }
#define showhashmap                                \
    for (auto it = m.begin(); it != m.end(); it++) \
        cout << it->first << sp << it->second << endl;
#define getset(n)    \
    set<int> s;      \
    loop(i, 0, n)    \
    {                \
        int a;       \
        cin >> a;    \
        s.insert(a); \
    }
#define sumv(v) accumulate(all(v), 0);

long long int NcR(int n, int r)
{

    // p holds the value of n*(n-1)*(n-2)...,
    // k holds the value of r*(r-1)...
    long long p = 1, k = 1;

    // C(n, r) == C(n, n-r),
    // choosing the smaller value
    if (n - r < r)
        r = n - r;

    if (r != 0)
    {
        while (r)
        {
            p *= n;
            k *= r;

            // gcd of p, k
            long long m = __gcd(p, k);

            // dividing by gcd, to simplify
            // product division by their gcd
            // saves from the overflow
            p /= m;
            k /= m;

            n--;
            r--;
        }

        // k should be simplified to 1
        // as C(n, r) is a natural number
        // (denominator should be 1 ) .
    }

    else
        p = 1;

    // if our approach is correct p = ans and k =1
    return p;
}

void yesno(bool c)
{
    if (c)
    {
        cout << "YES" << endl;
    }
    else
    {
        cout << "NO" << endl;
    }
}
constexpr int N = 2E7;
vector<int> minp, primes;
void sieve(int n)
{
    minp.assign(n + 1, 0);
    primes.clear();

    for (int i = 2; i <= n; i++)
    {
        if (minp[i] == 0)
        {
            minp[i] = i;
            primes.push_back(i);
        }

        for (auto p : primes)
        {
            if (i * p > n)
            {
                break;
            }
            minp[i * p] = p;
            if (p == minp[i])
            {
                break;
            }
        }
    }
}
ll power(ll x, int y, int p)
{
    ll res = 1; // Initialize result

    x = x % p; // Update x if it is more than or
    // equal to p

    while (y > 0)
    {

        // If y is odd, multiply x with result
        if (y & 1)
            res = (res * x) % p;

        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
ll modInverse(ll n, int p)
{
    return power(n, p - 2, p);
}
ll nCrModPFermat(ll n, int r, int p)
{
    // If n<r, then nCr should return 0
    if (n < r)
        return 0;
    // Base case
    if (r == 0)
        return 1;

    // Fill factorial array so that we
    // can find all factorial of r, n
    // and n-r
    ll fac[n + 1];
    fac[0] = 1;
    for (int i = 1; i <= n; i++)
        fac[i] = (fac[i - 1] * i) % p;

    return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;

    while (t--)
    {
        get(n);
        getv(n);

        vector<pair<ll, ll>> ans;
        for(int i=0; i<n; i++){
            for(int j=0; j<n-i-1; j++){
                if(v[j] < v[j+1]){
                    ans.pb({v[j], v[j+1]});
                    swap(v[j], v[j+1]);
                }

            }
        }

        for(int i=0; i<n; i++){
            for(int j=0; j<n-i-1; j++){
                if(v[j] > v[j+1]){
                    ans.pb({v[j], v[j+1]});
                    swap(v[j], v[j+1]);
                }

            }
        }

        cout<<ans.size()<<endl;
        reverse(all(ans));
        for(auto x: ans){
            cout<<x.first<<" "<<x.second<<endl;
        }

    }

    return 0;
}

Answer: 2


#include <bits/stdc++.h>
#define ll long long int
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define vl vector <ll>
#define pl pair <ll,ll>
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define big_prime 1000000000+7
struct P {
    ll x;
    ll y;
    bool operator<(const P& other) const {
        return y < other.y || (y == other.y && x < other.x);
    }

};

bool comp(const P &p1, const P &p2) {
    if (p1.y != p2.y) return p1.y < p2.y;
    else return p1.x < p2.x;
}
#define yon(pass) if(pass){cout << "YES" << "\n";}else{cout << "NO" << "\n";}
ll gcd(ll a, ll b) {
    while (b != 0) {
    ll tmp = a % b;
    a = b;
    b = tmp;
    }
    return a;
}
ll lcm(ll a, ll b) {
    return a * b / gcd(a, b);
}

long long binExpo(long long base, long long exponent, long long mod) {
long long result = 1;
base = base % mod;

while (exponent > 0) {

    if (exponent % 2 == 1) {
        result = (result * base) % mod;
    }

    base = (base * base) % mod;
    exponent /= 2;
}

return result;
}

using namespace std;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //ll t;
    //cin >> t;
    //while (t--) {
        ll n;
        cin >> n;
        vl v(n);
        rep(i, 0, n) cin >> v[i];
        vector<vector<ll>> vv(n,vector<ll>(n));
        vector<P> overtakes;
        rep(i, 1, n) {
            for(ll j= i;j>0;j--){
                if(v[j] < v[j-1]){
                    vv[j][j-1]++,vv[j-1][j]++;
                    P p;
                    p.x = v[j-1], p.y = v[j];
                    overtakes.PB(p);
                    swap(v[j],v[j-1]);
                }
            }
            for(ll j= 1;j<n;j++){
                if(v[j] > v[j-1] && vv[j][j-1] == 0){
                    vv[j][j-1]+=2;
                    vv[j-1][j]+=2;
                    P p;
                    p.x = v[j-1], p.y = v[j];
                    overtakes.PB(p);
                    swap(p.x, p.y);
                    overtakes.PB(p);
                }
            }
        }
        cout << overtakes.size() << "\n";
        reverse(overtakes.begin(), overtakes.end());
        for(auto a : overtakes){
            cout << a.x << " " << a.y << "\n";
        }

    //}
    return 0;
}

#include <bits/stdc++.h>
#define ll long long int
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define vl vector <ll>
#define pl pair <ll,ll>
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define big_prime 1000000000+7
struct P {
    ll x;
    ll y;
    bool operator<(const P& other) const {
        return y < other.y || (y == other.y && x < other.x);
    }

};

bool comp(const P &p1, const P &p2) {
    if (p1.y != p2.y) return p1.y < p2.y;
    else return p1.x < p2.x;
}
#define yon(pass) if(pass){cout << "YES" << "\n";}else{cout << "NO" << "\n";}
ll gcd(ll a, ll b) {
    while (b != 0) {
    ll tmp = a % b;
    a = b;
    b = tmp;
    }
    return a;
}
ll lcm(ll a, ll b) {
    return a * b / gcd(a, b);
}

long long binExpo(long long base, long long exponent, long long mod) {
long long result = 1;
base = base % mod;

while (exponent > 0) {

    if (exponent % 2 == 1) {
        result = (result * base) % mod;
    }

    base = (base * base) % mod;
    exponent /= 2;
}

return result;
}

using namespace std;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //ll t;
    //cin >> t;
    //while (t--) {
        ll n;
        cin >> n;
        vl v(n), v2(n);
        rep(i, 0, n){ cin >> v[i]; v2[i] = (i+1);}
        vector<P> overtakes;
        for(ll i=n-1;i>0;i--){
            ll ind = -1;
            rep(j,0,n){
                if(v2[j] == v[i]){
                    ind = j;
                    break;
                }
            }
            for(ll j = ind;j>0;j--){
                P p;
                p.x = v2[j], p.y = v2[j-1];
                overtakes.PB(p);
                swap(v2[j],v2[j-1]);

            }
            rep(j,0,i){
                P p;
                p.x = v2[j+1], p.y = v2[j];
                overtakes.PB(p);
                swap(v2[j], v2[j+1]);
            }
        }
        cout << overtakes.size() << "\n";
        for(auto a : overtakes){
            cout << a.x << " " << a.y << "\n";
        }

    //}
    return 0;
}

Answer: 2


#include <bits/stdc++.h>
#define int long long

// macros
#define For(x) for (int i = 0; i < x; i++)
#define SORT(a) sort(a.begin(), a.end())
#define FOR(a, b, c) for(int i = a; i < b; i += c)
#define SUM(a) accumulate(a.begin(), a.end(), 0LL)
#define MAX(a) *max_element(a.begin(), a.end())
#define all(x) (x.begin(), x.end())

// syntax
#define mp make_pair

// other macros
#define MOD (int)1e8

// speed
#pragma GCC optimize("O3")

using namespace std;

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int count;
    cin >> count;
    vector<int> arr(count);
    For (count){
        cin >> arr[i];
    }
    int counter = 0;
    for (int i = 0; i < count; i++){
        for (int j = i + 1; j < count; j++){
            if (arr[i] > arr[j]){
                counter++;
            } else {
                counter += 2;
            }
        }
    }
    cout << counter << "\n";
    for (int i = 0; i < count; i++){
        for (int j = i + 1; j < count; j++){
            cout << j + 1 << " " << i + 1 << "\n";
        }
    }
    for (int x = 0; x < count; x++){
        auto revIndex = count - x;
        if (arr[x] != revIndex){
            // cout << revIndex;
            if (arr[x] > revIndex){
                cout << revIndex << " " << arr[x] << "\n";
                auto v = arr[x];
                arr[x] = arr[x-1];
                arr[x-1] = v;
            } else {
                cout << arr[x] << " " << revIndex << "\n";

                auto v = arr[x];
                arr[x] = arr[x+1];
                arr[x+1] = v;
            }
        }
    }
}

#include <bits/stdc++.h>
#define int long long

// macros
#define For(x) for (int i = 0; i < x; i++)
#define SORT(a) sort(a.begin(), a.end())
#define FOR(a, b, c) for(int i = a; i < b; i += c)
#define SUM(a) accumulate(a.begin(), a.end(), 0LL)
#define MAX(a) *max_element(a.begin(), a.end())
#define all(x) (x.begin(), x.end())

// syntax
#define mp make_pair

// other macros
#define MOD (int)1e8

// speed
#pragma GCC optimize("O3")

using namespace std;

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int count;
    cin >> count;
    vector<int> arr(count);
    vector<int> temp(count);
    For (count){
        cin >> arr[i];
    }
    int counter = 0;
    for (int i = 0; i < count; i++){
        for (int j = i + 1; j < count; j++){
            if (arr[i] > arr[j]){
                counter++;
            } else {
                counter += 2;
            }
        }
    }
    cout << counter << "\n";
    for (int i = 0; i < count; i++){
        for (int j = i + 1; j < count; j++){
            cout << j + 1 << " " << i + 1 << "\n";
        }
        temp[i] = count - i;
    }
    while (temp != arr){
        for (int i = 0; i < count - 1; i++){
            int index1 = find(arr.begin(), arr.end(), temp[i]) - arr.begin();
            int index2 = find(arr.begin(), arr.end(), temp[i+1]) - arr.begin();
            if (index1 > index2){
                cout << temp[i + 1] << ' ' << temp[i] << "\n";
                auto v = temp[i];
                temp[i] = temp[i + 1];
                temp[i + 1] = v;
            }
        }
    }
    // 4 3 2 1
    // 3 4 1 2
    // 3 4 1 2
    // 3 1 4 2
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
const int N=230000;
int n,a[N],ans,vis[N];
struct fa{
	int x,y;
}as[N*2];
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		vis[a[i]]=1;
		for(int j=a[i]+1;j<=n;j++)if(!vis[j])as[++ans]={j,a[i]};
		for(int j=n;j>=1;j--)if(!vis[j])as[++ans]={a[i],j};
//		for(int j=a[i]-1;j>=1;j--)if(!vis[j])as[++ans]={a[i],j};
	}
//	sort(as+1,as+ans+1);
	cout<<ans<<endl;
	for(int i=1;i<=ans;i++){
		cout<<as[i].x<<' '<<as[i].y<<endl;
	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int N=230000;
int n,a[N],ans,vis[N];
struct fa{
	int x,y;
}as[N*20];
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		vis[a[i]]=1;
		for(int j=a[i]+1;j<=n;j++)if(!vis[j])as[++ans]={j,a[i]};
		for(int j=n;j>=1;j--)if(!vis[j])as[++ans]={a[i],j};
//		for(int j=a[i]-1;j>=1;j--)if(!vis[j])as[++ans]={a[i],j};
	}
	cout<<ans<<endl;
	for(int i=1;i<=ans;i++){
		cout<<as[i].x<<' '<<as[i].y<<endl;
	}
	return 0;
}

Answer: 2


import sys
from queue import deque
from bisect import bisect_left, bisect_right
from heapq import *
from collections import *

sys.setrecursionlimit(10**5)

input = lambda: sys.stdin.readline().rstrip('\r\n')
iint = lambda: int(input())
mint = lambda: map(int, input().split())
aint = lambda: list(map(int, input().split()))

MOD = 10**9 + 7

def solve():
    n = iint()
    a = aint()
    rank = [0] * (n + 1)
    for i in range(n):
        rank[a[i]] = i+1

    mx_len = n * (n-1) // 2
    curr = list(range(n, 0, -1))
    sq = []
    for i in range(n):
        j = i
        while j>0 and rank[curr[j]] < rank[curr[j-1]]:
            sq.append([curr[j], curr[j-1]])
            curr[j], curr[j-1] = curr[j-1], curr[j]
            mx_len += 1
            j -= 1

    print(mx_len)
    for i in range(1, n+1):
        for j in range(i+1, n+1):
            print(j, i)
    for a, b in sq:
        print(a, b)

solve()

import sys
from queue import deque
from bisect import bisect_left, bisect_right
from heapq import *
from collections import *

sys.setrecursionlimit(10**5)

input = lambda: sys.stdin.readline().rstrip('\r\n')
iint = lambda: int(input())
mint = lambda: map(int, input().split())
aint = lambda: list(map(int, input().split()))

MOD = 10**9 + 7

def solve():
    n = iint()
    a = aint()
    rank = [0] * (n + 1)
    for i in range(n):
        rank[a[i]] = i+1

    mx_len = n * (n-1) // 2
    curr = list(range(n, 0, -1))
    sq = []
    for i in range(n-1, -1, -1):
        j = i
        while j>0 and rank[curr[j]] < rank[curr[j-1]]:
            sq.append([curr[j], curr[j-1]])
            curr[j], curr[j-1] = curr[j-1], curr[j]
            mx_len += 1
            j -= 1

    print(mx_len)
    for i in range(1, n+1):
        for j in range(i+1, n+1):
            print(j, i)
    for a, b in sq:
        print(a, b)

solve()

Answer: 1


/*
Author:
Shivam Kumar
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long int
#define pb push_back
const int mod=1e9+7;
ll binaryExponentiation(ll n,ll m,ll b){ll result=1;ll power=n;if(m==0)return 1;if(m==1)return n;while(m>0){if((m&1)==1){result=(result*power)%b;}power=(power*power)%b; m=m>>1;}return result;}
ll binaryExponentiation(ll n,ll m){ll result=1;ll power=n;if(m==0)return 1;if(m==1)return n;while(m>0){if((m&1)==1){result=(result*power);}power=(power*power); m=m>>1;}return result;}
ll minv(ll a,ll b){return binaryExponentiation(a,b-2,b);}
ll add_mod(ll a,ll b,ll m){return ((a%m)+(b%m))%m;}
ll sub_mod(ll a,ll b,ll m){return ((a%m)-(b%m)+m)%m;}
ll mul_mod(ll a,ll b,ll m){return ((a%m)*(b%m))%m;}
bool isSet(ll n,ll i){ if(n & (1<<i)){return true;}return false;}
ll lcm(ll a,ll b){ return ((a*b)/__gcd(a,b));}
vector<int>get_all_digits(int x){vector<int>ans;while(x>9){ans.push_back(x%10);x/=10;}ans.push_back(x);return ans;}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
class StringHashing{
    public:
        vector<ll>powers;
        vector<ll>stringPrefixes;
        ll p=50;

       void calculatePrefixes(string s,ll m){
            int n=s.length();
            powers.clear();
            stringPrefixes.clear();
            powers.resize(n);
            stringPrefixes.resize(n);
            powers[0]=1;
            for(int i=1;i<n;i++){
                powers[i]=((powers[i-1]%m)*(p%m))%m;
           }
            stringPrefixes[0]=(s[0]-'a'+1)%m;

            for(int i=1;i<n;i++){
                ll t=(stringPrefixes[i-1]+(((s[i]-'a'+1)%m)*(powers[i]%m))%m)%m;
                stringPrefixes[i]=t;
                }
        }
  ll substringhash(int l,int r,ll m){
        if(l==0){
            return stringPrefixes[r];
        }

        ll a=(stringPrefixes[r]%m-stringPrefixes[l-1]%m)%m;
        a+=m;
        a%=m;

        ll b=powers[l];
        ll modinverse_b=binaryExponentiation(b,m-2,m);

        return (a*modinverse_b)%m;
}
};
class dsu{
    vector<int>parent,size,rank;
    public:
    dsu(int n){
        for(int i=0;i<n;i++){
        parent.push_back(i);
        size.push_back(1);
        rank.push_back(0);
        }
    }
    int findparent(int node){
        if(parent[node]==node){
            return node;
        }
        return parent[node]=findparent(parent[node]);
    }

    void unionSize(int u,int v){
        int pu=findparent(u);
        int pv=findparent(v);
        if(pu==pv){
            return;
        }
        if(size[pu]<size[pv]){
            parent[pu]=pv;
            size[pv]+=size[pu];
        }else{
            parent[pv]=pu;
            size[pv]+=size[pu];
        }
    }

    void unionRank(int u,int v){
        int pv=parent[v];
        int pu=parent[u];
        if(pu==pv){
            return;
        }
        if(rank[pu]==rank[pv]){
            parent[pu]=pv;
            rank[pv]++;
        }else if(rank[pv]<rank[pu]){
            parent[pv]=pu;
        }else{
            parent[pu]=pv;
        }
    }
    };
/*Z-algorithm*/

vector<int> z_function(string s){
    int n=(int)s.length();
    vector<int>z(n);
    z[0]=0;
    for(int i=1,l=0,r=0;i<n;i++){
        if(i<=r){
            z[i]=min(r-l+1,z[i-l]);
        }
        while(i+z[i]<n && s[z[i]]==s[i+z[i]]){
            z[i]++;
        }
        if(i+z[i]-1>r){
            l=i;
            r=i+z[i]-1;
        }

    }
    return z;
}
void solve(){
    int n;
    cin>>n;
    vector<int>c(n+1);

    vector<pair<int,int>>ans;
    vector<int>mark(n+1);
    for(int i=1;i<=n;i++){
        cin>>c[i];
        mark[c[i]]=1;
        for(int j=c[i]+1;j<=n;j++){
            if(!mark[j]){
                ans.push_back({j,c[i]});
            }

        }
        for(int j=1nj>=1;j++){
            if(!mark[j]){
                ans.push_back({c[i],j});

            }
        }
    }
    cout<<ans.size()<<'\n';
    for(int i=0;i<ans.size();i++){
        cout<<ans[i].first<<' '<<ans[i].second<<"\n";
    }

}

int main(){
    ios::sync_with_stdio(0), cin.tie(0);
    int t=1;
   // cin>>t;
    while(t--){
        solve();
    }

}

/*
Author:
Shivam Kumar
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long int
#define pb push_back
const int mod=1e9+7;
ll binaryExponentiation(ll n,ll m,ll b){ll result=1;ll power=n;if(m==0)return 1;if(m==1)return n;while(m>0){if((m&1)==1){result=(result*power)%b;}power=(power*power)%b; m=m>>1;}return result;}
ll binaryExponentiation(ll n,ll m){ll result=1;ll power=n;if(m==0)return 1;if(m==1)return n;while(m>0){if((m&1)==1){result=(result*power);}power=(power*power); m=m>>1;}return result;}
ll minv(ll a,ll b){return binaryExponentiation(a,b-2,b);}
ll add_mod(ll a,ll b,ll m){return ((a%m)+(b%m))%m;}
ll sub_mod(ll a,ll b,ll m){return ((a%m)-(b%m)+m)%m;}
ll mul_mod(ll a,ll b,ll m){return ((a%m)*(b%m))%m;}
bool isSet(ll n,ll i){ if(n & (1<<i)){return true;}return false;}
ll lcm(ll a,ll b){ return ((a*b)/__gcd(a,b));}
vector<int>get_all_digits(int x){vector<int>ans;while(x>9){ans.push_back(x%10);x/=10;}ans.push_back(x);return ans;}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
class StringHashing{
    public:
        vector<ll>powers;
        vector<ll>stringPrefixes;
        ll p=50;

       void calculatePrefixes(string s,ll m){
            int n=s.length();
            powers.clear();
            stringPrefixes.clear();
            powers.resize(n);
            stringPrefixes.resize(n);
            powers[0]=1;
            for(int i=1;i<n;i++){
                powers[i]=((powers[i-1]%m)*(p%m))%m;
           }
            stringPrefixes[0]=(s[0]-'a'+1)%m;

            for(int i=1;i<n;i++){
                ll t=(stringPrefixes[i-1]+(((s[i]-'a'+1)%m)*(powers[i]%m))%m)%m;
                stringPrefixes[i]=t;
                }
        }
  ll substringhash(int l,int r,ll m){
        if(l==0){
            return stringPrefixes[r];
        }

        ll a=(stringPrefixes[r]%m-stringPrefixes[l-1]%m)%m;
        a+=m;
        a%=m;

        ll b=powers[l];
        ll modinverse_b=binaryExponentiation(b,m-2,m);

        return (a*modinverse_b)%m;
}
};
class dsu{
    vector<int>parent,size,rank;
    public:
    dsu(int n){
        for(int i=0;i<n;i++){
        parent.push_back(i);
        size.push_back(1);
        rank.push_back(0);
        }
    }
    int findparent(int node){
        if(parent[node]==node){
            return node;
        }
        return parent[node]=findparent(parent[node]);
    }

    void unionSize(int u,int v){
        int pu=findparent(u);
        int pv=findparent(v);
        if(pu==pv){
            return;
        }
        if(size[pu]<size[pv]){
            parent[pu]=pv;
            size[pv]+=size[pu];
        }else{
            parent[pv]=pu;
            size[pv]+=size[pu];
        }
    }

    void unionRank(int u,int v){
        int pv=parent[v];
        int pu=parent[u];
        if(pu==pv){
            return;
        }
        if(rank[pu]==rank[pv]){
            parent[pu]=pv;
            rank[pv]++;
        }else if(rank[pv]<rank[pu]){
            parent[pv]=pu;
        }else{
            parent[pu]=pv;
        }
    }
    };
/*Z-algorithm*/

vector<int> z_function(string s){
    int n=(int)s.length();
    vector<int>z(n);
    z[0]=0;
    for(int i=1,l=0,r=0;i<n;i++){
        if(i<=r){
            z[i]=min(r-l+1,z[i-l]);
        }
        while(i+z[i]<n && s[z[i]]==s[i+z[i]]){
            z[i]++;
        }
        if(i+z[i]-1>r){
            l=i;
            r=i+z[i]-1;
        }

    }
    return z;
}
void solve(){
    int n;
    cin>>n;
    vector<int>c(n+1);

    vector<pair<int,int>>ans;
    vector<int>mark(n+1);
    for(int i=1;i<=n;i++){
        cin>>c[i];
        mark[c[i]]=1;
        for(int j=c[i]+1;j<=n;j++){
            if(!mark[j]){
                ans.push_back({j,c[i]});
            }

        }
        for(int j=n;j>=1;j--){
            if(!mark[j]){
                ans.push_back({c[i],j});

            }
        }
    }
    cout<<ans.size()<<'\n';
    for(int i=0;i<ans.size();i++){
        cout<<ans[i].first<<' '<<ans[i].second<<"\n";
    }

}

int main(){
    ios::sync_with_stdio(0), cin.tie(0);
    int t=1;
   // cin>>t;
    while(t--){
        solve();
    }

}

Answer: 2


#include<bits/stdc++.h>
#include<stdio.h>
#include<chrono>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds ;
using ll = long long ;
typedef tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> pbds ;
ll inf = (4e18L) + 100 ;
ll ninf = (-4e18L) - 100 ;
const ll mod = 1e9 + 7 ; // 998244353;
const int MAXN = 2e5 + 100 ;

void solve_test_case()
{
    int n ;
    cin >> n ;

    vector<int> arr(n+1) ;

    for(int i = 1 ; i <= n ; i++) cin >> arr[i] ;
    vector<pair<int,int>> ans ;
    vector<bool> visited(n,false) ;
    for(int j = n  ; j > 0 ; j--)
    {
        int x = arr[j] ;
        visited[x] = true ;

        for(int i = x-1 ; i > 0 ; i--)
        {
            if(visited[i] == true) continue ;
            ans.push_back({x,i}) ;
        }
        for(int i = 1 ; i <= x-1 ; i++)
        {
            if(visited[i] == true) continue ;
            ans.push_back({i,x}) ;
        }

        for(int i = x+1 ; i <= n ; i++)
        {
            if(visited[i] == true) continue ;
            ans.push_back({i,x}) ;
        }
    }

    cout << (int)ans.size() << endl;

    for(auto v : ans)
    {
        cout << v.first << " " << v.second << endl ;
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(false) ;
    cin.tie(NULL) ;
    cout.tie(NULL) ;
    // init_code() ;
    ll t = 1 ;
    // cin >> t;
    while(t--) solve_test_case() ;
    return 0 ;
}

#include<bits/stdc++.h>
#include<stdio.h>
#include<chrono>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds ;
using ll = long long ;
typedef tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> pbds ;
ll inf = (4e18L) + 100 ;
ll ninf = (-4e18L) - 100 ;
const ll mod = 1e9 + 7 ; // 998244353;
const int MAXN = 2e5 + 100 ;

void solve_test_case()
{
    int n ;
    cin >> n ;

    vector<int> arr(n+1) ;

    for(int i = 1 ; i <= n ; i++) cin >> arr[i] ;
    vector<pair<int,int>> ans ;
    vector<bool> visited(n+1,false) ;
    for(int j = n  ; j > 0 ; j--)
    {
        int x = arr[j] ;
        visited[x] = true ;

        for(int i = x-1 ; i > 0 ; i--)
        {
            if(visited[i] == true) continue ;
            ans.push_back({x,i}) ;
        }
        for(int i = 1 ; i <= x-1 ; i++)
        {
            if(visited[i] == true) continue ;
            ans.push_back({i,x}) ;
        }

        for(int i = x+1 ; i <= n ; i++)
        {
            if(visited[i] == true) continue ;
            ans.push_back({i,x}) ;
        }

    }

    cout << (int)ans.size() << endl;

    for(auto v : ans)
    {
        cout << v.first << " " << v.second << endl ;
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(false) ;
    cin.tie(NULL) ;
    cout.tie(NULL) ;
    // init_code() ;
    ll t = 1 ;
    // cin >> t;
    while(t--) solve_test_case() ;
    return 0 ;
}

Answer: 2


    #define GLIBCXX_DEBUG

    #include <bits/stdc++.h>
    // #include "optimization.h"
    using namespace std;
    std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
    #define all(a) a.begin(), a.end()
    #ifdef LOCAL
    #define dbg(...) [](auto...zxc){ ((cout << zxc << ' '), ...) << endl;}(#__VA_ARGS__, ":", __VA_ARGS__)
    #define dbgv(v) cout<< #v <<" : [ "; for(auto &zxc: v) {cout << zxc << " "; }cout <<"]"<< endl;
    #define dbgm(v) cout<< #v <<" :"; for(auto &zxc: v) {cout << '\n' << zxc.fs << ": " << zxc.sc; }cout << endl;
    #else
    #define dbg(...)
    #define dbgv(v)
    #define dbgm(v)
    #endif

    using ll = long long;
    using ld = long double;
    using ull = unsigned long long;

    template <class T>
    T gcd(T a, T b) {
        while (b) a = a % b, swap(a, b);
        return a;
    }

    const ll mod = 998244353;

    int64_t poww(int64_t val, int64_t d, int64_t modd = mod) {
        int64_t ans = 1, mn = val;
        while (d) {
            if (d & 1) ans = mn * ans % modd;
            mn = mn * mn % modd, d >>= 1;
        }
        return ans;
    }

    void solve() {
        int n; cin >> n;
        vector<int> a(n);
        for(int i=0;i<n;i++){
            cin >> a[i];
        }
        vector<int>perm(n);
        vector<pair<int,int>> ans;
        set<pair<int,int>> s, cands;
        iota(all(perm),1);
        int ind = 0;
        while(ind!=n){
            int cur_ind = find(all(perm), a[ind]) - perm.begin();
            dbg(ind);
            for(int i=cur_ind+1;i<n;i++){
                dbg(i-1,i);
                ans.push_back({perm[i],perm[i-1]});
                swap(perm[i], perm[i-1]);
            }
            dbg(ind);
            for(int i=n-1;i>=ind+1;i--){
                dbg(i-1,i);
                ans.push_back({perm[i],perm[i-1]});
                swap(perm[i], perm[i-1]);
            }
            ind++;
        }
        cout << ans.size() << '\n';
        for(auto el: ans){
            cout << el.first << ' ' << el.second << '\n';
        }
    }

    signed main() {
        ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
        int t = 1;
        //cin >> t;
        while (t--) solve();
    }

    #define GLIBCXX_DEBUG

    #include <bits/stdc++.h>
    // #include "optimization.h"
    using namespace std;
    std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
    #define all(a) a.begin(), a.end()
    #ifdef LOCAL
    #define dbg(...) [](auto...zxc){ ((cout << zxc << ' '), ...) << endl;}(#__VA_ARGS__, ":", __VA_ARGS__)
    #define dbgv(v) cout<< #v <<" : [ "; for(auto &zxc: v) {cout << zxc << " "; }cout <<"]"<< endl;
    #define dbgm(v) cout<< #v <<" :"; for(auto &zxc: v) {cout << '\n' << zxc.fs << ": " << zxc.sc; }cout << endl;
    #else
    #define dbg(...)
    #define dbgv(v)
    #define dbgm(v)
    #endif

    using ll = long long;
    using ld = long double;
    using ull = unsigned long long;

    template <class T>
    T gcd(T a, T b) {
        while (b) a = a % b, swap(a, b);
        return a;
    }

    const ll mod = 998244353;

    int64_t poww(int64_t val, int64_t d, int64_t modd = mod) {
        int64_t ans = 1, mn = val;
        while (d) {
            if (d & 1) ans = mn * ans % modd;
            mn = mn * mn % modd, d >>= 1;
        }
        return ans;
    }

    void solve() {
        int n; cin >> n;
        vector<int> a(n);
        for(int i=0;i<n;i++){
            cin >> a[i];
        }
        vector<int>perm(n);
        vector<pair<int,int>> ans;
        set<pair<int,int>> s, cands;
        iota(all(perm),1);
        int ind = 0;
        while(ind!=n){
            if(perm[ind]==a[ind]){
                ind++;
            }else{
                int cur_ind = find(all(perm), a[ind]) - perm.begin();
                for(int i=cur_ind;i>=ind+1;i--){
                    s.insert({perm[i], perm[i-1]});
                    swap(perm[i], perm[i-1]);
                }
            }
        }
        iota(all(perm),1);
        ind = 0;
        while(ind!=n){
            if(perm[ind]==a[ind]){
                for(int i=1;i<n;i++){
                    if(!(s.count({perm[i-1], perm[i]}) || s.count({perm[i], perm[i-1]}))){
                        if(cands.insert({perm[i],perm[i-1]}).second)ans.push_back({perm[i],perm[i-1]});
                        if(cands.insert({perm[i-1],perm[i]}).second)ans.push_back({perm[i-1],perm[i]});
                    }
                }
                ind++;
            }else{
                int cur_ind = find(all(perm), a[ind]) - perm.begin();
                for(int i=1;i<n;i++){
                    if(!(s.count({perm[i-1], perm[i]}) || s.count({perm[i], perm[i-1]}))){
                        if(cands.insert({perm[i],perm[i-1]}).second)ans.push_back({perm[i],perm[i-1]});
                        if(cands.insert({perm[i-1],perm[i]}).second)ans.push_back({perm[i-1],perm[i]});
                    }
                }
                for(int i=cur_ind;i>=ind+1;i--){
                    if(cands.insert({perm[i],perm[i-1]}).second)ans.push_back({perm[i], perm[i-1]});
                    swap(perm[i], perm[i-1]);
                    if(i< n-1){
                           if(!(s.count({perm[i+1], perm[i]}) || s.count({perm[i], perm[i+1]}))){
                            if(cands.insert({perm[i+1],perm[i]}).second)ans.push_back({perm[i+1],perm[i]});
                            if(cands.insert({perm[i],perm[i+1]}).second)ans.push_back({perm[i],perm[i+1]});
                        }
                    }
                    if(i>=2){
                           if(!(s.count({perm[i-1], perm[i-2]}) || s.count({perm[i-2], perm[i-1]}))){
                            if(cands.insert({perm[i-1],perm[i-2]}).second)ans.push_back({perm[i-1],perm[i-2]});
                            if(cands.insert({perm[i-2],perm[i-1]}).second)ans.push_back({perm[i-2],perm[i-1]});
                        }

                    }

                }
            }
        }
        cout << ans.size() << '\n';
        for(auto el: ans){
            cout << el.first << ' ' << el.second << '\n';
        }
    }

    signed main() {
        ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
        int t = 1;
        //cin >> t;
        while (t--) solve();
    }

Answer: 1


#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define ld long double
#define vii vector<int>
#define vll vector<ll>
#define vss vector<string>
#define vcc vector<char>
#define vld vector<ld>
#define vbb vector<bool>
#define re register
#define il inline
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define pcc pair<char,char>
#define mii map<ll,ll>
#define mci map<char,ll>
#define mll map<ll,ll>
#define sii set<ll>
#define sll set<ll>
#define msi map<string,ll>
#define msl map<char,ll>
#define usii unordered_set<ll>
#define usll unordered_set<ll>
#define umii unordered_map<ll,ll>
#define umll unordered_map<ll,ll>
#define umci unordered_map<char,ll>
#define pb push_back
#define pf push_front
#define fi first
#define so second
const ll MOD = 1e9 + 7;
const ll inf = 0x3f3f3f3f;
const ll MAXX = LLONG_MAX;
const ll INF = 0x3f3f3f3f3f3f3f3f;
using namespace std;
const ll MAXN = 362880 + 5; // 876543210的hash值为362880，即最多出现362880种可能
const int dirma[8][2] = { {-2,1},{-2,-1},{-1,2},{-1,-2},{1,2},{1,-2},{2,1},{2,-1} };
const int dir[4][2] = { {-1,0},{1,0},{0,1},{0,-1} };
const int mod = 1e6 + 7;
struct ooo
{

};

void solve()
{
    int n;
    cin >> n;
    vii res(n + 1);
    vii arr(n + 1);
    for (int i = 1; i <= n; i++)
    {
        arr[i] = i;
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> res[i];
    }
    int cnt = 0;
    for (int i = 1; i < n; i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            if (res[i] > res[j])
            {
                cnt++;
            }
            else
            {
                cnt += 2;
            }
        }
    }
    cout << cnt << endl;
    for (int i = 1; i <= n; i++)
    {
        int x = res[i];
        int idx = -1;
        for (int j = 1; j <= n; j++)
        {
            if (arr[j] == x)
            {
                idx = j;
                break;
            }
        }
        for (int j = idx + 1; j <= n; j++)
        {
            cout << arr[j] << " " <<  x << endl;
            swap(arr[j], arr[j-1]);
        }
        for (int j = n - 1; j >= i; j--)
        {
            cout << x << " " << arr[j] << endl;
            swap(arr[j], arr[j+1]);
        }
    }
}

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

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define ld long double
#define vii vector<ll>
#define vll vector<ll>
#define vss vector<string>
#define vcc vector<char>
#define vld vector<ld>
#define vbb vector<bool>
#define re register
#define il inline
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define pcc pair<char,char>
#define mii map<ll,ll>
#define mci map<char,ll>
#define mll map<ll,ll>
#define sii set<ll>
#define sll set<ll>
#define msi map<string,ll>
#define msl map<char,ll>
#define usii unordered_set<ll>
#define usll unordered_set<ll>
#define umii unordered_map<ll,ll>
#define umll unordered_map<ll,ll>
#define umci unordered_map<char,ll>
#define pb push_back
#define pf push_front
#define fi first
#define so second
const ll MOD = 1e9 + 7;
const ll inf = 0x3f3f3f3f;
const ll MAXX = LLONG_MAX;
const ll INF = 0x3f3f3f3f3f3f3f3f;
using namespace std;
const ll MAXN = 362880 + 5; // 876543210的hash值为362880，即最多出现362880种可能
const int dirma[8][2] = { {-2,1},{-2,-1},{-1,2},{-1,-2},{1,2},{1,-2},{2,1},{2,-1} };
const int dir[4][2] = { {-1,0},{1,0},{0,1},{0,-1} };
const int mod = 1e6 + 7;
struct ooo
{

};

void solve()
{
    int n;
    cin >> n;
    vii res(n + 1);
    vii arr(n + 1);
    for (int i = 1; i <= n; i++)
    {
        arr[i] = i;
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> res[i];
    }
    int cnt = 0;
    for (int i = 1; i < n; i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            if (res[i] > res[j])
            {
                cnt++;
            }
            else
            {
                cnt += 2;
            }
        }
    }
    cout << cnt << endl;
    for (int i = 1; i <= n; i++)
    {
        int x = res[i];
        int idx = -1;
        for (int j = 1; j <= n; j++)
        {
            if (arr[j] == x)
            {
                idx = j;
                break;
            }
        }
        for (int j = idx + 1; j <= n; j++)
        {
            cout << arr[j] << " " <<  x << endl;
        }
        for (int j = n - 1; j >= i; j--)
        {
            cout << x << " " << arr[j] << endl;
        }
        for (int j = idx - 1; j >= i; j--)
        {
            swap(arr[j], arr[j + 1]);
        }
    }
}

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

Answer: 1


#include <bits/stdc++.h>
#define int long long
#define mod 1000000007
using namespace std;

void solve()
{
    int n;
    cin >> n;
    vector<int> val(n);
    for (int i = 0; i < n; i++)
    {
        cin >> val[i];
    }

    vector<pair<int, int>> ans;
    for (int i = 0; i < n; i++)
    {
        vector<pair<int, int>> v;
        vector<pair<int, int>> s;
        for (int j = i + 1; j < n; j++)
        {
            if (val[i] < val[j])
            {
                ans.push_back({val[j], val[i]});
                v.push_back({val[i], val[j]});
            }
            else
            {
                s.push_back({val[i], val[j]});
            }
        }
        reverse(v.begin(), v.end());
        for (int j = 0; j < v.size(); j++)
        {
            ans.push_back(v[j]);
        }

        for (int j = 0; j < s.size(); j++)
        {
            ans.push_back(s[j]);
        }
    }

    cout << ans.size() << endl;
    for (auto i : ans)
    {
        cout << i.first << " " << i.second << endl;
    }
    cout << endl;
}

signed main()
{
    solve();
    return 0;
}

#include <bits/stdc++.h>
#define int long long
#define mod 1000000007
using namespace std;

void solve()
{
    int n;
    cin >> n;
    vector<int> val(n);
    for (int i = 0; i < n; i++)
    {
        cin >> val[i];
    }

    vector<pair<int, int>> ans;
    for (int i = 0; i < n; i++)
    {
        vector<pair<int, int>> v;
        vector<pair<int, int>> s;
        for (int j = i + 1; j < n; j++)
        {
            if (val[i] < val[j])
            {
                // ans.push_back({val[j], val[i]});
                v.push_back({val[i], val[j]});
            }
            else
            {
                s.push_back({val[i], val[j]});
            }
        }

        sort(v.begin(), v.end());
        for (int j = 0; j < v.size(); j++)
        {
            ans.push_back({v[j].second, v[j].first});
        }

        reverse(v.begin(), v.end());
        for (int j = 0; j < v.size(); j++)
        {
            ans.push_back(v[j]);
        }

        sort(s.begin(), s.end());
        reverse(s.begin(), s.end());
        for (int j = 0; j < s.size(); j++)
        {
            ans.push_back(s[j]);
        }
    }

    cout << ans.size() << endl;
    for (auto i : ans)
    {
        cout << i.first << " " << i.second << endl;
    }
    cout << endl;
}

signed main()
{
    solve();
    return 0;
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;

#define mod 1000000007
void solve() {
    int n;
    cin >> n;
    vector<int> vec(n + 1);
    vector<int> mp(n + 1);

    for (int i = 1; i <= n; i++)
        vec[i] = i;

    for (int i = 1; i <= n; i++) {
        int x; cin >> x;
        mp[x] = i;
    }
    map<int,int> curr;
    for(int i=1;i<=n;i++)
    curr[i]=i;
    vector<pair<int,int>> ans;
    for(int k=1;k<=n;k++)
    {
        int i=curr[k];
        int val = vec[i];
        for (int j = i - 1; j >= 1; j--) {
            ans.push_back({val, vec[j]});
            swap(vec[j],vec[j+1]);
        }
        for (int j = 2; j <= n; j++) {
            if(mp[vec[j]]>mp[val]) break;
            ans.push_back({vec[j], val});
            swap(vec[j-1],vec[j]);
        }
        for(int l=1;l<=n;l++)
        {
          curr[vec[l]]=l;
        }
    }

    cout << (int)ans.size() << "\n";
    for (auto &p : ans) {
        cout << p.first << " " << p.second << "\n";
    }
}

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

    int test = 1;
    // cin >> test; // If multiple test cases are needed, uncomment this.
    while (test--) {
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

struct PairHash {
    template <typename T1, typename T2>
    size_t operator()(const pair<T1, T2>& p) const {
        return hash<T1>()(p.first) ^ (hash<T2>()(p.second) << 1);
    }
};

void solve() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n; cin >> n;
    vector<int> a(n + 1), mp(n + 1);
    for (int i = 1; i <= n; i++)
        a[i] = i;

    for (int i = 1; i <= n; i++) {
        int x; cin >> x;
        mp[x] = i;
    }

    unordered_set<pair<int, int>, PairHash> st;
    for (int i = 1; i <= n; i++) {
        int shift = mp[a[i]] - i;
        if (shift <= 0) continue;
        int val2 = a[i];
        int pos = i;
        while (shift--) {
            a[pos] = a[pos + 1];
            if (st.find({a[pos + 1], val2}) != st.end()) {
                st.erase({a[pos + 1], val2});
            } else {
                st.insert({val2, a[pos + 1]});
            }
            pos++;
        }
        a[mp[val2]] = val2;
        i--;
    }

    vector<pair<int, int>> ans;
    vector<int> vec(n + 1);
    for (int i = 1; i <= n; i++)
        vec[i] = i;

    for (int i = 2; i <= n; i++) {
        int val = vec[i];
        for (int j = i - 1; j >= 1; j--) {
            ans.push_back({val, vec[j]});
            vec[j + 1] = vec[j];
        }
        vec[1] = val;

        for (int j = 2; j <= i; j++) {
            if (st.find({vec[j], val}) != st.end()) {
                vec[j - 1] = val;
                break;
            }
            ans.push_back({vec[j], val});
            vec[j - 1] = vec[j];
            if (j == i) vec[j] = val;
        }
    }

    cout << (int)ans.size() << "\n";
    for (auto &p : ans) {
        cout << p.first << " " << p.second << "\n";
    }
}

int main() {
    solve();
    return 0;
}

Answer: 1


#include<bits/stdc++.h>

using namespace std;
using pii=pair<int,int>;

signed main(){
    cin.tie(nullptr)->sync_with_stdio(0);
    cout.tie(nullptr)->sync_with_stdio(0);
    int n;
    cin>>n;
    vector<int> lst(n+1);
    vector<int> now(n+1);
    for(int i=1; i<=n ; i++){
        cin>>lst[i];
        now[i]=i;
    }
    vector<pii> ans;
    vector<vector<bool>> vis(n+1,vector<bool>(n+1,true));
    for(int i=1; i<=n ; i++){
        if(now[i]!=lst[i]){
            int p=0;
            int tar=lst[i];
            for(int j=i+1; j<=n ; j++){
                if(now[j]==tar){
                    p=j; break;
                }
            }
            for(int j=p ; j>i ; j--){
                ans.push_back({now[j],now[j-1]});
                swap(now[j],now[j-1]);
                vis[now[j]][now[j-1]]=vis[now[j-1]][now[j]]=false;
            }
        }
    }
    //cout<<"!!"<<' '<<ans.size()<<'\n';
    //for(auto it: now) cout<<it<<' ';
    //cout<<'\n';
    for(int i=1; i<n ; i++){
        int j=i+1;
        for(int v ; j<=n; j++){
            v=now[j];
            if(vis[now[j-1]][v]){
                //cout<<"??"<<u<<' '<<v<<'\n';
                ans.push_back({v,now[j-1]});
                swap(now[j],now[j-1]);
                vis[now[j-1]][v]=vis[v][now[j-1]]=false;
            }
            else{
                break;
            }
        }
        j--;
        for(int v; j>i ; j--){
            //cout<<"NMSL "<<i<<' '<<j<<'\n';
            v=now[j];
            ans.push_back({now[j],now[j-1]});
            swap(now[j],now[j-1]);
        }
    }
    cout<<ans.size()<<'\n';
    for(auto x :ans ){
        cout<<x.first<<' '<<x.second<<'\n';
    }
}

#include<bits/stdc++.h>

using namespace std;
using pii=pair<int,int>;

signed main(){
    cin.tie(nullptr)->sync_with_stdio(0);
    cout.tie(nullptr)->sync_with_stdio(0);
    int n;
    cin>>n;
    vector<int> lst(n+1);
    vector<int> now(n+1);
    for(int i=1; i<=n ; i++){
        cin>>lst[i];
        now[i]=i;
    }
    // for(auto it:now) cout<<it<<' ';
    // cout<<'\n';
    vector<pii> ans;
    vector<vector<bool>> vis(n+1,vector<bool>(n+1,true));
    for(int i=1 ; i<n ; i++){
        for(int j=n ; j>i ;  j--){
            ans.push_back({now[j],now[j-1]});
            swap(now[j],now[j-1]);
        }
    }
    for(int i=1; i<=n; i++){
        if(now[i]!=lst[i]){
            int p,tar=lst[i];
            for(int j=i+1 ; j<=n ; j++){
                if(now[j]==tar){
                    p=j;
                    break;
                }
            }
            for(int j=p ; j>i ; j--){
                    ans.push_back({now[j],now[j-1]});
                swap(now[j],now[j-1]);
            }
        }
    }
    // for(auto it:now) cout<<it<<' ';
    // cout<<'\n';
    cout<<ans.size()<<'\n';
    for(auto x :ans ){
        cout<<x.first<<' '<<x.second<<'\n';
    }
}

Answer: 2


import sys

def max_num(order, n):
    if n == 0:
        return 0, ""
    counted = set()
    m = 0
    string = ""
    i = order[0]
    before = ""
    after = ""
    for j in sorted(order, reverse=True):
        if i < j:
            m += 2
            string = f"\n{j} {i}"+string
            before += f"\n{i} {j}"
        elif i > j:
            m += 1
            after += f"\n{i} {j}"
    string += before + after
    m2, s2 = max_num(order[1:], n-1)
    m += m2
    string += s2
    return m, string
    # for i in range(n):
    #     for j in reversed(range(n)):
    #         if j+1 not in counted:
    #             if j+1 > order[i]:
    #                 m += 2
    #                 string += f"{j+1} {order[i]}\n{order[i]} {j+1}\n"
    #             elif j+1 < order[i]:
    #                 m += 1
    #                 string += f"{order[i]} {j+1}\n"
    #     counted.add(order[i])

i = 0
for line in sys.stdin.readlines():
    if i==0:
        n = int(line)
    else:
        order = [int(x) for x in line.split(" ")]
        m, string = max_num(order, n)
        print(f"{m}{string}")
    i+=1

import sys

def max_num(order, n):
    counted = set()
    m = 0
    string = ""
    for i in order:
        before = ""
        after = ""
        m += n - len(counted)-1
        for j in reversed(range(1,n+1)):
            if j not in counted and j != i:
                if i < j:
                    m += 1
                    before = f"\n{j} {i}"+before
                before += f"\n{i} {j}"
        string += before + after
        counted.add(i)
    print(f"{m}{string}")
    return m, string

i = 0
for line in sys.stdin.readlines():
    if i==0:
        n = int(line)
    else:
        order = [int(x) for x in line.split(" ")]
        max_num(order, n)
    i+=1

Answer: 2


#include <iostream>
#include <vector>
using namespace std;

int n;
int c[1100];
int main()
{

    vector<pair<int, int>> ans;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> c[i];
    for (int i = 1; i < n; i++)
        if (c[i] < c[i + 1])
        {
            ans.push_back({c[i], c[i + 1]});
            ans.push_back({c[i + 1], c[i]});
        }
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n - i; j++)
        {
            if (c[j] > c[j + 1])
            {
                ans.push_back({c[j], c[j + 1]});
                swap(c[j + 1], c[j]);
            }
        }

    cout << ans.size() << endl;
    for (int i = (int)ans.size() - 1; i >= 0; i--)
        cout << ans[i].first << " " << ans[i].second << endl;

    system("pause");
    return 0;
}

#include <iostream>
#include <vector>
using namespace std;

int n;
int c[1100];
int v[1100][1100] = {0};
int main()
{

    vector<pair<int, int>> ans;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> c[i];
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n - i; j++)
        {
            if (c[j] < c[j + 1])
            {
                ans.push_back({c[j], c[j + 1]});
                swap(c[j + 1], c[j]);
            }
        }
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n - i; j++)
        {
            if (c[j] > c[j + 1])
            {
                ans.push_back({c[j], c[j + 1]});
                swap(c[j + 1], c[j]);
            }
        }
    cout << ans.size() << endl;
    for (int i = (int)ans.size() - 1; i >= 0; i--)
        cout << ans[i].first << " " << ans[i].second << endl;

    system("pause");
    return 0;
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> c(n);
    for (int i = 0; i < n; ++i) cin >> c[i];

    vector<int> cars(n);
    iota(cars.begin(), cars.end(), 1); // Initialize cars as [1, 2, ..., n]

    vector<pair<int, int>> overtakes;

    // Perform Bubble Sort to count overtakes
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n - 1; ++j) {
            if (cars[j] > cars[j + 1]) {
                swap(cars[j], cars[j + 1]);
                overtakes.push_back({cars[j + 1], cars[j]});
            }
        }
    }

    // Output the number of overtakes and the list of overtakes
    cout << overtakes.size() << endl;
    for (const auto& overtake : overtakes) {
        cout << overtake.first << " " << overtake.second << endl;
    }

    return 0;
}

#include <iostream>
using namespace std;
const int N=230000;
int n,a[N],ans,vis[N];
struct fa{
	int x,y;
}as[N*20];
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		vis[a[i]]=1;
		for(int j=a[i]+1;j<=n;j++) if(!vis[j]) as[++ans]={j,a[i]};
		for(int j=n;j>=1;j--) if(!vis[j]) as[++ans]={a[i],j};
	}
	cout<<ans<<endl;
	for(int i=1;i<=ans;i++){
		cout<<as[i].x<<' '<<as[i].y<<endl;
	}
	return 0;
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
int n,a[N],b[N];
vector<pair<int,int> > v;
void solve(){
	v.clear();
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<=n;i++){
		for(int j=1;j<n;j++){
			if(a[j]<a[j+1]){
				v.emplace_back(make_pair(a[j],a[j+1]));
				swap(a[j],a[j+1]);
			}
		}
	}
	for(int i=n;i>=1;i--){
		for(int j=i-1;j>=1;j--) v.emplace_back(make_pair(i,j));
	}
	reverse(v.begin(),v.end());
	cout<<v.size()<<"\n";
	for(auto x:v){
		cout<<x.first<<' '<<x.second<<'\n';
	}
}
int main(){
	int t=1;
//	cin>>t;
	while(t--) solve();
	return 0;
}
/*

*/

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
int n,a[N],b[N];
vector<pair<int,int> > v;
void solve(){
	v.clear();
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		b[i]=a[i];
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<n;j++){
			if(a[j]>a[j+1]){
				v.emplace_back(make_pair(a[j],a[j+1]));
				swap(a[j],a[j+1]);
			}
		}
	}
	reverse(v.begin(),v.end());
	for(int i=1;i<n;i++){
		if(b[i]<b[i+1]){
			v.emplace_back(b[i+1],b[i]);
			v.emplace_back(b[i],b[i+1]);
		}
	}
	cout<<v.size()<<"\n";
	for(auto x:v){
		cout<<x.first<<' '<<x.second<<'\n';
	}
}
int main(){
	int t=1;
//	cin>>t;
	while(t--) solve();
	return 0;
}

Answer: 1


#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
int main(){
    int n;
    cin >> n;
    vector<int> a(n);
    vector<pair<int,int>> ans;
    int an=0;
    for(int i=0;i<n;i++){
        int j;
        cin >> j;
        a[i]=j;
    }
    unordered_set<int> bks;
    for(int i=n-1;i>0;i--){
        int cur=a[i]-1;
        while(cur>0){
            if(bks.find(cur)==bks.end() && cur!=a[i]){
                ans.push_back(make_pair(a[i],cur));
                cur--;
            }else{
                cur--;
            }
        }
        cur=1;
        while(cur<=n){
            if(bks.find(cur)==bks.end()&&cur!=a[i]){
                ans.push_back(make_pair(cur,a[i]));
                cur++;
            }else{
                cur++;
            }
        }
        bks.emplace(a[i]);
    }
    cout << ans.size();
    cout << "\n";
    for(auto& [a,b]:ans){
        cout << a << " " << b << "\n";
    }
    return 0;
}

#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
int main(){
    int n;
    cin >> n;
    vector<int> a(n);
    vector<pair<int,int>> ans;
    int an=0;
    for(int i=0;i<n;i++){
        int j;
        cin >> j;
        a[i]=j;
    }
    int back=0;
    unordered_set<int> bks;
    for(int i=n-1;i>0;i--){
        int l=a[i]+i-1-back;//going all the way left&back to pos
        an+=l;
        int cur=back+1;
        while(cur>0){
            if(bks.find(cur)==bks.end() && cur!=a[i]){
                ans.push_back(make_pair(a[i],cur));
                cur--;
            }else{
                cur--;
            }
        }
        cur=1;
        while(cur<=i+1){
            if(bks.find(cur)==bks.end()&&cur!=a[i]){
                ans.push_back(make_pair(cur,a[i]));
                cur++;
            }else{
                cur++;
            }
        }
        back++;
        bks.emplace(a[i]);
    }
    cout << an;
    cout << "\n";
    for(auto& [a,b]:ans){
        cout << a << " " << b << "\n";
    }
    return 0;
}

Answer: 1


#include <bits/stdc++.h>
#include <vector>
#include <algorithm>
#define ll long long int
using namespace std;

int main() {
   int z;
   cin>>z;
    int a[z];
    int b[z];
    vector<vector<int>> v(z,vector<int> (z,0));
    for(int i=0;i<z;i++){
      cin>>a[i];
      b[i]=i+1;
    }
    vector<pair<int,int>> ans;
    for(int i=0;i<z;i++){
      if(b[i]!=a[i]){
        int j=i;
        while(b[j]!=a[i]){
         j++;
        }
        while (j>i)
        {
         ans.push_back({a[i],b[j-1]});
         swap(b[j],b[j-1]);
         v[b[j]-1][b[j-1]-1]=1;
         j--;
        }
      }
    }
   for(int i=z-1;i>=1;i--){
     int j=i-1;
     while(j>=0&&v[b[i]-1][b[j]-1]==0&&v[b[j]-1][b[i]-1]==0){
      ans.push_back({b[i],b[j]});
      j--;
     }
     while(j<i-1){
      ans.push_back({b[j+1],b[i]});
      j++;
     }
   }
   cout<<ans.size()<<endl;
   for(int i=0;i<ans.size();i++){
      cout<<ans[i].first<<" "<<ans[i].second<<endl;
   }

    return 0;
}

#include <bits/stdc++.h>
#include <vector>
#include <algorithm>
#define ll long long int
using namespace std;

int main() {
   int z;
   cin>>z;
    int a[z];
    int b[z];
    for(int i=0;i<z;i++){
      cin>>a[i];
      b[i]=z-i;
    }
    vector<pair<int,int>> ans;
    for(int i=z;i>=2;i--){
      for(int j=i-1;j>=1;j--){
         ans.push_back({i,j});
      }
    }
    for(int i=0;i<z-1;i++){
      int j=i;
      while(a[i]!=b[j]){
         j++;
      }
      while(j>i){
         ans.push_back({a[i],b[j-1]});
         swap(b[j],b[j-1]);
         j--;
      }
    }
   cout<<ans.size()<<endl;
   for(int i=0;i<ans.size();i++){
      cout<<ans[i].first<<" "<<ans[i].second<<endl;
   }

    return 0;
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) (x).begin(),(x).end()
typedef pair<int, int> PII;
const int N = 2e6 + 10;

int a[N];

void solve() {
    int n;
    cin>>n;
    map<int,int>pos;
    vector<PII>ans;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        pos[a[i]]=i;
    }
    for(int i=1;i<=n;i++){
        for(int j=pos[i];j<n;j++){
            swap(a[j],a[j+1]);
            pos[a[j]]=j;
            pos[a[j+1]]=j+1;
            ans.push_back({j,j+1});
            // for(int i=1;i<=n;i++){
            //     cout<<a[i]<<' ';
            // }
            // cout<<endl;
        }
        for(int j=n;j>=i+1;j--){
            swap(a[j],a[j-1]);
            pos[a[j]]=j;
            pos[a[j-1]]=j-1;
            ans.push_back({j,j-1});
            // for(int i=1;i<=n;i++){
            //     cout<<a[i]<<' ';
            // }
            // cout<<endl;
        }
    }
    // for(int i=0;i<ans.size();i++){
    //     cout<<ans[i].first<<' '<<ans[i].second<<endl;
    // }
    cout<<ans.size()<<endl;
    for(int i=ans.size()-1;i>=0;i--){
        cout<<ans[i].first<<' '<<ans[i].second<<endl;
    }
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int T = 1;
    //cin >> T;
    while (T--) solve();
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) (x).begin(),(x).end()
typedef pair<int, int> PII;
const int N = 2e6 + 10;

int a[N];

void solve() {
    int n;
    cin>>n;
    map<int,int>pos;
    vector<PII>ans;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        pos[a[i]]=i;
    }
    for(int i=1;i<=n;i++){
        for(int j=pos[i];j<n;j++){
            swap(a[j],a[j+1]);
            pos[a[j]]=j;
            pos[a[j+1]]=j+1;
            ans.push_back({a[j+1],a[j]});
            // for(int i=1;i<=n;i++){
            //     cout<<a[i]<<' ';
            // }
            // cout<<endl;
        }
        for(int j=n;j>=i+1;j--){
            swap(a[j],a[j-1]);
            pos[a[j]]=j;
            pos[a[j-1]]=j-1;
            ans.push_back({a[j],a[j-1]});
            // for(int i=1;i<=n;i++){
            //     cout<<a[i]<<' ';
            // }
            // cout<<endl;
        }
    }
    // for(int i=0;i<ans.size();i++){
    //     cout<<ans[i].first<<' '<<ans[i].second<<endl;
    // }
    cout<<ans.size()<<endl;
    for(int i=ans.size()-1;i>=0;i--){
        cout<<ans[i].first<<' '<<ans[i].second<<endl;
    }
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int T = 1;
    //cin >> T;
    while (T--) solve();
    return 0;
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fr(i, a, b) for (int i = a; i < b; i++)
#define vi vector<int>
#define vc vector<char>
#define pb push_back
#define ub(x, v) upper_bound(v.begin(), v.end(), x)
#define lb(x, v) lower_bound(v.begin(), v.end(), x)
#define lbi(x, v) lower_bound(v.begin(), v.end(), x) - v.begin()
#define ubi(x, v) upper_bound(v.begin(), v.end(), x) - v.begin()
#define all(a) (a).begin(), (a).end()
int power(int a, int b)
{
    int result = 1;
    while (b)
    {
        if (b & 1)
            result = result * a;
        a = a * a;
        b >>= 1;
    }
    return result;
}
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
signed main()
{
    fast;
    {
        int n;
        cin >> n;
        vi v(n);
        fr(i, 0, n) { cin >> v[i]; }
        vector<pair<int, int>> p;
        map<int, vi> m;
        fr(i, 0, n - 1)
        {
            fr(j, 1 + i, n)
            {
                if (v[i] > v[j])
                {
                    m[v[i]].pb(v[j]);
                }
                else
                {
                    m[v[i]].pb(v[j]);
                    m[v[j]].pb(v[i]);
                }
            }
        }
        for (int i = n; i > 0; i--)
        {
            if (!m[i].empty())
            {
                sort(all(m[i]));
                vi temp;
                for (int j = m[i].size() - 1; j >= 0; j--)
                {
                    if (m[i][j] > i)
                    {
                        temp.pb(m[i][j]);
                    }
                    else
                    {
                        p.pb({i, m[i][j]});
                    }
                }
                if (!temp.empty())
                {
                    sort(all(temp));
                    fr(j, 0, temp.size())
                    {
                        p.pb({i, temp[j]});
                    }
                }
            }
        }
        cout << p.size() << endl;
        fr(i, 0, p.size())
        {
            cout << p[i].first << " " << p[i].second << endl;
        }
    }
}
/*
1 2 3 4 5
5 3 2 1 4
tot = 13
unordered
5 3
5 2
5 1
5 4
3 2
3 1
4 3
3 4
2 1
4 2
2 4
4 1
1 4

if we sort
5 4
5 3
5 2
5 1
5 1 2 3 4
4 3
4 2
4 1
5 4 1 2 3
3 2
3 1
3 4
5 3 4 1 2
2 1
2 4
5 3 2 4 1
1 4
5 3 2 1 4

2 1
3 1
4 1
5 1
2 3 4 5 1
4 3
4 2

1 4
*/

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fr(i, a, b) for (int i = a; i < b; i++)
#define vi vector<int>
#define vc vector<char>
#define pb push_back
#define ub(x, v) upper_bound(v.begin(), v.end(), x)
#define lb(x, v) lower_bound(v.begin(), v.end(), x)
#define lbi(x, v) lower_bound(v.begin(), v.end(), x) - v.begin()
#define ubi(x, v) upper_bound(v.begin(), v.end(), x) - v.begin()
#define all(a) (a).begin(), (a).end()
int power(int a, int b)
{
    int result = 1;
    while (b)
    {
        if (b & 1)
            result = result * a;
        a = a * a;
        b >>= 1;
    }
    return result;
}
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
signed main()
{
    fast;
    {
        int n;
        cin >> n;
        vi v(n);
        fr(i, 0, n) { cin >> v[i]; }
        vector<pair<int, int>> p;
        set<int> done;
        map<int, vi> m;
        fr(i, 0, n)
        {
            fr(j, v[i] + 1, n + 1)
            {
                if (done.find(j) == done.end())
                {
                    p.pb({j, v[i]});
                }
            }
            for (int j = n; j > v[i]; j--)
            {
                if (done.find(j) == done.end())
                {
                    p.pb({v[i], j});
                }
            }
            for (int j = v[i]-1; j > 0; j--)
            {
                if (done.find(j) == done.end())
                {
                    p.pb({v[i], j});
                }
            }
            done.insert(v[i]);
        }
        // for (int i = n; i > 1; i--)
        // {
        //     if (!m[i].empty())
        //     {
        //         sort(all(m[i]));
        //         vi temp;
        //         for (int j = m[i].size() - 1; j >= 0; j--)
        //         {
        //             if (m[i][j] > i)
        //             {
        //                 temp.pb(m[i][j]);
        //             }
        //             else
        //             {
        //                 p.pb({i, m[i][j]});
        //             }
        //         }
        //         if (!temp.empty())
        //         {
        //             sort(all(temp));
        //             fr(j, 0, temp.size())
        //             {
        //                 p.pb({i, temp[j]});
        //             }
        //         }
        //     }
        // }
        // if (!m[1].empty())
        // {
        //     sort(all(m[1]));
        //     for (int i = m[1].size() - 1; i >= 0; i--)
        //     {
        //         p.pb({1, m[1][i]});
        //     }
        // }

        cout << p.size() << endl;
        fr(i, 0, p.size())
        {
            cout << p[i].first << " " << p[i].second << endl;
        }
    }
}
/*
1 2 3 4 5
5 3 2 1 4
tot = 13
unordered
5 3
5 2
5 1
5 4
3 2
3 1
4 3
3 4
2 1
4 2
2 4
4 1
1 4

if we sort
5 4
5 3
5 2
5 1
5 1 2 3 4
4 3
4 2
4 1
5 4 1 2 3
3 2
3 1
3 4
5 3 4 1 2
2 1
2 4
5 3 2 4 1
1 4
5 3 2 1 4

3
1 2 3
unordered
1 2
2 1
1 3
3 1
2 3
3 2
 ordered
3 2
3 1
3 1 2
2 1
2 3
2 3 1
1 3
1 2

4 1 2 3
4 1
4 2
4 3
2 1
// 1 2
3 1
// 1 3
3 2
// 2 3

4 3
4 2
4 1
4 1 2 3
3 2
2 3

4 3 1 2
2 1
2 3
4 2 3 1

*/

Answer: 2


#include<bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define int long long
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int a,b,d,k;;
    cin>>b;
    vector<int> v;for(int i=0;i<b;i++){cin>>d;v.insert(v.end(),d);}
    vector<int> c;
    vector<bool> n(b);
    vector<array<int,2>> g;
    for(int i=0;i<b;i++){
        c.insert(c.end(),i);
    }
    for(int i=0;i<b;i++){
        n[v[i]]=1;
        for(int j=v[i];j<b;j++){
            if(n[j]!=1){
                g.insert(g.end(),{j,v[i]});
            }
        }
        for(int j=b;j>=1;j--){
            if(n[j]!=1){
                g.insert(g.end(),{v[i],j});
            }
        }
    }
    cout<<g.size()<<endl;
    for(auto [x,y]:g){
        cout<<x<<' '<<y<<endl;
    }
    v.erase(all(v));
    c.erase(all(c));
    n.erase(all(n));
    g.erase(all(g));
    }

#include<bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define int long long
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int a,b,d,k;;
    cin>>b;
    vector<int> v;for(int i=0;i<b;i++){cin>>d;v.insert(v.end(),d);}
    vector<int> c;
    vector<bool> n(b);
    vector<array<int,2>> g;
    for(int i=0;i<b;i++){
        c.insert(c.end(),i);
    }
    for(int i=0;i<b;i++){
        n[v[i]]=1;
        for(int j=v[i];j<=b;j++){
            if(n[j]!=1){
                g.insert(g.end(),{j,v[i]});
            }
        }
        for(int j=b;j>=1;j--){
            if(n[j]!=1){
                g.insert(g.end(),{v[i],j});
            }
        }
    }
    cout<<g.size()<<endl;
    for(auto [x,y]:g){
        cout<<x<<' '<<y<<endl;
    }
    v.erase(all(v));
    c.erase(all(c));
    n.erase(all(n));
    g.erase(all(g));
    }

Answer: 2


/*
  卫风·芄兰
芄兰之支,童子佩觿.
虽则佩觿,能不我知?
容兮遂兮,垂带悸兮.
芄兰之叶,童子佩韘.
虽则佩韘,能不我甲?
容兮遂兮,垂带悸兮.
*/
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
#define tul array<int,3>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define rep_(i,a,b) for(int i=a;i>=b;--i)
#define all(x) x.begin(),x.end()
#define bp(x) __builtin_popcountll(x)
#define ctz(x) __builtin_ctzll(x)
#define cy cout<<"YES"<<endl
#define cn cout<<"NO"<<endl
#define lc (rt<<1)
#define rc (rt<<1|1)
mt19937_64 rnd(time(0));
const int N=3e5+5,yyx=998244353;
vector<PII> to[N];
int n,m,a[N],b[N];
inline int mod(int x){
  return (x%yyx+yyx)%yyx;
}
inline int cmin(int &x,int y){
  return x>y?x=y,1:0;
}
inline int cmax(int &x,int y){
  return x<y?x=y,1:0;
}
vector<PII> t;
void bfs(){
   rep_(i,n,1) rep_(j,i-1,1) t.push_back({i,j});
   rep(i,1,n) b[i]=n-i+1;
   rep(i,1,n){
    int z=1;
    rep(j,1,n) if(a[i]==b[j]) z=j;
    while(z>i){
        t.push_back({b[z],b[z-1]});
        swap(b[z],b[z-1]);
        z--;
    }
   }
}
inline void solve(){
  cin>>n;
  rep(i,1,n) cin>>a[i];
  bfs();
  cout<<t.size()<<endl;
  for(auto [x,y]:t) cout<<x<<" "<<y<<endl;
}
signed main(){
  cin.tie(0)->sync_with_stdio(0);
  //freopen("D://321//in.txt","r",stdin);
  //freopen("D://321//out.txt","w",stdout);
  int _=1;
  //cin>>_;
  while(_--)
  solve();
  return 0;
}

/*
  卫风·芄兰
芄兰之支,童子佩觿.
虽则佩觿,能不我知?
容兮遂兮,垂带悸兮.
芄兰之叶,童子佩韘.
虽则佩韘,能不我甲?
容兮遂兮,垂带悸兮.
*/
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
#define tul array<int,3>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define rep_(i,a,b) for(int i=a;i>=b;--i)
#define all(x) x.begin(),x.end()
#define bp(x) __builtin_popcountll(x)
#define ctz(x) __builtin_ctzll(x)
#define cy cout<<"YES"<<endl
#define cn cout<<"NO"<<endl
#define lc (rt<<1)
#define rc (rt<<1|1)
mt19937_64 rnd(time(0));
const int N=3e5+5,yyx=998244353;
vector<int> to[N];
int n,m,a[N];
inline int mod(int x){
  return (x%yyx+yyx)%yyx;
}
inline int cmin(int &x,int y){
  return x>y?x=y,1:0;
}
inline int cmax(int &x,int y){
  return x<y?x=y,1:0;
}
inline void solve(){
  cin>>n;
  set<PII> s;
  vector<PII> t;
  rep(i,1,n) cin>>a[i];
  rep(i,1,n){
    rep(j,1,i-1){
        if(a[j]<a[i]) s.insert({a[i],a[j]}),s.insert({a[j],a[i]});
        else s.insert({a[j],a[i]});
    }
  }
  cout<<s.size()<<endl;
  for(auto [x,y]:s) t.push_back({x,y});
  sort(all(t),[&](PII x,PII y){
    return x.second==y.second?x.first<y.first:x.second<y.second;
  });
  for(auto [x,y]:t) cout<<x<<" "<<y<<endl;
}
signed main(){
  cin.tie(0)->sync_with_stdio(0);
  //freopen("D://321//in.txt","r",stdin);
  //freopen("D://321//out.txt","w",stdout);
  int _=1;
  //cin>>_;
  while(_--)
  solve();
  return 0;
}

Answer: 1


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N = 1002, M = 2e5 + 9, mod = 998244353, inf = 1e9;
void solve() {
    int n;
    cin >> n;
    vector<int> v(n), pos(n + 1);
    for(int i = 0; i < n; i++) {
        cin >> v[i];
        pos[v[i]] = i;
    }
    vector<pair<int, int>> vec;
    for(int i = 1; i <= n; i++) {
        for(int j = pos[i] - 1; j >= 0; j--) {
            if(v[j] > i) {
                vec.push_back({v[j], i});
            }
            else {
                vec.push_back({i, v[j]});
                vec.push_back({v[j], i});
            }
        }
    }
    cout << vec.size() << "\n";
    for(auto [x, y] : vec) {
        cout << x << " " << y << "\n";
    }
}
int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int t = 1, cs = 1;
    //cin >> t;
    while (t--) {
        //cout << "Case " << cs++ << ": ";
        solve();
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N = 1002, M = 2e5 + 9, mod = 998244353, inf = 1e9;
void solve() {
    int n;
    cin >> n;
    vector<int> v(n), ans(n + 1);
    for(int i = 0; i < n; i++) {
        cin >> v[i];
        ans[i] = i + 1;
    }
    vector<pair<int, int>> vec;
    for(int i = n - 1; i >= 0; i--) {
        int val = v[i], pos = 0;
        for(int j = 0; j < n; j++) {
            if(ans[j] == val) {
                pos = j;
                break;
            }
        }
        for(int j = pos - 1; j >= 0; j--) {
            vec.push_back({val, ans[j]});
        }
        for(int j = 0; j < pos; j++) {
            vec.push_back({ans[j], val});
        }
        for(int j = pos + 1; j <= i; j++) {
            vec.push_back({ans[j], ans[j - 1]});
            swap(ans[j], ans[j - 1]);
        }
    }
    cout << vec.size() << "\n";
    for(auto [x, y] : vec) {
        cout << x << " " << y << "\n";
    }
}
int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int t = 1, cs = 1;
    //cin >> t;
    while (t--) {
        //cout << "Case " << cs++ << ": ";
        solve();
    }
    return 0;
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'

void solve()
{
    ll n,val,pos;cin>>n;
    vector <ll> v(n),p(n);
    vector <pair<ll,ll>> vp;

    for(auto &i:v) cin>>i;
    iota(p.begin(),p.end(),1);

    for(int i=n-1;i>=0;i--){
        val=v[i];
        for(int j=0;j<n;j++){
            if(val==p[j]){
                pos=j;
                break;
            }
        }
        for(int j=pos-1;j>=0;j--){
            vp.push_back({p[j],val});
            swap(p[j],p[j+1]);
        }
        for(int j=0;j<i;j++){
            vp.push_back({p[j],p[j+1]});
            swap(p[j],p[j+1]);
        }
    }

    cout<<vp.size()<<endl;
    for(auto i:vp){
        cout<<i.first<<" "<<i.second<<endl;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int t=1;//cin>>t;
    while(t--){
        solve();
    }
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'

void solve()
{
    ll n,val,pos;cin>>n;
    vector <ll> v(n),p(n);
    vector <pair<ll,ll>> vp;

    for(auto &i:v) cin>>i;
    iota(p.begin(),p.end(),1);

    for(int i=n-1;i>=0;i--){
        val=v[i];
        for(int j=0;j<n;j++){
            if(val==p[j]){
                pos=j;
                break;
            }
        }
        for(int j=pos-1;j>=0;j--){
            vp.push_back({val,p[j]});
            swap(p[j],p[j+1]);
        }
        for(int j=0;j<i;j++){
            vp.push_back({p[j+1],p[j]});
            swap(p[j],p[j+1]);
        }
    }

    cout<<vp.size()<<endl;
    for(auto i:vp){
        cout<<i.first<<" "<<i.second<<endl;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int t=1;//cin>>t;
    while(t--){
        solve();
    }
}

Answer: 2


Answer: 1


#include <bits/stdc++.h>
using namespace std;
#define int long long

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 1e18;

void Solve()
{
	int n;
	cin >> n;
	vector<int> a(n + 1), pos(n + 1);
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		pos[a[i]] = i;
	}
	vector<pair<int, int> > overtakes;
	vector<int> current_positioning;
	current_positioning.push_back(1);
	for(int i = 2; i <= n; i++) {
		for(int j = current_positioning.size() - 1; j >= 0; j--) {
			overtakes.push_back({i, current_positioning[j]});
		}
		for(int j = 0; j < current_positioning.size(); j++) {
			if(pos[current_positioning[j]] < pos[i]) {
				overtakes.push_back({current_positioning[j], i});
			}
			else {
				current_positioning.insert(current_positioning.begin() + j, i);
				break;
			}
		}

		if(current_positioning.size() == i - 1) {
			current_positioning.push_back(i);
		}

		// for(int j = 0; j < current_positioning.size(); j++) {
		// 	cout << current_positioning[j] << " ";
		// }
		// cout << "\n";
	}

	// for(int i = 0; i < n; i++) {
	// 	for(int j = i + 1; j < n; j++) {
	// 		if(a[i] < a[j]) {
	// 			overtakes.push_back({a[j], a[i]});
	// 			overtakes.push_back({a[i], a[j]});
	// 		}
	// 		else {
	// 			overtakes.push_back({a[i], a[j]});
	// 		}
	// 	}
	// }

	cout << overtakes.size() << "\n";
	for(auto p: overtakes) {
		cout << p.first << " " << p.second << "\n";
	}
}

int32_t main()
{
	auto begin = std::chrono::high_resolution_clock::now();
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	// cin >> t;
	for(int i = 1; i <= t; i++)
	{
		//cout << "Case #" << i << ": ";
		Solve();
	}
	auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    //cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 1e18;

void Solve()
{
	int n;
	cin >> n;
	vector<int> a(n);
	for(int &x: a) {
		cin >> x;
	}
	vector<pair<int, int> > overtakes;
	for(int i = 0; i < n; i++) {
		for(int j = i + 1; j < n; j++) {
			if(a[i] < a[j]) {
				overtakes.push_back({a[j], a[i]});
				overtakes.push_back({a[i], a[j]});
			}
			else {
				overtakes.push_back({a[i], a[j]});
			}
		}
	}

	cout << overtakes.size() << "\n";
	for(auto p: overtakes) {
		cout << p.first << " " << p.second << "\n";
	}
}

int32_t main()
{
	auto begin = std::chrono::high_resolution_clock::now();
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	// cin >> t;
	for(int i = 1; i <= t; i++)
	{
		//cout << "Case #" << i << ": ";
		Solve();
	}
	auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    //cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
	return 0;
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

#define rep(i,a,b) for(ll i=(a); i<(b); ++i)
#define repc(i,a,b,c) for(ll i=(a); i<(b); i+=c)
#define repn(i,a,b) for(ll i=(a); i>(b); --i)
#define trav(a,x) for(auto& a: x)
#define travm(a,x) for(const auto& [a, v]: x)
#define all(x) begin(x),end(x)
#define sz(x) (int)size(x)
#define tmod(a) ((a%MOD)+MOD)%MOD
#define tmod2(a) ((a%MOD2)+MOD2)%MOD2
#define tmodc(a,b) ((a%b)+b)%b
#define madd(a,b) (tmod(a)+tmod(b))%MOD
#define madd2(a,b) (tmod2(a)+tmod2(b))%MOD2
#define maddc(a,b,c) (tmodc(a,c)+tmodc(b,c))%c
#define pb push_back
#define mp make_pair
#define cauto const auto
using ll = long long;
int iINF = numeric_limits<int>::max();
int MOD = 1e9+7;
int MOD2 = 1e9+22;
#define int ll
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vd = vector<double>;
using vll = vector<ll>;
using vpii = vector<pii>;

ll lINF = numeric_limits<ll>::max();
ll negINF = lINF+1;
double EPS = .00001;
cauto maxn = 200001;

/*
3 1 4 2

4 3 >3notseen inc

3 4 allbutseen dec
3 2
3 1

2 1 >1notseen inc
4 1

1 4 allbutseen dec
1 2

4 2 allbutseen dec
*/
void solve()
{
  int n;
  cin >> n;
  vi a(n+1);
  rep(i,1,n+1) cin >> a[i];
  vpii moves;
  vi seen(n+1);
  rep(i,1,n)
  {
    seen[a[i]] = 1;
    rep(j,a[i]+1,n+1) if(!seen[j]) moves.pb({j,a[i]});
    repn(j,n,0) if(!seen[j]) moves.pb({a[i],j});
  }
  cout << sz(moves) << "\n";
  trav(move,moves) cout << move.first << " " << move.second << "\n";
}
void pre()
{

}
int32_t main()
{
  ios::sync_with_stdio(false);
  pre();
  int t=1; //cin >> t;
  while(t--) solve();
}
/*
When flooring double that has been divided, add .00000000001
Careful mixing subtracting and mod!
*/

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

#define rep(i,a,b) for(ll i=(a); i<(b); ++i)
#define repc(i,a,b,c) for(ll i=(a); i<(b); i+=c)
#define repn(i,a,b) for(ll i=(a); i>(b); --i)
#define trav(a,x) for(auto& a: x)
#define travm(a,x) for(const auto& [a, v]: x)
#define all(x) begin(x),end(x)
#define sz(x) (int)size(x)
#define tmod(a) ((a%MOD)+MOD)%MOD
#define tmod2(a) ((a%MOD2)+MOD2)%MOD2
#define tmodc(a,b) ((a%b)+b)%b
#define madd(a,b) (tmod(a)+tmod(b))%MOD
#define madd2(a,b) (tmod2(a)+tmod2(b))%MOD2
#define maddc(a,b,c) (tmodc(a,c)+tmodc(b,c))%c
#define pb push_back
#define mp make_pair
#define cauto const auto
using ll = long long;
int iINF = numeric_limits<int>::max();
int MOD = 1e9+7;
int MOD2 = 1e9+22;
#define int ll
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vd = vector<double>;
using vll = vector<ll>;
using vpii = vector<pii>;

ll lINF = numeric_limits<ll>::max();
ll negINF = lINF+1;
double EPS = .00001;
cauto maxn = 200001;

/*
4 3
4 2
4 1
3 2
3 1
2 1

1 2
1 3
1 4
2 3
2 4
3 4
*/
void solve()
{
  int n;
  cin >> n;
  vi a(n+1);
  rep(i,1,n+1) cin >> a[i];
  vpii groups;
  int st = a[1];
  int sz = 1;
  rep(i,2,n+1)
  {
    if(a[i]==st+sz) sz++;
    else groups.pb({st,sz}), st = a[i], sz = 1;
  }
  groups.pb({st,sz});
  vpii moves;
  vi seen(n+1);
  rep(i,1,n+1)
  {
    repn(j,a[i]-1,0) if(!seen[j]) moves.pb({a[i],j});
    seen[a[i]] = 1;
  }
  trav(group,groups)
  {
    if(group.second==1) continue;
    repn(i,group.first+group.second-1,group.first) repn(j,i-1,group.first-1) moves.pb({i,j});
    rep(i,group.first,group.first+group.second-1) rep(j,i+1,group.first+group.second) moves.pb({i,j});
  }
  cout << sz(moves) << "\n";
  trav(move,moves) cout << move.first << " " << move.second << "\n";
}
void pre()
{

}
int32_t main()
{
  ios::sync_with_stdio(false);
  pre();
  int t=1; //cin >> t;
  while(t--) solve();
}
/*
When flooring double that has been divided, add .00000000001
Careful mixing subtracting and mod!
*/

Answer: 1