#include <bits/stdc++.h>
using namespace std;
#define f(i, a) for(ll (i) = 0; (i)<(a) ;(i)++)
#define r(i, a) for (ll (i) = a-1; (i)>=0 ;(i)--)
#define F(i, a, b) for(ll (i) = (a); (i)<(b) ;(i)++)
#define R(i, a, b) for (ll (i) = (b)-1; (i)>=(a) ;(i)--)
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define ll long long int

void sol(){
    ll n;
    cin>>n;
    ll a[n+1] = {0};
    ll c[n];
    ll b[n];
    f(i, n) {cin>>b[i]; c[i] = i+1;}
    vector<pair<ll, ll>> v;
    r(i, n){
        ll vic = b[i];
        a[vic] = 1;
        ll j=0;
        f(k, n){if(c[k]==vic){j=k;break;}}
        while(j>0){
            j--;
            v.pb({c[j+1], c[j]});
            swap(c[j], c[j+1]);
        }
        while(j<i){
            v.pb({c[j+1], c[j]});
            swap(c[j], c[j+1]);
            j++;
        }

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

}

int main(){

 //#ifndef ONLINE_JUDGE
 //freopen("input.txt", "r", stdin);
 //freopen("output.txt", "w", stdout);
 //#endif
 ios_base::sync_with_stdio(false);
 cin.tie(NULL);
 ll T;
//  cin>>T; while(T--)
        {
     sol();
        }
}

#include <bits/stdc++.h>
using namespace std;
#define f(i, a) for(ll (i) = 0; (i)<(a) ;(i)++)
#define r(i, a) for (ll (i) = a-1; (i)>=0 ;(i)--)
#define F(i, a, b) for(ll (i) = (a); (i)<(b) ;(i)++)
#define R(i, a, b) for (ll (i) = (b)-1; (i)>=(a) ;(i)--)
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define ll long long int

void sol(){
    ll n;
    cin>>n;
    ll a[n+1] = {0};
    ll b[n];
    f(i, n) cin>>b[i];
    vector<pair<ll, ll>> v;
    r(i, n){
        ll vic = b[i];
        a[vic] = 1;
        ll j = vic-1;
        while(j>0){
            // cout<<vic<<" - "<<j<<endl;
            if(a[j]==0)
            v.pb({vic, j});
            j--;
        }
        j++;
        while(j<=i+1){
            // cout<<vic<<" + "<<j<<endl;

            if(a[j]==0)
            v.pb({j, vic});
            j++;
        }

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

}

int main(){

 //#ifndef ONLINE_JUDGE
 //freopen("input.txt", "r", stdin);
 //freopen("output.txt", "w", stdout);
 //#endif
 ios_base::sync_with_stdio(false);
 cin.tie(NULL);
 ll T;
//  cin>>T; while(T--)
        {
     sol();
        }
}

Answer: 1


// Problem: A. Adrenaline Rush
// Contest: Codeforces - 2024-2025 ICPC, NERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams Preferred)
// URL: https://codeforces.com/problemset/problem/2052/A
// Memory Limit: 1024 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int, int> pii;
typedef pair<int, long long> pil;
typedef pair<long long, int> pli;

const int M = 1e9+7;
const int N = 2e6 + 10;
int a[N], b[N];
int l[N], r[N];
char g[110][110];
char mapp[110][110];
bool st[110][110];
int n, m;

void solve() {
	cin >> n;
	set<int> S;
	for(int i = 1;i <= n;i ++) {
		cin >> a[i];
		S.insert(i);
	}

	vector<pii> ans;

	for(int i = n;i >= 1;i --) {
		for(int j = a[i] - 1;j >= 1;j --) {
			if(S.count(j) == 0) continue;
			ans.push_back({a[i], j});
		}
		S.erase(a[i]);
		for(auto t : S) {
			ans.push_back({t, a[i]});
		}
	}

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

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

// Problem: A. Adrenaline Rush
// Contest: Codeforces - 2024-2025 ICPC, NERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams Preferred)
// URL: https://codeforces.com/problemset/problem/2052/A
// Memory Limit: 1024 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int, int> pii;
typedef pair<int, long long> pil;
typedef pair<long long, int> pli;

const int M = 1e9+7;
const int N = 2e6 + 10;
int a[N], b[N];
int l[N], r[N];
char g[110][110];
char mapp[110][110];
bool st[110][110];
int n, m;

void solve() {
	cin >> n;
	for(int i = 1;i <= n;i ++) {
		cin >> a[i];
	}

	vector<pii> ans;

	for(int i = n;i >= 1;i --) {
		if(a[i] < i) {
			for(int j = a[i] + 1;j <= i;j ++) {
				ans.push_back({j, a[i]});
			}
		} else {
			if(i == 1) break;
			ans.push_back({a[i], a[i - 1]});
			ans.push_back({a[i - 1], a[i]});
		}
	}

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

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

Answer: 1


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
typedef long double ld;
using namespace std;
using namespace __gnu_pbds;
using ordered_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;
#define all(x) x.begin(), x.end()
#define pll pair<ll, ll>
#define vec vector
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
const ll mod = 998244353;
ll fpow(ll a, ll b, ll m = -1)
{
	if (m != -1)
		a %= m;
	ll res = 1;
	while (b > 0)
	{
		if (b % 2 == 1)
		{
			if (m != -1)
				res = res * a % m;
			else
			{
				res *= a;
			}
		}
		a = a * a;
		if (m != -1)
		{
			a %= m;
		}
		b /= 2;
	}
	return res;
}
int main()
{
	fast;
	// freopen("pails.in", "r", stdin);
	// freopen("pails.out", "w", stdout);
	ll t = 1;
	// cin >> t;
end:;
	while (t--)
	{
		ll n, k;
		cin >> n;
		vec<ll> v(n);
		for (int i = 0; i < n; i++)
		{
			cin >> v[i];
		}
		set<ll> st;
		ll mx = 0;
		vec<pll> ans;
		for (int i = 0; i < n; i++)
		{
			for(int j = i - 1; j >= 0; j--)
			{
				swap(v[j+1], v[j]);
				ans.push_back({v[j+1], v[j]});
			}
			ll in = 0;
			while(in < i && v[in]> v[in+1])
			{
				swap(v[in], v[in+1]);
				ans.push_back({v[in+1], v[in]});
				in++;
			}
		}
		reverse(all(ans));
		cout << ans.size() << '\n';
		for (int i = 0; i < ans.size(); i++)
		{
			cout << ans[i].first << ' ' << ans[i].second << '\n';
		}

	}
	return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
typedef long double ld;
using namespace std;
using namespace __gnu_pbds;
using ordered_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;
#define all(x) x.begin(), x.end()
#define pll pair<ll, ll>
#define vec vector
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
const ll mod = 998244353;
ll fpow(ll a, ll b, ll m = -1)
{
	if (m != -1)
		a %= m;
	ll res = 1;
	while (b > 0)
	{
		if (b % 2 == 1)
		{
			if (m != -1)
				res = res * a % m;
			else
			{
				res *= a;
			}
		}
		a = a * a;
		if (m != -1)
		{
			a %= m;
		}
		b /= 2;
	}
	return res;
}
int main()
{
	fast;
	// freopen("pails.in", "r", stdin);
	// freopen("pails.out", "w", stdout);
	ll t = 1;
	// cin >> t;
end:;
	while (t--)
	{
		ll n, k;
		cin >> n;
		vec<ll> v(n);
		for (int i = 0; i < n; i++)
		{
			cin >> v[i];
		}
		set<ll> st;
		ll mx = 0;
		vec<pll> ans;
		for (int i = 0; i < n; i++)
		{
			for(int j = i - 1; j >= 0; j--)
			{
				swap(v[j+1], v[j]);
				ans.push_back({v[j+1], v[j]});
			}
			ll in = 0;
			while(in < i && v[in]> v[in+1])
			{
				swap(v[in], v[in+1]);
				ans.push_back({v[in+1], v[in]});
			}
		}
		reverse(all(ans));
		cout << ans.size() << '\n';
		for (int i = 0; i < ans.size(); i++)
		{
			cout << ans[i].first << ' ' << ans[i].second << '\n';
		}

	}
	return 0;
}

Answer: 1


#include <iostream>
#include<vector>
#include<queue>
#include<iterator>
#include<algorithm>
#include<stack>
#include<set>
#include<unordered_set>
#include<map>
#include <numeric>
#include <tuple>
#include <unordered_map>
#include <cstdlib>
#include <string>
#define N 200001
#define M 1001

using namespace std;
using ll = long long;

int ord[M],arr[M];
int state[M][M];
int find(int x,int n){
    for(int i=1;i<=n;i++){
        if(arr[i] == x){
            return i;
        }
    }
    return -1;
}

int main(){
    int n;
    cin>>n;
    //ll ans = 0ll;
    for(int i=0;i<=n;i++){
        for(int j=0;j<=n;j++){
            state[i][j] = 0;
        }
    }
    for(int i=1;i<=n;i++){
        cin>>ord[i];
        arr[i] = i;
    }
    vector<pair<int,int> > ans;

    for(int i=1;i<=n;i++){
        if(ord[i] == arr[i]){
            continue;
        }
        else{
            int j = find(ord[i],n);
            for(int k=j;k<n;k++){
                ans.push_back(make_pair(arr[k+1],arr[k]));
                swap(arr[k],arr[k+1]);
                state[arr[k]][arr[k+1]] = 1;
            }
            for(int k=n;k>j;k--){
                ans.push_back(make_pair(arr[k],arr[k-1]));
                swap(arr[k],arr[k-1]);
                state[arr[k-1]][arr[k]] = 1;
            }
            for(int k=j;k>i;k--){
                ans.push_back(make_pair(arr[k],arr[k-1]));
                swap(arr[k],arr[k-1]);
                state[arr[k-1]][arr[k]] = 1;

            }
        }
    }

    for(int i=1;i<n;i++){
        int k = i;
        for(int j=i+1;j<=n;j++){
            //printf("%d %d:%d\n",arr[j-1],arr[j],state[arr[j-1]][arr[j]]);
            if(!state[arr[j-1]][arr[j]]){
                ans.push_back(make_pair(arr[j],arr[j-1]));
                swap(arr[j-1],arr[j]);
                state[arr[j-1]][arr[j]] = 1;
                k = j;
            }
            else {
                k = j-1;
                break;
            }

        }
        if(k > i){
            while(k != i){
                ans.push_back(make_pair(arr[k],arr[k-1]));
                swap(arr[k],arr[k-1]);
                state[arr[k]][arr[k-1]] = 1;
                k--;
            }
        }
    }
    //cout << state[3][1] << endl;
    cout << ans.size() << endl;
    for(int i=0;i<ans.size();i++){
        printf("%d %d\n",ans[i].first,ans[i].second);
    }
}

#include <iostream>
#include<vector>
#include<queue>
#include<iterator>
#include<algorithm>
#include<stack>
#include<set>
#include<unordered_set>
#include<map>
#include <numeric>
#include <tuple>
#include <unordered_map>
#include <cstdlib>
#include <string>
#define N 200001
#define M 1001

using namespace std;
using ll = long long;

int ord[M],arr[M];
int state[M][M];
int find(int x,int n){
    for(int i=1;i<=n;i++){
        if(arr[i] == x){
            return i;
        }
    }
    return -1;
}

int main(){
    int n;
    cin>>n;
    //ll ans = 0ll;
    for(int i=0;i<=n;i++){
        for(int j=0;j<=n;j++){
            state[i][j] = 0;
        }
    }
    for(int i=1;i<=n;i++){
        cin>>ord[i];
        arr[i] = i;
    }
    vector<pair<int,int> > ans;

    for(int i=1;i<=n;i++){
        if(ord[i] == arr[i]){
            continue;
        }
        else{
            int j = find(ord[i],n);
            for(int k=j;k>i;k--){
                ans.push_back(make_pair(arr[k],arr[k-1]));
                swap(arr[k],arr[k-1]);
                state[arr[k-1]][arr[k]] = 1;

            }
        }
    }
    for(int i=1;i<n;i++){
        int k = i;
        for(int j=i+1;j<=n;j++){
            if(!state[arr[i]][arr[j]]){
                ans.push_back(make_pair(arr[j],arr[j-1]));
                swap(arr[j-1],arr[j]);
                state[arr[j-1]][arr[j]] = 1;
            }
            else{
                k = j-1;
                break;
            }
            if(j == n){
                k = n;
            }
        }
        if(k > i){
            while(k != i){
                ans.push_back(make_pair(arr[k],arr[k-1]));
                swap(arr[k],arr[k-1]);
                state[arr[k]][arr[k-1]] = 1;
                k--;
            }
        }
    }
    cout << ans.size() << endl;
    for(int i=0;i<ans.size();i++){
        printf("%d %d\n",ans[i].first,ans[i].second);
    }
}

Answer: 1


#include <iostream>
#include <algorithm>

using namespace std;

int a[1005], cur[1005], ans[1000005], ans1[1000005];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    int cnt = 0;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }

    for(int i = 1; i <= n; i++){
        cur[i] = i;
    }
    for(int i = n; i >= 2; i--){
        int pos = 0;
        for(int j = 1; j <= n; j++){
            if(cur[j] == a[i]){
                pos = j;
                break;
            }
        }
        for(int j = pos; j >= 2; j--){
            cnt++;
            ans[cnt] = cur[j];
            ans1[cnt] = cur[j - 1];
            swap(cur[j], cur[j - 1]);
        }
        for(int j = 1; j <= i - 1; j++){
            cnt++;
            ans[cnt] = cur[j + 1];
            ans1[cnt] = cur[j];
            swap(cur[j], cur[j + 1]);
        }
    }

    cout << cnt << "\n";
    for(int i = 1; i <= cnt; i++){
        cout << ans[i] << " " << ans1[i] << "\n";
    }
    return 0;
}

#include <iostream>
#include <algorithm>

using namespace std;

int a[1005], cur[1005], ans[1005], ans1[1005];

int main(){
    int n;
    cin >> n;
    int cnt = 0;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }

    for(int i = 1; i <= n; i++){
        cur[i] = i;
    }
    for(int i = n; i >= 2; i--){
        int pos = 0;
        for(int j = 1; j <= n; j++){
            if(cur[j] == a[i]){
                pos = j;
                break;
            }
        }
        for(int j = pos; j >= 2; j--){
            cnt++;
            ans[cnt] = cur[j];
            ans1[cnt] = cur[j - 1];
            swap(cur[j], cur[j - 1]);
        }
        for(int j = 1; j <= i - 1; j++){
            cnt++;
            ans[cnt] = cur[j + 1];
            ans1[cnt] = cur[j];
            swap(cur[j], cur[j + 1]);
        }
    }

    cout << cnt << "\n";
    for(int i = 1; i <= cnt; i++){
        cout << ans[i] << " " << ans1[i] << "\n";
    }
    return 0;
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> ve;
typedef vector<long long> vl;
typedef vector<pair<int,int>> vp;
typedef vector<char> vc;
typedef vector<double> vd;
#define fi first
#define se second
#define pb push_back
#define con continue
#define rep(i,a,b) for (int i=a;i<b;i++)
#define res(j,a,b) for (int j=a;j>=b;j--)
const double eps = 1e-6;
#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define endl "\n"
signed main()
{
    fastio;
        int n;
        cin>>n;
        ve v(n);
        stack<int>a,b;
        rep(i,0,n){
        cin>>v[i];
        a.push(n-i);
        }
        if(n==1){
        cout<<"0"<<endl;
        }
        else{
        vp ans;
        res(j,n-1,0){
            while(a.top()!=v[j]){
               b.push(a.top());
               a.pop();
            }
            a.pop();
            while(!b.empty()){
                ans.pb({v[j],b.top()});
                a.push(b.top());
                b.pop();
            }
            a.push(v[j]);
        }
        cout<<ans.size()<<endl;
        for(auto k:ans)
        cout<<k.fi<<" "<<k.se<<endl;
    }
        return 0;
    }

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> ve;
typedef vector<long long> vl;
typedef vector<pair<int,int>> vp;
typedef vector<char> vc;
typedef vector<double> vd;
#define fi first
#define se second
#define pb push_back
#define con continue
#define rep(i,a,b) for (int i=a;i<b;i++)
#define res(j,a,b) for (int j=a;j>=b;j--)
const double eps = 1e-6;
#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define endl "\n"
signed main()
{
    fastio;
        int n;
        cin>>n;
        ve v(n);
        stack<int>a,b;
        rep(i,0,n){
        cin>>v[i];
        a.push(n-i);
        }
        res(j,n-1,0){
            while(a.top()!=v[j]){
               cout<<v[j]<<" "<<a.top()<<endl;
               b.push(a.top());
               a.pop();
            }
            a.pop();
            while(!b.empty()){
                a.push(b.top());
                b.pop();
            }
            a.push(v[j]);
        }
        return 0;
    }

Answer: 1


#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;
template<class T> using ordered_set =tree<T, null_type, less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define int long long
#define pb push_back
#define in insert

#define sortva(ans) sort(ans.begin(), ans.end());
#define sortvd(ans) sort(ans.rbegin(), ans.rend());

const int MOD = 1e9+7;

void printVec(vector<int> &ans){
    for(int i=0;i<ans.size();++i) cout<<ans[i]<<" ";
    cout<<"\n";
}

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

    vector<int> v(n+1);
    vector<int> pos(n+1);
    for(int i = 1; i <= n ; i++){
        cin >> v[i];
        pos[i] = i;
    }

    vector<pair<int,int>> ans;
    for(int i = 1; i <= n; i++){
        int idx = -1;
        for(int j = 1; j <= n; j++){
            if(v[i] == pos[j]){
                idx = j;
                break;
            }
        }

        for(int j = idx; j < n; j++){
            ans.pb({pos[j+1], pos[j]});
            swap(pos[j], pos[j+1]);
        }

        for(int j = n; j > idx; j--){
            ans.pb({pos[j], pos[j-1]});
            swap(pos[j], pos[j-1]);
        }

        for(int j = idx; j > i; j--){
            ans.pb({pos[j], pos[j-1]});
            swap(pos[j], pos[j-1]);
        }

    }

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

}

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

#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;
template<class T> using ordered_set =tree<T, null_type, less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define int long long
#define pb push_back
#define in insert

#define sortva(ans) sort(ans.begin(), ans.end());
#define sortvd(ans) sort(ans.rbegin(), ans.rend());

const int MOD = 1e9+7;

void printVec(vector<int> &ans){
    for(int i=0;i<ans.size();++i) cout<<ans[i]<<" ";
    cout<<"\n";
}

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

    vector<int> v(n+1);
    vector<int> pos(n+1);
    for(int i = 1; i <= n ; i++){
        cin >> v[i];
        pos[v[i]] = i;
    }

    vector<int> check(n+1);
    vector<int> posele(n+1);
    for(int i = 1; i <= n ; i++){
        check[i] = i;
        posele[i] = i;
    }

    vector<pair<int,int>> ans;
    for(int i = 1; i <= n; i++){
        if(pos[v[i]] <= check[v[i]]){
            vector<pair<int,int>> temp;
            for(int j = check[v[i]] + 1; j <= n; j++){
                ans.pb({posele[j], v[i]});
                temp.pb({posele[j], v[i]});
            }
            reverse(temp.begin(), temp.end());
            for(auto x : temp){
                ans.pb({x.second, x.first});
            }
            for(int j = check[v[i]] - 1; j >= i; j--){
                ans.pb({v[i], posele[j]});
                check[check[j]] += 1;
            }
            check[v[i]] = i;
            for(int j = 1; j <= n; j++){
                posele[check[j]] = j;
            }
        }
    }

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

}

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

Answer: 1


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vll vector<ll>
#define vpll vector<pair<long long,long long>>
#define fl(n) for (ll i=0; i < n; i++)
#define st(v) sort(v.begin(),v.end())
#define pb push_back

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

    ll t;
 t=1;

    while (t--) {
        ll n;  cin >> n;
       vll v(n);
       fl(n)cin>>v[i];
       vpll ans;
       for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            // ans.pb(make_pair(v[i],v[j]));
            if(v[i]<v[j]){
                ans.pb(make_pair(v[j],v[i]));
            }
            ans.pb(make_pair(v[i],v[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>
using namespace std;

#define ll long long
#define vll vector<ll>
#define vpll vector<pair<ll,ll>>
#define fl(n) for (ll i=0; i < n; i++)
#define st(v) sort(v.begin(),v.end())
#define pb push_back
#define el '\n'
#define mod 998244353

ll mod_inverse(ll a,ll m) {
    ll res=1,p=m - 2;
    while (p > 0) {
        if (p & 1) res=(res * a)%m;
        a=(a * a)%m;
        p >>= 1;
    }
    return res;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    t=1;
    while (t--) {
        int n;cin>>n;vll v(n);fl(n){cin>>v[i];} vpll ans;
        vll x(n);fl(n){x[i]=i+1;}
        for(int i=n-1;i>=0;i--){

            bool found=false;int a;
            for(int j=n-1;j>=0;j--){
                 if(found){
                    // cout<<v[i]<<" "<<x[j]<<endl;
                    ans.pb(make_pair(v[i],x[j]));
                 }
                 if(x[j]==v[i]&& !found){found=true;a=j;}
            }

          x.erase(x.begin()+a);
          x.insert(x.begin(),v[i]);

            }
            cout<<ans.size()<<endl;
            fl(ans.size()){cout<<ans[i].first<<" "<<ans[i].second<<endl;}

        }

    return 0;
}

Answer: 2


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

typedef pair <int, int> ii;

const int Maxn = 1005;

int n;
vector <ii> res;

void Solve(const vector <int> &seq)
{
    if (seq.empty()) return;
    int mn = 0;
    for (int i = 0; i < seq.size(); i++)
        if (seq[i] < seq[mn]) mn = i;
    vector <int> tmp;
    for (int i = 0; i < seq.size(); i++)
        if (i != mn) tmp.push_back(seq[i]);
    Solve(tmp);
    for (int i = 0; i < tmp.size(); i++)
        res.push_back({tmp[i], seq[mn]});
    for (int i = int(seq.size()) - 1; i > mn; i--)
        res.push_back({seq[mn], seq[i]});
}

int main()
{
    scanf("%d", &n);
    vector <int> seq;
    for (int i = 1; i <= n; i++) {
        int num; scanf("%d", &num);
        seq.push_back(num);
    }
    Solve(seq);
    printf("%d\n", int(res.size()));
    for (int i = 0; i < res.size(); i++)
        printf("%d %d\n", res[i].first, res[i].second);
    return 0;
}

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

typedef pair <int, int> ii;

const int Maxn = 1005;

int n;
int c[Maxn];
vector <ii> res;

void Solve(int l, int r)
{
    if (l > r) return;
    int mn = l;
    for (int i = l; i <= r; i++)
        if (c[i] < c[mn]) mn = i;
    for (int i = l; i <= r; i++)
        if (i != mn)
            res.push_back({c[i], c[mn]});
    Solve(l, mn - 1);
    Solve(mn + 1, r);
    for (int i = r; i > mn; i--)
        res.push_back({c[mn], c[i]});
}

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &c[i]);
    Solve(1, n);
    printf("%d\n", int(res.size()));
    for (int i = 0; i < res.size(); i++)
        printf("%d %d\n", res[i].first, res[i].second);
    return 0;
}

Answer: 1


// Problem: A. Adrenaline Rush
// Contest: Codeforces - 2024-2025 ICPC, NERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams Preferred)
// URL: https://codeforces.com/problemset/problem/2052/A
// Memory Limit: 1024 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int, int> pii;
typedef pair<int, long long> pil;
typedef pair<long long, int> pli;

const int M = 1e9+7;
const int N = 2e6 + 10;
int a[N], b[N];
int l[N], r[N];
char g[110][110];
char mapp[110][110];
bool st[110][110];
int n, m;

void solve() {
	cin >> n;
	for(int i = 1;i <= n;i ++) {
		cin >> a[i];
	}

	vector<pii> ans;

	for(int i = n;i >= 1;i --) {
		if(a[i] < i) {
			for(int j = a[i];j < i;j ++) {
				ans.push_back({a[j], a[i]});
			}
		} else {
			if(i == 1) break;
			int idx = -1;
			for(int j = i;j >= 1;j --) {
				if(a[j] < j) {
					idx = j + 1;
					break;
				}
			}
			if(idx == -1) idx = 1;
			vector<pii> tmp;
			int len = i - idx;
			int cnt = 0;
			while(cnt < len) {
				int j = i;
				for(;j > idx + cnt;j --) {
					tmp.push_back({a[j], a[j - 1]});
					ans.push_back({a[j], a[j - 1]});
					swap(a[j], a[j - 1]);

				}
				cnt ++;
			}
			reverse(tmp.begin(), tmp.end());
			for(auto t : tmp) {
				ans.push_back({t.second, t.first});
			}
			i = idx;
		}
	}

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

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

// Problem: A. Adrenaline Rush
// Contest: Codeforces - 2024-2025 ICPC, NERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams Preferred)
// URL: https://codeforces.com/problemset/problem/2052/A
// Memory Limit: 1024 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int, int> pii;
typedef pair<int, long long> pil;
typedef pair<long long, int> pli;

const int M = 1e9+7;
const int N = 2e6 + 10;
int a[N], b[N];
int l[N], r[N];
char g[110][110];
char mapp[110][110];
bool st[110][110];
int n, m;

void solve() {
	cin >> n;
	set<int> S;
	for(int i = 1;i <= n;i ++) {
		cin >> a[i];
		S.insert(i);
	}

	vector<pii> ans;

	for(int i = n;i >= 1;i --) {
		for(int j = a[i] - 1;j >= 1;j --) {
			if(S.count(j) == 0) continue;
			ans.push_back({a[i], j});
		}
		S.erase(a[i]);
		for(auto t : S) {
			ans.push_back({t, a[i]});
		}
	}

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

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

Answer: 2


#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define sig(x) (x<0?-1:1)
#define sz(x) ((int)size(x))
#define all(x) begin(x),end(x)
#define all1(x) begin(x)+1,end(x)
#define cmax(x,y) x=max(x,y)
#define cmin(x,y) x=min(x,y)
template <typename T> void vprint(T st, T nd) {auto it=st;while (next(it)!=nd){cout<<*it<<' ';it=next(it);}cout<<*it<<'\n';}
void yn(bool ans) {puts(ans?"yes":"no");}
template <typename T> T cdiv(T a, T b) {return a/b+(sig(a)/sig(b)==1 && a%b);}
template <typename T> using vec=vector<T>;
template <typename T> struct vec2:vector<vector<T>> {vec2()=default;vec2(int n, int m, T val=T()):vector<vector<T>>(n,vector<T>(m,val)){}};
template <typename T> struct vec3:vector<vector<vector<T>>> {vec3()=default;vec3(int n, int m, int k, T val=T()):vector<vector<vector<T>>>(n,vector<vector<T>>(m,vector<T>(k,val))){}};
template <typename T> void reset(T *arr, int l, int r, T val) {fill(arr+l,arr+r+1,val);}

const int N=1000;
int n;
int c[N+1];

int cind[N+1];

void solve() {
    for (int i=1; i<=n; ++i) {
        cind[c[i]]=i;
    }

    vec<int> a(n+1);
    for (int i=1; i<=n; ++i) {
        a[i]=i;
    }

    vec<pair<int,int>> ans;
    for (int x=1; x<=n; ++x) {
        int i=find(all1(a),x)-begin(a);

        // pass everyone ahead
        for (int j=i-1; j>=1; --j) {
            ans.push_back({a[j+1],a[j]});
            swap(a[j],a[j+1]);
        }

        // let actual ahead go through
        i=1;
        for (int j=2; j<=n; ++j) {
            // a[j] pass x?
            if (cind[a[j]]<cind[a[i]]) {
                for (int k=j-1; k>=i; --k) {
                    ans.push_back({a[k+1],a[k]});
                    swap(a[k],a[k+1]);
                }

                i++;
            }
        }
    }

    printf("%d\n", sz(ans));
    for (auto &[x,y]:ans) {
        printf("%d %d\n", x,y);
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin>>n;
    for (int i=1; i<=n; ++i) cin>>c[i];
    solve();
}

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define sig(x) (x<0?-1:1)
#define sz(x) ((int)size(x))
#define all(x) begin(x),end(x)
#define all1(x) begin(x)+1,end(x)
#define cmax(x,y) x=max(x,y)
#define cmin(x,y) x=min(x,y)
template <typename T> void vprint(T st, T nd) {auto it=st;while (next(it)!=nd){cout<<*it<<' ';it=next(it);}cout<<*it<<'\n';}
void yn(bool ans) {puts(ans?"yes":"no");}
template <typename T> T cdiv(T a, T b) {return a/b+(sig(a)/sig(b)==1 && a%b);}
template <typename T> using vec=vector<T>;
template <typename T> struct vec2:vector<vector<T>> {vec2()=default;vec2(int n, int m, T val=T()):vector<vector<T>>(n,vector<T>(m,val)){}};
template <typename T> struct vec3:vector<vector<vector<T>>> {vec3()=default;vec3(int n, int m, int k, T val=T()):vector<vector<vector<T>>>(n,vector<vector<T>>(m,vector<T>(k,val))){}};
template <typename T> void reset(T *arr, int l, int r, T val) {fill(arr+l,arr+r+1,val);}

const int N=1000;
int n;
int c[N+1];

int cind[N+1];

void solve() {
    for (int i=1; i<=n; ++i) {
        cind[c[i]]=i;
    }

    vec<int> a(n+1);
    for (int i=1; i<=n; ++i) {
        a[i]=i;
    }

    vec<pair<int,int>> ans;
    for (int y=1; y<=n; ++y) {
        int x=c[n-y+1];
        int i=find(all1(a),x)-begin(a);

        // pass everyone ahead
        for (int j=i-1; j>=1; --j) {
            ans.push_back({a[j+1],a[j]});
            swap(a[j],a[j+1]);
        }

        // let actual ahead go through
        i=1;
        for (int j=2; j<=n; ++j) {
            // a[j] pass x?
            if (cind[a[j]]<cind[a[i]]) {
                for (int k=j-1; k>=i; --k) {
                    ans.push_back({a[k+1],a[k]});
                    swap(a[k],a[k+1]);
                }

                i++;
            }
        }
    }

    printf("%d\n", sz(ans));
    for (auto &[x,y]:ans) {
        printf("%d %d\n", x,y);
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin>>n;
    for (int i=1; i<=n; ++i) cin>>c[i];
    solve();
}

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 > 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]});
                    }
                }
            }
        }
        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

*/

#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


import java.util.*;
import java.io.*;

public class A_Adrenaline_Rush {

    int mod = (int) 1e9 + 7;

    public static void main(String[] args) {
        int n = sc.nextInt();
        int[] f = new int[n];
        input(f);
        int[] a = new int[n];
        for(int i=0;i<n;i++) a[i] = i+1;
        List<int[]> l = new ArrayList<>();
        for(int i=1;i<n;i++){
            int j = i;
            while(j>0){
                int temp = a[j];
                a[j] = a[j-1];
                a[j-1] = temp;
                l.add(new int[]{a[j-1],a[j]});
                j--;
            }
        }
        for(int i=0;i<n;i++){
            int pl = f[i];
            // System.out.println(pl);
            int idx = -1;
            for(int j=0;j<n;j++){
                if(a[j]==pl){
                    idx = j;
                    break;
                }
            }
            // System.out.println(idx);
            if(i<idx)    {int j = idx;
                while(j>i){
                    int temp = a[j];
                    a[j] = a[j-1];
                    a[j-1] = temp;
                    l.add(new int[]{a[j-1],a[j]});
                    j--;
                }
                // if(dest>0){
                //     while(j<dest){
                //         int temp = a[j];
                //         a[j] = a[j+1];
                //         a[j+1] = temp;
                //         l.add(new int[]{a[j],a[j+1]});
                //         j++;
                //     }
                // }
            }
            // printArr(a);
        }
        System.out.println(l.size());
        for(int i=0;i<l.size();i++){
            System.out.println(l.get(i)[0] + " " + l.get(i)[1]);
        }
        // printArr(a);
    }

    public static void input(Object array) {
        if (array instanceof int[])
            for (int i = 0; i < ((int[]) array).length; i++)
                ((int[]) array)[i] = sc.nextInt();
        else if (array instanceof long[])
            for (int i = 0; i < ((long[]) array).length; i++)
                ((long[]) array)[i] = sc.nextLong();
    }

    public static void printArr(Object array) {
        if (array instanceof int[])
            for (int x : (int[]) array)
                System.out.print(x + " ");
        else if (array instanceof long[])
            for (long x : (long[]) array)
                System.out.print(x + " ");
        System.out.println();
    }

    public static <T extends Number> long gcd(T a, T b) {
        return b.longValue() == 0 ? a.longValue() : gcd(b, a.longValue() % b.longValue());
    }

    static FastReader sc = new FastReader();

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine().trim();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}

import java.util.*;
import java.io.*;

public class A_Adrenaline_Rush {

    int mod = (int) 1e9 + 7;

    public static void main(String[] args) {
        int n = sc.nextInt();
        int[] f = new int[n];
        input(f);
        int[] a = new int[n];
        for(int i=0;i<n;i++) a[i] = i+1;
        List<int[]> l = new ArrayList<>();
        for(int i=0;i<n;i++){
            int pl = i+1;
            int dest = -1;
            for(int j=0;j<n;j++){
                if(f[j]==pl){
                    dest = j;
                    break;
                }
            }
            // System.out.println(pl);
            int idx = -1;
            for(int j=0;j<n;j++){
                if(a[j]==pl){
                    idx = j;
                    break;
                }
            }
            // System.out.println(idx);
            if(dest<=idx){
                int j = idx;
                while(j>0 && a[j]>a[j-1]){
                    int temp = a[j];
                    a[j] = a[j-1];
                    a[j-1] = temp;
                    l.add(new int[]{a[j-1],a[j]});
                    j--;
                }
                if(dest>0){
                    while(j<dest){
                        int temp = a[j];
                        a[j] = a[j+1];
                        a[j+1] = temp;
                        l.add(new int[]{a[j],a[j+1]});
                        j++;
                    }
                }
            }
            // printArr(a);
        }
        System.out.println(l.size());
        for(int i=0;i<l.size();i++){
            System.out.println(l.get(i)[0] + " " + l.get(i)[1]);
        }
        // printArr(a);
    }

    public static void input(Object array) {
        if (array instanceof int[])
            for (int i = 0; i < ((int[]) array).length; i++)
                ((int[]) array)[i] = sc.nextInt();
        else if (array instanceof long[])
            for (int i = 0; i < ((long[]) array).length; i++)
                ((long[]) array)[i] = sc.nextLong();
    }

    public static void printArr(Object array) {
        if (array instanceof int[])
            for (int x : (int[]) array)
                System.out.print(x + " ");
        else if (array instanceof long[])
            for (long x : (long[]) array)
                System.out.print(x + " ");
        System.out.println();
    }

    public static <T extends Number> long gcd(T a, T b) {
        return b.longValue() == 0 ? a.longValue() : gcd(b, a.longValue() % b.longValue());
    }

    static FastReader sc = new FastReader();

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine().trim();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}

Answer: 1


/*JAI MAHAKAAL*/

#include <bits/stdc++.h>

using namespace std;

#define int            long long int
#define f              first
#define s              second
#define pb             push_back
#define si             set <int>
#define vi             vector <int>
#define pii            pair <int, int>
#define vpi            vector <pii>
#define vpp            vector <pair<int, pii>>
#define mii            map <int, int>
#define mpi            map <pii, int>
#define spi            set <pii>
#define endl           "\n"
#define sz(x)          ((int) x.size())
#define all(p)         p.begin(), p.end()
#define double         long double
#define que_max        priority_queue <int>
#define que_min        priority_queue <int, vi, greater<int>>
#define bug(...)       __f (#__VA_ARGS__, __VA_ARGS__)
#define print(a)       for(auto x : a) cout << x << " "; cout << endl
#define print1(a)      for(auto x : a) cout << x.f << " " << x.s << endl
#define print2(a,x,y)  for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl

inline int power(int a, int b)
{
    int x = 1;
    while (b)
    {
        if (b & 1) x *= a;
        a *= a;
        b >>= 1;
    }
    return x;
}

template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
    const char* comma = strchr (names + 1, ',');
    cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}

const int N = 200005;

void solve() {
    int n;
    cin >> n;
    vi a(n), c(n);
    map<int, int>ind, m;
    for (int i = 0; i < n; i++) {
        cin >> c[i];
        ind[c[i]] = i;
    }
    for (int i = 0; i < n; i++) {
        a[i] = i + 1;
        m[a[i]] = i;
    }
    mpi mp;
    vpi v;
    for (int i = 0; i < n; i++)a[i] = i + 1;
    int i = n - 1;
    int ans = 0;
    while (i >= 0) {
        int j = m[c[i]];
        while (j < i) {
            v.pb({a[j + 1], a[j]});
            swap(a[j], a[j + 1]);
            int mi = min(a[j], a[j + 1]);
            int ma = max(a[j], a[j + 1]);
            mp[ {mi, ma}]++;
            ans++;
            j++;
        }
        for (int i = 0; i < n; i++) {
            m[a[i]] = i;
        }
        i--;
    }
    vpi temp;
    for (int i = n; i >= 1; i--) {
        int j = ind[i];
        while (j >= 0) {
            if (c[j - 1] < i) {
                v.pb({i, c[j - 1]});
                temp.pb({i, c[j - 1]});
                ans += 2;
            }
            j--;
        }
    }
    int size = temp.size();
    for (int j = size - 1; j >= 0; j--) {
        int x = temp[j].f;
        int y = temp[j].s;
        v.pb({y, x});
    }
    cout << ans << endl;
    for (int i = 0; i < v.size(); i++) {
        cout << v[i].f << " " << v[i].s << endl;
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#ifndef ONLINE_JUDGE
    freopen("input.txt",  "r",  stdin);
    freopen("output.txt", "w", stdout);
#endif

    clock_t z = clock();

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

    cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC);

    return 0;
}

/*JAI MAHAKAAL*/

#include <bits/stdc++.h>

using namespace std;

#define int            long long int
#define f              first
#define s              second
#define pb             push_back
#define si             set <int>
#define vi             vector <int>
#define pii            pair <int, int>
#define vpi            vector <pii>
#define vpp            vector <pair<int, pii>>
#define mii            map <int, int>
#define mpi            map <pii, int>
#define spi            set <pii>
#define endl           "\n"
#define sz(x)          ((int) x.size())
#define all(p)         p.begin(), p.end()
#define double         long double
#define que_max        priority_queue <int>
#define que_min        priority_queue <int, vi, greater<int>>
#define bug(...)       __f (#__VA_ARGS__, __VA_ARGS__)
#define print(a)       for(auto x : a) cout << x << " "; cout << endl
#define print1(a)      for(auto x : a) cout << x.f << " " << x.s << endl
#define print2(a,x,y)  for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl

inline int power(int a, int b)
{
    int x = 1;
    while (b)
    {
        if (b & 1) x *= a;
        a *= a;
        b >>= 1;
    }
    return x;
}

template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
    const char* comma = strchr (names + 1, ',');
    cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}

const int N = 200005;

void solve() {
    int n;
    cin >> n;
    vi a(n), c(n);
    map<int, int>ind;
    for (int i = 0; i < n; i++) {
        cin >> c[i];
        ind[c[i]] = i;
    }
    mpi mp;
    vpi v;
    for (int i = 0; i < n; i++)a[i] = i + 1;
    int ans = 0;
    int i = 0;
    while (i < n) {
        if (a[i] == c[i]) {
            i++;
            continue;
        }
        int j = i;
        int id = ind[a[i]];
        while (j < id) {
            int mi = min(a[j], a[j + 1]);
            int ma = max(a[j], a[j + 1]);
            mp[ {mi, ma}]++;
            v.pb({a[j + 1], a[j]});
            swap(a[j], a[j + 1]);
            ans++;
            j++;
        }
    }
    for (int i = n - 1; i >= 0; i--) {
        vpi temp;
        for (int j = i - 1; j >= 0; j--) {
            int mi = min(a[j], a[i]);
            int ma = max(a[j], a[i]);
            if (mp.find({mi, ma}) == mp.end()) {
                mp[ {mi, ma}]++;
                ans += 2;
                temp.pb({a[i], a[j]});
                v.pb({a[i], a[j]});
            }
            else break;
        }
        int size = temp.size();
        for (int j = size - 1; j >= 0; j--) {
            int x = temp[j].f;
            int y = temp[j].s;
            v.pb({y, x});
        }
    }
    cout << ans << endl;
    for (int i = 0; i < v.size(); i++) {
        cout << v[i].f << " " << v[i].s << endl;
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#ifndef ONLINE_JUDGE
    freopen("input.txt",  "r",  stdin);
    freopen("output.txt", "w", stdout);
#endif

    clock_t z = clock();

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

    cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC);

    return 0;
}

Answer: 1


#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fix(x) fixed<<setprecision(x)
#define int long long
#define endl '\n'
using namespace std;
using vei = vector<int>;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
const int N = 1e6 + 10;
const int mod = 1e9 + 7;

void solve()
{
    int n;cin>>n;
    auto a=vei(n+1);
    for(int i=1;i<=n;i++) cin>>a[i];
    vector<pii>f;
    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]);
                f.push_back({a[j],a[j+1]});
            }
        }
    }
    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]);
                f.push_back({a[j],a[j+1]});
            }
        }
    }
    reverse(f.begin(),f.end());
    cout<<f.size()<<'\n';
    for(auto [x,y]:f) cout<<x<<" "<<y<<"\n";

}
signed main()
{
    IOS;

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

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fix(x) fixed<<setprecision(x)
#define int long long
#define endl '\n'
using namespace std;
using vei = vector<int>;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
const int N = 1e6 + 10;
const int mod = 1e9 + 7;

void solve()
{
    int n;cin>>n;
    auto a=vei(n+1);
    for(int i=1;i<=n;i++) cin>>a[i];
    vector<pii>f;
    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]);
                f.push_back({a[j],a[j+1]});
            }
        }
    }
    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]);
                f.push_back({a[j],a[j+1]});
            }
        }
    }
    reverse(f.begin(),f.end());
    cout<<f.size()<<'\n';
    for(auto [x,y]:f) cout<<y<<" "<<x<<"\n";

}
signed main()
{
    IOS;

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

Answer: 2


#include <bits/stdc++.h>
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define sig signed
#define ll long long int
#define rep(i,s,e) for(ll i=s; i<e; i++)
#define repp(n) for(ll i = 0; i < n; i++)
#define repr(i,s,e) for(ll i=s; i>=e; i--)
#define reppr(n) for(ll i = n-1; i >= 0; i--)
#define pb push_back
#define all(x) x.begin(), x.end()
#define mp(x, y) make_pair(x, y)
using namespace std;

// 1 2 3

// 3 1 2

// 2 3 1

// 1 5 3 2 4 6

// 6 4 2 3 5 1

// 1 2 3 4 5 6

// 6 1 2 3 4 5 ->5

// 6 4 1 2 3 5 ->3

// 6 4 2 1 3 5 ->1

// 6 4 2 3 1 5 ->1

// 6 4 2 3 5 1->1

// 2 3 1

// 1 2 3

// 1 3 2

void solve(){
    int n;
    cin>>n;
    vector<int> a(n);

    vector<int> init(n);
    repp(n)
    {
      cin>>a[i];
      init[i]=i+1;
    }
     reverse(a.begin(),a.end());
     vector<pair<int,int>> ans;
    //  cout<<"fe";
    // ll ans=0;
    // ans+=(n*1LL*(n-1))/2;
    // 1 3 2
    repp(n)
    {
      int val=a[i];
      int ind=-1;
      rep(j,i,n)
      {
        if(init[j]==val)
        {
          ind=j;
          break;
        }
      }
      for(int k=ind;k>i;k--)
      {
        ans.push_back({init[k],init[k-1]});
        swap(init[k-1],init[k]);
      }
    }
    for(int i=0;i<n;i++)
    {
      for(int k=n-1;k>i;k--)
      {
        ans.push_back({init[k],init[k-1]});
        swap(init[k-1],init[k]);
      }
    }
    cout<<ans.size()<<"\n";
    for(auto el:ans)
    {
      cout<<el.first<<" "<<el.second<<"\n";
    }
}

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

    ll t = 1;
    // cin >> t;

    while(t--){
        solve();
    }

    return 0;
}

#include <bits/stdc++.h>
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define sig signed
#define ll long long int
#define rep(i,s,e) for(ll i=s; i<e; i++)
#define repp(n) for(ll i = 0; i < n; i++)
#define repr(i,s,e) for(ll i=s; i>=e; i--)
#define reppr(n) for(ll i = n-1; i >= 0; i--)
#define pb push_back
#define all(x) x.begin(), x.end()
#define mp(x, y) make_pair(x, y)
using namespace std;

// 1 2 3

// 3 1 2

// 2 3 1

// 1 5 3 2 4 6

// 6 4 2 3 5 1

// 1 2 3 4 5 6

// 6 1 2 3 4 5 ->5

// 6 4 1 2 3 5 ->3

// 6 4 2 1 3 5 ->1

// 6 4 2 3 1 5 ->1

// 6 4 2 3 5 1->1

// 2 3 1

// 1 2 3

// 1 3 2

void solve(){
    int n;
    cin>>n;
    vector<int> a(n);

    vector<int> init(n);
    repp(n)
    {
      cin>>a[i];
      init[i]=i+1;
    }
     reverse(a.begin(),a.end());
     vector<pair<int,int>> ans;
    //  cout<<"fe";
    // ll ans=0;
    // ans+=(n*1LL*(n-1))/2;
    repp(n)
    {
      int val=a[i];
      int ind=-1;
      rep(j,i,n)
      {
        if(init[j]==val)
        {
          ind=j;
          break;
        }
      }
      for(int k=ind;k>i;k--)
      {
        ans.push_back({init[k-1],init[k]});
        swap(init[k-1],init[k]);
      }

    }
    for(int i=0;i<n;i++)
    {
      for(int k=n-1;k>i;k--)
      {
        ans.push_back({init[k-1],init[k]});
        swap(init[k-1],init[k]);
      }
    }
    cout<<ans.size()<<"\n";
    for(auto el:ans)
    {
      cout<<el.first<<" "<<el.second<<"\n";
    }
}

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

    ll t = 1;
    // cin >> t;

    while(t--){
        solve();
    }

    return 0;
}

Answer: 1


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<unordered_map>
#include<deque>
#define fi first
#define se second
#define pb push_back
#define ppb pop_back
#define int long long
using namespace std;

const int N=1100,mod=1e9+7;
int a[N],b[N];
int s=0,n=0;
pair<int,int> ans[N];

void go(int cur,int begin,int end) {
    for (int i=begin;i<n;++i) {
        ans[++s]={b[i+1],b[i]};
        swap(b[i],b[i+1]);
    }
    for (int i=n;i>end;--i) {
        ans[++s]={b[i],b[i-1]};
        swap(b[i],b[i-1]);
    }
}

signed main() {
    cin>>n;
    for (int i=1;i<=n;++i) cin>>a[i];
    for (int i=1;i<=n;++i) b[i]=i;
    for (int i=1;i<=n;++i) {
        int pos;
        for (int j=1;j<=n;++j) {
            if (a[i]==b[j]) pos=j;
        }
        go(a[i],pos,i);
        //cout<<a[i]<<" "<<pos<<" "<<i<<endl;
    }
    cout<<s<<endl;
    for (int i=1;i<=s;++i) {
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    }
	return 0;
}

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<unordered_map>
#include<deque>
#define fi first
#define se second
#define pb push_back
#define ppb pop_back
#define int long long
using namespace std;

const int N=1000000,mod=1e9+7;
int a[N],b[N];
int s=0,n=0;
pair<int,int> ans[N];

void go(int cur,int begin,int end) {
    for (int i=begin;i<n;++i) {
        ans[++s]={b[i+1],b[i]};
        swap(b[i],b[i+1]);
    }
    for (int i=n;i>end;--i) {
        ans[++s]={b[i],b[i-1]};
        swap(b[i],b[i-1]);
    }
}

signed main() {
    cin>>n;
    for (int i=1;i<=n;++i) cin>>a[i];
    for (int i=1;i<=n;++i) b[i]=i;
    for (int i=1;i<=n;++i) {
        int pos;
        for (int j=1;j<=n;++j) {
            if (a[i]==b[j]) pos=j;
        }
        go(a[i],pos,i);
        //cout<<a[i]<<" "<<pos<<" "<<i<<endl;
    }
    cout<<s<<endl;
    for (int i=1;i<=s;++i) {
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    }
	return 0;
}

Answer: 2


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <set>
#include <limits.h>
using namespace std;

int main(){
    int n;
    cin>>n;
    vector<int> v(n) ,final_(n);

    // n = 4;
    // final_ = {2,3,1,4};
    // v = {4,3,2,1};

    for(int i=0;i<n;i++){
        int a ;
        cin>>a;
        final_[a-1] = i+1;
        v[i] = n-i;
        v[n-i] = i;
    }

    // for(int i=0;i<n;i++)cout<<final_[i]<<' ';
    // cout<<endl;
    // for(int i=0;i<n;i++)cout<<v[i]<<' ';
    // cout<<endl;

    // cout<<"reached"<<endl;
    vector<pair<int,int>> swaps;
    for(int i=n-1;i>=0;i--){
        int j = i-1;
        while(v[i] != final_[i]){
            swap(v[i],v[j]);
            // cout<<i<<' '<<j<<' '<<v[i]<<' '<<final_[i]<<' '<<v[j]<<endl;
            swaps.push_back({j+1,i+1});
            j--;
        }
        // cout<<"reached"<<endl;
        // if(j<-1)cout<<"Error"<<endl;
    }

    int num = n*(n-1)/2 + swaps.size();
    cout<<num<<endl;
    for(int i=0;i<n;i++){
        for(int j=i+1 ;j<n ;j++){
            cout<<j+1<<' '<<i+1<<endl;
        }
    }

    for(pair<int,int> &p:swaps)
        cout<<p.first<<' '<<p.second<<endl;

    // cout<<endl;

    return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <set>
#include <limits.h>
using namespace std;

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

    // n = 4;
    vector<int> v(n);
    vector<int> final_(n);
    vector<int> index_v(n+1);
    vector<int> index_f(n+1);

    for(int i=0;i<n;i++){
        cin>>a;
        final_[a-1] = i+1;
        v[i] = n-i;
        // index[n-i] = i;
    }

    // final_ = {2,3,1,4};
    // final_ = {1,4,2,3};
    // v =      {4,3,2,1};

    for(int i=0;i<n;i++){
        index_v[n-i] = i;
        index_f[final_[i]] = i;
    }

    // cout<<"reached"<<endl;
    vector<pair<int,int>> swaps;
    for(int a = n ;a > 0 ;a--){
        while( v[index_f[a]] != a){
            int b = v[index_f[a]];
            swap(v[index_v[b]],v[index_v[b+1]]);
            swaps.push_back({index_v[b+1]+1,index_v[b]+1});
            swap(index_v[b],index_v[b+1]);
            // cout<<a<<' '<<index_[a]<<' '<<index[a-1]<<endl;
        }
        // cout<<"reached "<<a<<endl;
        // for(int b:v)cout<<b<<' ';
        // cout<<endl;
        // for(int b:index_v)cout<<b<<' ';
        // cout<<endl;
        // if(j<-1)cout<<"Error"<<endl;
    }

    int num = n*(n-1)/2 + swaps.size();
    cout<<num<<endl;
    for(int i=0;i<n;i++){
        for(int j=i+1 ;j<n ;j++){
            cout<<j+1<<' '<<i+1<<endl;
        }
    }

    for(pair<int,int> &p:swaps)
        cout<<p.first<<' '<<p.second<<endl;

    // cout<<endl;

    return 0;
}

Answer: 2


// code for finding maximum number of overtakes
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int n;            // number of cars
    cin >> n;         // taking input of number of cars
    vector<int> a(n); // array of size n to store the positions of the cars
    vector<int> b(n); // array of size n to store the positions of the cars
    vector<int> c;
    for (int i = 0; i < n; i++)
    {
        b[i] = i + 1; // initialising the positions of the cars
    }
    for (int i = 0; i < n; i++) // loop to take input of the positions of the cars
    {
        cin >> a[i];
    }
    int count = 0; // intialising counter
    for(int i=1;i<=n;i++)
    {
        int init_position=find(b.begin(),b.end(),i)-b.begin();
        for(int j=init_position;j<n-1;j++)
        {
            if(i>b[find(b.begin(),b.end(),i)-b.begin()+1] && find(a.begin(),a.end(),i)<find(a.begin(),a.end(),b[find(b.begin(),b.end(),i)-b.begin()+1]))
            {
                break;
            }
            swap(b[j],b[j+1]);
            c.push_back(b[j]);c.push_back(b[j+1]);
            count++;
        }
        while(find(a.begin(),a.end(),i)-a.begin()<find(a.begin(),a.end(),b[find(b.begin(),b.end(),i)-b.begin()-1])-a.begin() && find(b.begin(),b.end(),i)-b.begin()!=0)
        {
            int j=find(b.begin(),b.end(),i)-b.begin()-1;
            int k=find(b.begin(),b.end(),i)-b.begin();
            swap(b[k],b[j]);
            c.push_back(b[j]);c.push_back(b[k]);
            count ++;
        }
    }
    cout<<count<<endl;
    for(int i=0;i<c.size();i+=2)
    {
        cout<<c[i]<<" "<<c[i+1]<<endl;
    }
    return 0;
}

// code for finding maximum number of overtakes
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int n;            // number of cars
    cin >> n;         // taking input of number of cars
    vector<int> a(n); // array of size n to store the positions of the cars
    vector<int> b(n); // array of size n to store the positions of the cars
    vector<int> c;
    for (int i = 0; i < n; i++)
    {
        b[i] = i + 1; // initialising the positions of the cars
    }
    for (int i = 0; i < n; i++) // loop to take input of the positions of the cars
    {
        cin >> a[i];
    }
    int count = 0; // intialising counter
    for(int i=1;i<=n;i++)
    {
        int init_position=find(b.begin(),b.end(),i)-b.begin();
        for(int j=init_position;j<n-1;j++)
        {
            if(i>b[find(b.begin(),b.end(),i)-b.begin()+1] && find(a.begin(),a.end(),i)<find(a.begin(),a.end(),b[find(b.begin(),b.end(),i)-b.begin()+1]))
            {
                break;
            }
            swap(b[j],b[j+1]);
            c.push_back(b[j]);c.push_back(b[j+1]);
            count++;
        }
    }
    cout<<count<<endl;
    for(int i=0;i<c.size();i+=2)
    {
        cout<<c[i]<<" "<<c[i+1]<<endl;
    }
    return 0;
}

Answer: 2


#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;//inflation at back
    unordered_set<int> bks;
    for(int i=n-1;i>0;i--){
        if(bks.find(i+1)!=bks.end())back--;
        int l=a[i]+i-1-back;//going all the way left&back to pos
        an+=l;
        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++;
            }
        }
        if(a[i]<i+1)back++;
        bks.emplace(a[i]);
    }
    cout << an;
    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;
    }
    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;
}

Answer: 2


#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;//inflation at back
    unordered_set<int> bks;
    for(int i=n-1;i>0;i--){
        if(bks.find(i+1)!=bks.end())back--;
        int l=a[i]+i-1-back;//going all the way left&back to pos
        an+=l;
        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<=i+1){
            if(bks.find(cur)==bks.end()&&cur!=a[i]){
                ans.push_back(make_pair(cur,a[i]));
                cur++;
            }else{
                cur++;
            }
        }
        if(a[i]<i+1)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 <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
typedef long double ld;
using namespace std;
using namespace __gnu_pbds;
using ordered_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;
#define all(x) x.begin(), x.end()
#define pll pair<ll, ll>
#define vec vector
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
const ll mod = 998244353;
ll fpow(ll a, ll b, ll m = -1)
{
	if (m != -1)
		a %= m;
	ll res = 1;
	while (b > 0)
	{
		if (b % 2 == 1)
		{
			if (m != -1)
				res = res * a % m;
			else
			{
				res *= a;
			}
		}
		a = a * a;
		if (m != -1)
		{
			a %= m;
		}
		b /= 2;
	}
	return res;
}
int main()
{
	fast;
	// freopen("pails.in", "r", stdin);
	// freopen("pails.out", "w", stdout);
	ll t = 1;
	// cin >> t;
end:;
	while (t--)
	{
		ll n, k;
		cin >> n;
		vec<ll> v(n);
		for (int i = 0; i < n; i++)
		{
			cin >> v[i];
		}
		set<ll> st;
		ll mx = 0;
		vec<pll> ans;
		for (int i = 0; i < n; i++)
		{
			for (int j = 1; j < v[i]; j++)
			{
				ans.push_back({v[i], j});
				if(st.count(j))
				{
					ans.push_back({j, v[i]});
				}
			}
			st.insert(v[i]);
		}
		cout << ans.size() << '\n';
		for (int i = 0; i < ans.size(); i++)
		{
			cout << ans[i].first << ' ' << ans[i].second << '\n';
		}

	}
	return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
typedef long double ld;
using namespace std;
using namespace __gnu_pbds;
using ordered_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;
#define all(x) x.begin(), x.end()
#define pll pair<ll, ll>
#define vec vector
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
const ll mod = 998244353;
ll fpow(ll a, ll b, ll m = -1)
{
	if (m != -1)
		a %= m;
	ll res = 1;
	while (b > 0)
	{
		if (b % 2 == 1)
		{
			if (m != -1)
				res = res * a % m;
			else
			{
				res *= a;
			}
		}
		a = a * a;
		if (m != -1)
		{
			a %= m;
		}
		b /= 2;
	}
	return res;
}
int main()
{
	fast;
	// freopen("pails.in", "r", stdin);
	// freopen("pails.out", "w", stdout);
	ll t = 1;
	// cin >> t;
end:;
	while (t--)
	{
		ll n, k;
		cin >> n;
		vec<ll> v(n);
		for (int i = 0; i < n; i++)
		{
			cin >> v[i];
		}
		set<ll> st;
		ll mx = 0;
		vec<pll> ans;
		for (int i = 0; i < n; i++)
		{
			for(int j = i - 1; j >= 0; j--)
			{
				swap(v[j+1], v[j]);
				ans.push_back({v[j+1], v[j]});
			}
			ll in = 0;
			while(in < i && v[in]> v[in+1])
			{
				swap(v[in], v[in+1]);
				ans.push_back({v[in+1], v[in]});
				in++;
			}
		}
		reverse(all(ans));
		cout << ans.size() << '\n';
		for (int i = 0; i < ans.size(); i++)
		{
			cout << ans[i].first << ' ' << ans[i].second << '\n';
		}

	}
	return 0;
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define popcount __builtin_popcount
#define fal (fa<<1)
#define far (fa<<1|1)
#define endl '\n'
int const N=2e3+5,M=2e3+5,inf=0x7fffffffffffffff,mod=998244353;
int n,m,a[N],b[N],c[N],d[N],cnt=0;
string s[5];
bool vis=0;
void solve(){
	int i,j,k,h,t,x=0,y=0,z,ans=0,minn=inf,maxn=-inf;
	cin>>n;
	for(i=1;i<=n;i++){
		c[i]=i;
	}
	for(i=1;i<=n;i++){
		cin>>a[i];
		b[i]=a[i];
	}
	for(i=n;i>=1;i--){
		int now=lower_bound(c+1,c+1+i,a[i])-c;
		for(j=now;j>1;j--){
			ans++;
			swap(c[j],c[j-1]);
		}
		for(j=1;j<=now-1;j++){
			ans++;
			swap(c[j],c[j+1]);
		}
		for(j=now;j<=i-1;j++){
			ans++;
			swap(c[j],c[j+1]);
		}
	}
	for(i=1;i<=n;i++){
		c[i]=i;
	}
	cout<<ans<<endl;
	for(i=n;i>=1;i--){
		int now=lower_bound(c+1,c+1+i,a[i])-c;
		for(j=now;j>1;j--){
			cout<<c[j]<<" "<<c[j-1]<<endl;
			swap(c[j],c[j-1]);
		}
		for(j=1;j<=now-1;j++){
			cout<<c[j+1]<<" "<<c[j]<<endl;
			swap(c[j],c[j+1]);
		}
		for(j=now;j<=i-1;j++){
			cout<<c[j+1]<<" "<<c[j]<<endl;
			swap(c[j],c[j+1]);
		}
	}
	return;
}
signed main(void){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int t;
//	cin>>t;
	t=1;
	while(t--){
		solve();
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
using i64 = long long;
#define print(...) std::cerr << format(__VA_ARGS__) << std::endl
void solve()
{
    int n;
    std::cin >> n;
    std::vector<int> c(n + 1), a(n + 1);
    for (int i = 1; i <= n; i++)
    {
        a[i] = i;
        std::cin >> c[i];
    }
    std::vector<pair<int,int>> ttt;
    for(int i = n;i>=1;i--){
        int t = c[i];
        for(int j = t+1;j<=i;j++){
            ttt.push_back(pair<int,int>(a[j],a[j-1]));
            // std::cout << a[j] << " " << a[j-1] << "\n";
            swap(a[j],a[j-1]);
        }
    }
    std::cout << ttt.size() << "\n";
    for(int i = 0;i<ttt.size();i++) std::cout << ttt[i].first << " " << ttt[i].second << "\n";

}

signed main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr), std::cout.tie(nullptr);
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);

    solve();
    return 0;
}
/* stuff you should look for
 * int overflow, array bounds
 * special cases (n=1?)
 * do smth instead of nothing and stay organized
 * WRITE STUFF DOWN
 * DON'T GET STUCK ON ONE APPROACH
 */

Answer: 2


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

void solve(){
    int n; cin >> n;
    vector<int> vt(n);
    for (int &i : vt) cin >> i;
    set<pair<int, int>> ans;
    vector<pair<int, int>> vtans;
    // 1, 2, 3 -> 2, 1, 3
    vector<int> form(n);
    for (int i = 0; i < n; i++)
        form[i] = i + 1;
    for (int i = 0; i < n; i++){
        int idx = i;
        for (; idx < n; idx++)
            if (form[idx] == vt[i])
                break;
        if (idx + 1 < n){
            if (!ans.count({form[idx + 1], form[idx]})){
                vtans.push_back({form[idx + 1], form[idx]});
                ans.insert({form[idx + 1], form[idx]});
            }
            if (!ans.count({form[idx], form[idx + 1]})){
                vtans.push_back({form[idx], form[idx + 1]});
                ans.insert({form[idx], form[idx + 1]});
            }

        }
        for (int j = idx; j > i; j--){
            if (!ans.count({form[j], form[j - 1]})){
                vtans.push_back({form[j], form[j - 1]});
                ans.insert({form[j], form[j - 1]});
            }
            swap(form[j], form[j - 1]);
        }
    }
    cout << ans.size() << '\n';
    for (auto &p : vtans)
        cout << p.first << " " << p.second << '\n';
}

int 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;

void solve(){
    int n; cin >> n;
    vector<int> vt(n);
    for (int &i : vt) cin >> i;
    vector<pair<int, int>> ans;
    for (int i = 1; i < n; i++)
        for (int j = i + 1; j <= n; j++)
            ans.push_back({j, i});
    vector<bool> b(n + 1, false);
    for (int i = n - 1; ~i; i--){
        for (int j = vt[i] - 1; j >= 1; j--){
            if (!b[j])
                ans.push_back({j, vt[i]});
        }
        b[vt[i]] = true;
    }
    cout << ans.size() << '\n';
    for (auto &[x, y] : ans)
        cout << x << ' ' << y << '\n';
}

int 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;
using ll=long long;
using vll=vector<ll>;
using pii=pair<ll,ll>;
using pll=pair<ll,ll>;
#define all(v) (v).begin(),(v).end()
#define sz(v) (ll)(v).size()
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define f(i,a,b) for(ll i=(a);i<(b);i++)
#define fr(i,a,b) for(ll i=(a);i>=(b);i--)
void solve(){
  ll n;cin>>n;
  vll a(n);
  f(i,0,n){cin>>a[i];}
  vector<pair<ll,ll>>ans;
  vll x(n+1,0);
  fr(i,n-1,0)
  {
    ll val=a[i];
    x[a[i]]=1;
    fr(j,val-1,1)
    {
      if(x[j]==0){ans.pb({val,j});}
    }
   f(i,1,n+1)
   {
     if(x[i]!=1)
     {
       ans.pb({i,val});
     }
   }

  }
  cout<<ans.size()<<endl;
  f(i,0,ans.size())
  {
    cout<<ans[i].F<<" "<<ans[i].S<<endl;
  }
  cout<<endl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;t=1;
    while(t--)solve();
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using vll=vector<ll>;
using pii=pair<ll,ll>;
using pll=pair<ll,ll>;
#define all(v) (v).begin(),(v).end()
#define sz(v) (ll)(v).size()
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define f(i,a,b) for(ll i=(a);i<(b);i++)
#define fr(i,a,b) for(ll i=(a);i>=(b);i--)

ll ck(ll x,ll y,ll z){
    return (x-y)*(x-y) + (y-z)*(y-z) + (x-z)*(x-z);
}

void solve(){
    ll n;cin>>n;
    vll a(n);
    vll hsh(n+1,0);
    f(i,0,n){cin>>a[i];
      hsh[a[i]]=(i+1);
    }
    map<pair<ll,ll>,ll>m;
    f(i,1,n+1)
    {
      ll pos=hsh[i];
      f(j,0,pos)
      {
        if(a[j]>i){m[{a[j],i}]++;}
      }
    }
    // auto it=m.begin();
    // cout<<"sz"<<m.size()<<endl;
    // while(it!=m.end())
    // {
    //   pair<ll,ll>p=it->F;
    //   cout<<p.F<<" "<<p.S<<endl;it++;
    // }
    vector<pair<ll,ll>>ans;
    f(i,1,n+1)
    {
      f(j,1,n+1)
      {
        if(i!=j)
        {
          pair<ll,ll>now={i,j};
          if(m[{i,j}]==0){ans.pb({i,j});}
        }
      }
    }
    cout<<ans.size()<<endl;
    f(i,0,ans.size())
    {
      cout<<ans[i].S<<" "<<ans[i].F<<endl;
    }

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;t=1;
    while(t--)solve();
    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]){
         ans.push_back({a[i],b[i]});
         v[a[i]-1][b[i]-1]=1;
         v[b[i]-1][a[i]-1]=1;
         b[a[i]-1]=b[i];
         b[i]=a[i];
      }
    }
   for(int i=0;i<z-1;i++){
         if(v[b[i]-1][b[i+1]-1]==0){
            ans.push_back({b[i+1],b[i]});
            ans.push_back({b[i],b[i+1]});
         }
   }
   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


import io,os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def main():
    n = int(input())
    arr = list(map(int,input().split()))

    ans = []
    for i in range(n-1):
        for j in range(i+1,n):
            if arr[i] < arr[j]:
                ans.append((arr[j],arr[i]))
                ans.append((arr[i],arr[j]))
            else:
                ans.append((arr[i],arr[j]))

    print(len(ans))
    for ele in ans:
        print(*ele)

main()

import io,os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def main():
    n = int(input())
    arr = list(map(int,input().split()))
    for i in range(n):
        arr[i] -= 1

    curr = [i for i in range(n)]

    ans = []

    for i in range(n):
        index = -1
        for j in range(n):
            if curr[j]==arr[i]:
                index = j
                break

        for j in range(index,n-1):
            curr[j],curr[j+1] = curr[j+1],curr[j]
            ans.append((curr[j]+1,curr[j+1]+1))
        for j in range(n-2,i-1,-1):
            curr[j],curr[j+1] = curr[j+1],curr[j]
            ans.append((curr[j]+1,curr[j+1]+1))

    print(len(ans))
    for ele in ans:
        print(*ele)

main()

Answer: 2


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

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long int ull;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge {c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// ll my_rand(ll l, ll r) {
//     return uniform_int_distribution<ll>(l, r) (rng);
// }

void tcase(int Tcase)
{
	int n;
    scanf("%d",&n);
    int a[n+5], pos[n+5], b[n+5], pos2[n+5];
    for(int i = 1; i <= n; i++){
        scanf("%d",&a[i]);
        pos[a[i]] = i;
        b[i] = n+1-i;
        pos2[b[i]] = i;
    }
    vector<pair<int,int>>v;
    for(int i = 1; i <= n; i++){
        for(int j = i+1; j <= n; j++){
            v.push_back({j, i});
        }
    }
    int curr = 1;
    while(curr < n){
        if(curr == pos[b[curr]]){
            curr++;
            continue;
        }
        int diff = pos2[a[curr]]-curr;
        int temp = b[curr+diff];
        for(int i = diff; i > 0; i--){
            v.push_back({b[curr+i], b[curr+i-1]});
            swap(b[curr+i], b[curr+i-1]);
        }
        for(int i = 1; i <= n; i++) pos2[b[i]] = i;
    }
    int siz = v.size();
    printf("%d\n",siz);
    for(int i = 0; i < siz; i++){
        printf("%d %d\n",v[i].first, v[i].second);
    }
}

int main(){
	int T = 1;
  	//scanf("%d",&T);
  	for(int Tcase = 1; Tcase <= T; Tcase++){
		  tcase(Tcase);
  	}
}

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

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long int ull;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge {c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// ll my_rand(ll l, ll r) {
//     return uniform_int_distribution<ll>(l, r) (rng);
// }

void tcase(int Tcase)
{
	int n;
    scanf("%d",&n);
    int a[n+5], pos[n+5], b[n+5];
    for(int i = 1; i <= n; i++){
        scanf("%d",&a[i]);
        pos[a[i]] = i;
        b[i] = n+1-i;
    }
    vector<pair<int,int>>v;
    for(int i = 1; i <= n; i++){
        for(int j = i+1; j <= n; j++){
            v.push_back({j, i});
        }
    }
    int curr = 1;
    while(curr < n){
        if(curr == pos[b[curr]]){
            curr++;
            continue;
        }
        int temp = b[curr];
        int diff = pos[temp]-curr;
        for(int i = 1; i <= diff; i++){
            v.push_back({b[curr+i], temp});
            b[curr+i-1] = b[curr+i];
        }
        b[curr+diff] = temp;
    }
    int siz = v.size();
    printf("%d\n",siz);
    for(int i = 0; i < siz; i++){
        printf("%d %d\n",v[i].first, v[i].second);
    }
}

int main(){
	int T = 1;
  	//scanf("%d",&T);
  	for(int Tcase = 1; Tcase <= T; Tcase++){
		  tcase(Tcase);
  	}
}

Answer: 1


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

#define int long long
#define nl cout<<endl
#define vec vector<int>
#define vecs vector<string>
#define ddv vector<vec>
#define ub upper_bound
#define lb lower_bound
#define prnt(v) for(auto i:v) cout<<i<<" "; nl
#define inp(v); for(auto &i:v) cin>>i;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define pinp(a) for(int i=1;i<=n;++i) { int p; cin>>p; a[i] = p; }

const int N = 2e5;
const int M = 1e9+7;

int factorials[N+1];
int factInv[N+1];
bool isPrime[N+1];

int gcd(int a,int b){ //O(logb)
    if(!b) return a;
    return gcd(b,a%b);
}

int lcm(int a,int b){ //O(logb)
    return (a*b/gcd(a,b)) % M;
}

int binExp(long long a, int b) { //O(logb)
    a %= M;
    int ans = 1;
    while (b) {
        if (b & 1) ans = (ans * 1LL * a) % M;
        a = (a * 1LL * a) % M;
        b >>= 1;
    }
    return ans;
}

int inv(int n){ //O(logM)
    return binExp(n,M-2);
}

void FACT(){ //O(N)
    factorials[0] = 1;
    for (int i = 1; i <= N; i++) {
        factorials[i] = factorials[i - 1] * 1LL * i % M;
    }
}

void FACTINV(){ //O(N)
    for (int i = 0; i <= N; i++) {
        factInv[i] = inv(factorials[i]);
    }
}

void SIEVE(){ //O(NloglogN)

    //Pre-compute

    fill(isPrime, isPrime+N, true);
    isPrime[0]=isPrime[1]=false;

    for(int i = 2; i <= N; ++i){
        if(isPrime[i]){
            for(int j=2*i ; j<=N ; j+=i)
                isPrime[j]=false;
        }
    }
}

long long nCr(int n, int r){ //O(1) excl. Pre-Comp which is O(N).

    // Pre-Compute before using this func.
    // FACT();
    // FACTINV();

    if (r > n) return 0;
    return (factorials[n] * 1LL * factInv[r] % M * factInv[n - r] % M) % M;
}

void solve(){
    int n;
    cin>>n;
    vec a(n+1);
    pinp(a);

    vec vis(n+1,0);
    ddv ans;

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

    for(int i=1;i<n;++i){
        if(a[i] < a[i+1]){
            ans.pb({a[i+1],a[i]});
            ans.pb({a[i],a[i+1]});
        }
    }

    cout<<ans.size()<<endl;
    for(auto i:ans){
        cout<<i[0]<<" "<<i[1]<<endl;
    }
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);

    // FACT();
    // FACTINV();
    // SIEVE();

    int t = 1;
    // cin>>t;

    while(t--){
        solve();
    }

    return 0;
}

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

#define int long long
#define nl cout<<endl
#define vec vector<int>
#define vecs vector<string>
#define ddv vector<vec>
#define ub upper_bound
#define lb lower_bound
#define prnt(v) for(auto i:v) cout<<i<<" "; nl
#define inp(v); for(auto &i:v) cin>>i;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define pinp(a) for(int i=1;i<=n;++i) { int p; cin>>p; a[i] = p; }

const int N = 2e5;
const int M = 1e9+7;

int factorials[N+1];
int factInv[N+1];
bool isPrime[N+1];

int gcd(int a,int b){ //O(logb)
    if(!b) return a;
    return gcd(b,a%b);
}

int lcm(int a,int b){ //O(logb)
    return (a*b/gcd(a,b)) % M;
}

int binExp(long long a, int b) { //O(logb)
    a %= M;
    int ans = 1;
    while (b) {
        if (b & 1) ans = (ans * 1LL * a) % M;
        a = (a * 1LL * a) % M;
        b >>= 1;
    }
    return ans;
}

int inv(int n){ //O(logM)
    return binExp(n,M-2);
}

void FACT(){ //O(N)
    factorials[0] = 1;
    for (int i = 1; i <= N; i++) {
        factorials[i] = factorials[i - 1] * 1LL * i % M;
    }
}

void FACTINV(){ //O(N)
    for (int i = 0; i <= N; i++) {
        factInv[i] = inv(factorials[i]);
    }
}

void SIEVE(){ //O(NloglogN)

    //Pre-compute

    fill(isPrime, isPrime+N, true);
    isPrime[0]=isPrime[1]=false;

    for(int i = 2; i <= N; ++i){
        if(isPrime[i]){
            for(int j=2*i ; j<=N ; j+=i)
                isPrime[j]=false;
        }
    }
}

long long nCr(int n, int r){ //O(1) excl. Pre-Comp which is O(N).

    // Pre-Compute before using this func.
    // FACT();
    // FACTINV();

    if (r > n) return 0;
    return (factorials[n] * 1LL * factInv[r] % M * factInv[n - r] % M) % M;
}

void solve(){
    int n;
    cin>>n;
    vec a(n+1);
    pinp(a);

    vec vis(n+1,0);
    ddv ans;

    for(int i=n;i>0;--i){
        vis[a[i]] = 1;

        for(int j=a[i]-1;j>=1;--j){
            if(!vis[j])
                ans.pb({a[i],j});
        }
        for(int j=1;j<a[i];++j){
            if(!vis[j])
                ans.pb({j,a[i]});
        }

        for(int j=a[i]+1;j<=n;++j){
            if(!vis[j]){
                ans.pb({j,a[i]});
            }
        }
    }

    // int l=1, r=1;
    // while(r<=n){
    //     while(r<n && a[r+1]>a[r]){
    //         ++r;
    //     }

    //     for(int i=r;i>l;--i){
    //         for(int j=i-1;j>=l;--j){
    //             ans.pb({a[i],a[j]});
    //         }
    //         for(int j=l;j<i;++j){
    //             ans.pb({a[j],a[i]});
    //         }
    //     }

    //     ++r;
    //     l=r;
    // }

    cout<<ans.size()<<endl;
    for(auto i:ans){
        cout<<i[0]<<" "<<i[1]<<endl;
    }
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);

    // FACT();
    // FACTINV();
    // SIEVE();

    int t = 1;
    // cin>>t;

    while(t--){
        solve();
    }

    return 0;
}

Answer: 2


#include<iostream>
#include<algorithm>
using namespace std;
const int N=1010;
int a[N];
int st[N];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	int cut=0;
	for(int i=1;i<=n;i++){
		for(int j=i+1;j<=n;j++){
			if(a[i]>a[j]){
				cut++;
			}
		}
	}
	int ans=n*(n-1)-cut;
	printf("%d\n",ans);
	reverse(a+1,a+n+1);
	for(int i=1;i<=n;i++){
		int p=a[i];
		for(int j=p-1;j>0;j--){
			if(st[j]) continue;
			printf("%d %d\n",p,j);
		}
		for(int j=n;j>=i+1;j--){
			if(st[a[j]]) continue;
			printf("%d %d\n",a[j],p);
		}
		st[p]=1;
	}
	return 0;
}

#include<iostream>
#include<algorithm>
using namespace std;
const int N=1010;
int a[N];
int b[N];
int st[N];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		cin>>a[i];
		b[i]=i;
	}
	int cut=0;
	for(int i=1;i<=n;i++){
		for(int j=i+1;j<=n;j++){
			if(a[i]>a[j]){
				cut++;
			}
		}
	}
	int ans=n*(n-1)-cut;
	printf("%d\n",ans);
	reverse(a+1,a+n+1);
	for(int i=1;i<=n;i++){
		int p=a[i];
		for(int j=p-1;j>0;j--){
			if(st[j]) continue;
			printf("%d %d\n",p,j);
		}
		st[p]=1;
		for(int j=1;j<=n;j++){
			if(st[j]) continue;
			printf("%d %d\n",j,p);
		}

	}
	return 0;
}

Answer: 2


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

typedef long long ll;
typedef pair<ll, ll> pll;

#define len(a) (int) a.size()

signed main() {
    ios_base::sync_with_stdio(false);
    ll n; cin >> n;
    vector<ll> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    if (n == 1) {
        return cout << 0, 0;
    }

    vector<pll> ans;

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

    cout << len(ans) << '\n';
    for (auto [l, r]: ans) {
        cout << l << ' ' << r << '\n';
    }

    return 0;

}

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

typedef long long ll;
typedef pair<ll, ll> pll;

#define len(a) (int) a.size()

signed main() {
    ios_base::sync_with_stdio(false);
    ll n; cin >> n;
    vector<ll> a(n), b(n+1);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        b[a[i]] = i;
    }
    if (n == 1) {
        return cout << 0, 0;
    }

    vector<pll> ans1, ans2, ans3;

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

    cout << (len(ans1) + (n * (n - 1)) / 2) << '\n';

    for (int i = 1; i <= n; i++) {
        for (int j = i + 1; j <= n; j++) {
            cout << j << ' ' << i << '\n';
        }
    }

    // vector<ll> b(n);
    // iota(b.begin(), b.end(), 1);
    // reverse(b.begin(), b.end());

    // for (int i = 0; i < n; i++) {
    //     for (int j = i - 1; j >= 0; j--) {
    //         ans1.push_back({a[i], a[j]});
    //         swap
    //     }
    // }

    sort(ans1.begin(), ans1.end(), [&](pll i, pll j) {
        if (b[i.first] == b[j.first])
            return i.second < j.second;
        return b[i.first] < b[j.first];

    });
    for (auto [l, r]: ans1) {
        cout << l << ' ' << r << '\n';
    }

    return 0;

}

Answer: 2


#include<iostream>
#include <map>
#include<set>
#include <vector>
#define mod 998244353
#include <algorithm>
#include <limits>
using namespace std;

int main() {
   int n;
   cin>>n;
   vector<int> v(n);
   for(int i=0;i<n;i++) {
      cin>>v[i];
   }
  vector<int> v2(n);
    for(int i =0;i<n;i++) {
        v2[i] =i+1;
    }
    vector<pair<int,int>> ans(n*n);
    int ans2=0;
    for(int i =0;i<n;i++) {
        for(int j =0;j<n-i-1;j++) {
            ans[ans2] = {v2[j+1],v2[j]};
            ans2++;
            int temp = v2[j+1];
            v2[j+1] = v2[j];
            v2[j] = temp;
        }
    }
    for(int i =0;i<n;i++) {
        int element = v[i];
        int k=0;
        for(int j =i;j<n;j++) {
            if(v2[j] == element) {
                k = j;
            }
        }
        for(int j =k;j>i;j--) {
            ans[ans2] = {v2[j],v2[j-1]};
            ans2++;
            int temp = v2[j-1];
            v2[j-1] = v2[j];
            v2[j] = temp;
        }
    }
    cout<<ans2<<endl;
    for(auto i:ans) {
        if(i.first ==i.second&& i.second==0) {
            break;
        }else {
            cout<<i.first<<" "<<i.second<<endl;
        }
    }
}

#include<iostream>
#include <map>
#include<set>
#include <vector>
#define mod 998244353
#include <algorithm>
#include <limits>
using namespace std;

int main() {
   int n;
   cin>>n;
   vector<int> v(n);
   for(int i=0;i<n;i++) {
      cin>>v[i];
   }
  vector<int> v2(n);
    for(int i =0;i<n;i++) {
        v2[i] =i+1;
    }
    vector<pair<int,int>> ans(n*n);
    int ans2=0;
    for(int i =0;i<n;i++) {
        for(int j =0;j<n-i-1;j++) {
            ans[ans2] = {v2[j+1],v2[j]};
            ans2++;
            int temp = v2[j+1];
            v2[j+1] = v2[j];
            v2[j] = temp;
        }
    }
    for(int i =0;i<n;i++) {
        int element = v[i];
        for(int j =n-element;j>i;j--) {
            ans[ans2] = {v2[j],v2[j-1]};
            ans2++;
            int temp = v2[j-1];
            v2[j-1] = v2[j];
            v2[j] = temp;
        }
    }
    cout<<ans2<<endl;
    for(auto i:ans) {
        if(i.first ==i.second&& i.second==0) {
            break;
        }else {
            cout<<i.first<<" "<<i.second<<endl;
        }
    }
}

Answer: 1


/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
using namespace std;
#include <bits/stdc++.h>
#define Code ios_base::sync_with_stdio(false);
#define By cin.tie(NULL);

int main()
{
    int T;
    cin >> T;
    while (T--) {
        int n, k;
        cin>>n>>k;
        vector<int>a(n);
        for(int i =0  ; i < n ; i++){
            cin>>a[i];
        }
        unordered_map<int , int>mpp;
        for(int i =0 ; i < n ; i++){
            mpp[a[i]]++;
        }
        vector<int>freq;
        for(auto p : mpp){
            freq.push_back(p.second);
        }
        sort(freq.begin(), freq.end());
        int min_req = 0 ;
        int cnt = k;
        int res = 0;
        int m = freq.size();
        for(int i = 0 ; i < m ; i++){
            if(cnt>=freq[i]){
                cnt-=freq[i];
            }
            else{
                res++;
            }
        }
        if(res == 0 ){
            res =1 ;
        }
        cout<<res<<endl;
    }

    return 0;
}

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define Code ios_base::sync_with_stdio(false);
#define By cin.tie(NULL);

int main()
{
    Code By
    int T;
    cin >> T;
    while (T--) {
        int n, k;
        cin >> n >> k;
        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }

        sort(a.begin(), a.end());

        vector<int> freq;
        int cnt = 1;
        for (int i = 1; i < n; i++) {
            if (a[i - 1] == a[i]) {
                cnt++;
            } else {
                freq.push_back(cnt);
                cnt = 1;
            }
        }
        freq.push_back(cnt);

        sort(freq.begin(), freq.end());

        int res = 0;
        for (int f : freq) {
            if (k >= f) {
                k -= f;
            } else {
                res++;
            }
        }
        if(res == 0 ){
            res =1 ;
        }
        cout << res << endl;
    }

    return 0;
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        vector<int> a(n);
        //set<int> s;

        map<int,int> m;
        vector<int> freq;
        for(int i=0;i<n;i++){
            cin>>a[i];
            m[a[i]]++;

            //s.insert(a[i]);
        }
        for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
            freq.push_back(it->second);
        }
        sort(freq.begin(),freq.end());

        int ans=m.size();
        for(int i=0;i<freq.size();i++){
            if(k>=freq[i]){
                k=k-freq[i];
                ans--;
            }
            else {
                break;
            }
        }
        if(ans==0) cout<<"1"<<endl;
       else  cout<<ans<<endl;

    }
}

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

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

    int t;
    cin >> t;

    while (t--) {
        int n, k;
        cin >> n >> k;

        unordered_map<int, int> m;
        for (int i = 0; i < n; i++) {
            int num;
            cin >> num;
            m[num]++;
        }

        priority_queue<int, vector<int>, greater<int>> minHeap;
        for (auto& p : m) {
            minHeap.push(p.second);
        }

        int distinct = m.size();

        while (!minHeap.empty() && k >= minHeap.top()) {
            k -= minHeap.top();
            minHeap.pop();
            distinct--;
        }

        cout << max(1, distinct) <<endl;
    }

}

Answer: 1