#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) v.begin(), v.end()
#define LL_MAX LLONG_MAX
#define LL_MIN LLONG_MIN
#define pb push_back

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

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

if(n == 1){
	cout << 0 << '\n';
	return;
}
	vector<int>v(n);

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

	reverse(all(v));
	vector<int>store1;
	vector<int>store2;
int count = 0;
	for(int i = 1; i <= n; i++){
		for(int j = i + 1; j <= n; j++){
			count++;
			store1.push_back(j);
			store2.pb(i);

		}
	}

	for(int i = 0; i < n; i++){
		int j = i;
		while(v[j] != final[i]){
			j++;
		}

		for(int k = j; k > i; k--){
			count++;
			store1.pb(v[k]);
			store2.pb(v[k - 1]);
			swap(v[k],v[k - 1]);

		}

	}
cout << count << '\n';
	for(int i = 0; i < store1.size(); i++){
		cout << store1[i] << " " << store2[i] << '\n';
	}

}

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) v.begin(), v.end()
#define LL_MAX LLONG_MAX
#define LL_MIN LLONG_MIN
#define pb push_back

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

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

if(n == 1){
	cout << 0 << '\n';
	return;
}
	vector<int>v(n);

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

	reverse(all(v));

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

		}
	}

	for(int i = 0; i < n; i++){
		int j = i;
		while(v[j] != final[i]){
			j++;
		}

		for(int k = j; k > i; k--){
			cout << v[k] << " " << v[k - 1] << '\n';
			swap(v[k],v[k - 1]);

		}

	}

}

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

Answer: 1


#include <bits/stdc++.h>
using namespace std;
#define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL)
#define int long long
#define mod 1000000007

signed main() {
    int n;
    cin >> n;
    vector<int> v(n);
    for(int i=0; i<n; i++) {
        cin>>v[i];
    }
    bool arr[n+1] = {false};
    vector<pair<int,int>> ans;
    for(int i=n-1; i>=0; i--) {
        arr[v[i]] = true;
        for(int j=v[i]-1; j>0; j--) {
            if(!arr[j]) ans.push_back({v[i],j});
        }
        for(int j=1; j<=n; j++) {
            if(!arr[j]) ans.push_back({j,v[i]});
        }

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

#include <bits/stdc++.h>
using namespace std;
#define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL)
#define int long long
#define mod 1000000007

signed main() {
    int n;
    cin >> n;
    vector<int> v(n);
    for(int i=0; i<n; i++) {
        cin>>v[i];
    }
    bool arr[n+1] = {false};
    vector<pair<int,int>> ans;
    for(int i=n-1; i>=0; i--) {
        arr[v[i]] = true;
        for(int j=v[i]-1; j>0; j--) {
            if(!arr[j]) ans.push_back({v[i],j});
        }
        for(int j=1; j<=i+1; j++) {
            if(!arr[j]) ans.push_back({j,v[i]});
        }

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

Answer: 1


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

int main()
{
   ll n;
   cin>>n;
   vector<ll>pos(n+1);
   vector<ll>cur(n+1);
   vector<ll>a(n+1);
   for(ll i = 1;i<=n;i++)
   {
     cin>>a[i];
     pos[a[i]] = i;
     cur[i] = i;
   }
   vector<pair<ll,ll>>ans;
   for(ll i = 1;i<=n;i++)
   {
     for(ll j = 1;j<n;j++)
     {
        if(pos[cur[j]] < pos[cur[j+1]])
        {
           ans.push_back({cur[j+1],cur[j]});
           swap(cur[j],cur[j+1]);
        }
     }
   }
   for(ll i = 1;i<=n;i++)
   {
     for(ll j = 1;j<n;j++)
     {
        if(pos[cur[j]] > pos[cur[j+1]])
        {
          ans.push_back({cur[j+1],cur[j]});
          swap(cur[j+1],cur[j]);
        }
     }
   }
   cout<<ans.size()<<endl;
   for(auto [x,y] : ans)
   cout<<x<<" "<<y<<endl;
   return 0;
}

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

int main()
{
   ll n;
   cin>>n;
   vector<ll>a(n);
   for(ll i = 0;i<n;i++)
   cin>>a[i];
   ll m = 0;
   vector<vector<ll>>greater(n+1,vector<ll>());
   vector<vector<ll>>smaller(n+1,vector<ll>());
   for(ll i = 0;i<n;i++)
   {
      for(ll j = i+1;j<n;j++)
      {
         if(a[j]<a[i])
        {
           m+=1;
           smaller[a[i]].push_back(a[j]);
        }
        else
        {
          m+=2;
          greater[a[i]].push_back(a[j]);
        }
      }
   }
   cout<<m<<endl;
   for(ll i = 0;i<n;i++)
   {
      for(auto it:greater[a[i]])
      {
        cout<<it<<" "<<a[i]<<endl;
      }
       for(ll j = greater[a[i]].size()-1;j>=0;j--)
      {
        cout<<a[i]<<" "<<greater[a[i]][j]<<endl;
      }

      for(auto it:smaller[a[i]])
      {
        cout<<a[i]<<" "<<it<<endl;
      }
   }

}

Answer: 1


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

#define fastIO                                                                 \
    ios::sync_with_stdio(false);                                               \
    cin.tie(NULL);                                                             \
    cout.tie(NULL);                                                            \
    cout.precision(numeric_limits<double>::max_digits10);
#define int long long

void Bankai() {
    int n;
    cin>>n;
    vector<int> v(n);
    for(auto &i:v)cin>>i;

    vector<vector<int>> vk(n+1,vector<int>(n+1,0));

    vector<int> rank(n+1);
    vector<int> temp(n);
    for(int i=0;i<n;i++){
    	rank[v[i]]=i+1;
    	temp[i]=i+1;
    }
    vector<pair<int,int>> vp;
    for(int i=0;i<n-1;i++){
    	int yh=0;
    	for(int j=0;j<n-1;j++){
    		if(rank[temp[j]]>rank[temp[j+1]]){
    			yh=1;
    			//cout<<temp[j+1]<<" "<<temp[j]<<endl;
    			swap(temp[j],temp[j+1]);
    			vp.push_back({temp[j],temp[j+1]});

    		}else if(vk[temp[j]][temp[j+1]]==0){
    		 vp.push_back({temp[j+1],temp[j]});
    		 vp.push_back({temp[j],temp[j+1]});
    		}
    		vk[temp[j]][temp[j+1]]=1;
    		vk[temp[j+1]][temp[j]]=0;
    	}
    	if(yh==0)break;
    }

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

}

int32_t main() {
    fastIO;

    int t = 1;
   // cin >> t;
   // int a=fac(200000);
    while (t--) {
        Bankai();
    }
    return 0;
}

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

#define fastIO                                                                 \
    ios::sync_with_stdio(false);                                               \
    cin.tie(NULL);                                                             \
    cout.tie(NULL);                                                            \
    cout.precision(numeric_limits<double>::max_digits10);
#define int long long

void Bankai() {
    int n;
    cin>>n;
    vector<int> v(n);
    for(auto &i:v)cin>>i;

    vector<vector<int>> vk(n+1,vector<int>(n+1,0));

    vector<int> rank(n+1);
    vector<int> temp(n);
    for(int i=0;i<n;i++){
    	rank[v[i]]=i+1;
    	//temp[i]=i;
    }
    vector<pair<int,int>> vp;
    for(int i=0;i<n;i++){
    	int yh=rank[i+1];
    	for(int j=yh;j<n;j++){
    		if(v[j]>(i+1)){
    			vp.push_back({v[j],i+1});
    		}
    	}
    	for(int j=n-1;j>=yh;j--){
    		if(v[j]>(i+1)){
    			vp.push_back({i+1,v[j]});
    		}
    	}
    	for(int j=yh-2;j>=0;j--){
    		if(v[j]>(i+1)){
    			vp.push_back({i+1,v[j]});
    		}
    	}
    }

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

}

int32_t main() {
    fastIO;

    int t = 1;
   // cin >> t;
   // int a=fac(200000);
    while (t--) {
        Bankai();
    }
    return 0;
}

Answer: 2


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

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

// syntax
#define mp make_pair

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

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

using namespace std;

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

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

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

// syntax
#define mp make_pair

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

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

using namespace std;

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

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

Answer: 1


#include <bits/stdc++.h>

#ifndef LOCAL
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#endif
#define COPYRIGHT "Copyright (c) 2024 ,yzn. All rights Reserved."
#define longbi xjn
#define AUTHOR yzn
#define YEAR 2025
#define kzn yzn

#define inf (1ll<<60)

#define fori(i, n) for (int i = 1; i <= n; i++)
#define ford(i, n,m) for (int i = n; i > m; i--)

#define ll long long
#define int ll

#define clr(n,m) for(int i=0;i<=m;i++)n[i]=0;
#define sinf(n,m) for(int i=0;i<=m;i++)n[i]=inf;

#define vi vector<int>
#define pi pair<int, int>
#define vpi vector<pair<int, int>>
#define fi first
#define se second

constexpr double eps=1e-6;
constexpr bool debug=0;
#define ID if(debug)
char abt[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
using namespace std;
namespace IO{
	char ibuf[(1<<20)+1],*iS,*iT;
	#ifndef LOCAL
	#define gh() (iS==iT?iT=(iS=ibuf)+fread(ibuf,1,(1<<20)+1,stdin),(iS==iT?EOF:*iS++):*iS++)
 	#else
	#define gh() getchar()
	#endif
	inline long long read(){char ch=gh();long long x=0;bool t=0;while(ch<'0'||ch>'9')   t|=ch=='-',ch=gh();while(ch>='0'&&ch<='9') x=x*10+(ch^48),ch=gh();return t?-x:x;}
	inline void write(int x){static int sta[35];int top = 0;do{sta[top++] = x % 10, x /= 10;} while (x);while (top)putchar(sta[--top] + 48);}
	const void File(bool b1,bool b2){if(b1)freopen("out.out","w",stdout);if(b2)freopen("in.in","r",stdin);}
	void YN(bool b){if(b)puts("Yes");else puts("No");}
}
using namespace IO;
const int N=1e3+5;
int n,A[N];
/*
冒泡排序...没有交换的对子互相交换一下.
*/
vector<pi>opt;
signed main()
{
    n=read();
    for(int i=1;i<=n;i++)A[i]=read();
    for(int i=1;i<n;i++){
        if(A[i]<A[i+1])opt.push_back({A[i],A[i+1]}),opt.push_back({A[i+1],A[i]});
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<n;j++){
            if(A[j]>A[j+1])opt.push_back({A[j],A[j+1]}),swap(A[j],A[j+1]);
        }
    }
    cout<<opt.size()<<endl;
    reverse(opt.begin(),opt.end());
    for(pi j:opt)cout<<j.first<<' '<<j.second<<endl;
}
//standard template

#include <bits/stdc++.h>

#ifndef LOCAL
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#endif
#define COPYRIGHT "Copyright (c) 2024 ,yzn. All rights Reserved."
#define longbi xjn
#define AUTHOR yzn
#define YEAR 2025
#define kzn yzn

#define inf (1ll<<60)

#define fori(i, n) for (int i = 1; i <= n; i++)
#define ford(i, n,m) for (int i = n; i > m; i--)

#define ll long long
#define int ll

#define clr(n,m) for(int i=0;i<=m;i++)n[i]=0;
#define sinf(n,m) for(int i=0;i<=m;i++)n[i]=inf;

#define vi vector<int>
#define pi pair<int, int>
#define vpi vector<pair<int, int>>
#define fi first
#define se second

constexpr double eps=1e-6;
constexpr bool debug=0;
#define ID if(debug)
char abt[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
using namespace std;
namespace IO{
	char ibuf[(1<<20)+1],*iS,*iT;
	#ifndef LOCAL
	#define gh() (iS==iT?iT=(iS=ibuf)+fread(ibuf,1,(1<<20)+1,stdin),(iS==iT?EOF:*iS++):*iS++)
 	#else
	#define gh() getchar()
	#endif
	inline long long read(){char ch=gh();long long x=0;bool t=0;while(ch<'0'||ch>'9')   t|=ch=='-',ch=gh();while(ch>='0'&&ch<='9') x=x*10+(ch^48),ch=gh();return t?-x:x;}
	inline void write(int x){static int sta[35];int top = 0;do{sta[top++] = x % 10, x /= 10;} while (x);while (top)putchar(sta[--top] + 48);}
	const void File(bool b1,bool b2){if(b1)freopen("out.out","w",stdout);if(b2)freopen("in.in","r",stdin);}
	void YN(bool b){if(b)puts("Yes");else puts("No");}
}
using namespace IO;
const int N=1e3+5;
int n,A[N],C[N],wp[N][N];
pi B[N];
/*
冒泡排序...没有交换的对子互相交换一下.
*/
vector<pi>opt;
signed main()
{
    n=read();
    for(int i=1;i<=n;i++)A[i]=read(),C[A[i]]=i;

    /*for(int i=1;i<=n;i++){
        for(int j=1;j<n;j++){
            if(A[j]>A[j+1])opt.push_back({A[j],A[j+1]}),swap(A[j],A[j+1]);
        }
    }*/
    for(int i=1;i<=n;i++)B[i]={i,C[i]};
    for(int i=1;i<=n;i++){
        for(int j=1;j<n;j++){
            if(B[j].se>B[j+1].se)opt.push_back({B[j+1].fi,B[j].fi}),wp[B[j+1].fi][B[j].fi]=wp[B[j].fi][B[j+1].fi]=1,swap(B[j],B[j+1]);
            else if(!wp[B[j].fi][B[j+1].fi])opt.push_back({B[j+1].fi,B[j].fi}),swap(B[j],B[j+1]);
        }
    }
    for(int i=1;i<n;i++){
        if(!wp[A[i]][A[i+1]])opt.push_back({A[i+1],A[i]}),opt.push_back({A[i],A[i+1]});
    }
    cout<<opt.size()<<endl;
    for(pi j:opt)cout<<j.first<<' '<<j.second<<endl;
}
//standard template
/*
1 2 3 4
2 1 3 4
2 1 4 3
2 4 1 3
4 2 1 3
4 2 3 1
4 2 1 3

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

*/

Answer: 2


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

int a[N];

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

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

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

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

int a[N];

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

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

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

Answer: 2


#pragma ccc optimize(3)
#pragma ccc optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define INF 0x3f3f3f3f
#define il inline
#define pb push_back
#define ft first
#define sd second
#define mp make_pair
#define pb push_back
#define P pair<int,int>
//#define cin fin
//#define cout fout
//ifstream cin("文件名.in");
//ofstream cout("文件名.out");
int a[1005];
void solve(){
	int n;
	cin >> n;
	vector<P > ans;
	for(int i = 1; i <= n; ++i)cin >> a[i];
	for(int i = 1; i <= n; ++i){
		for(int j = i + 1; j <= n; ++j){
			if(a[i] < a[j]){
				ans.pb(mp(a[j], a[i]));
				ans.pb(mp(a[i], a[j]));
			}
			else ans.pb(mp(a[i], a[j]));
		}
	}
	cout << ans.size() << endl;
	for(int i = 0; i < ans.size(); ++i)cout << ans[i].ft << ' ' << ans[i].sd << endl;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	solve();
	return 0;
}

#pragma ccc optimize(3)
#pragma ccc optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define INF 0x3f3f3f3f
#define il inline
#define pb push_back
#define ft first
#define sd second
#define mp make_pair
#define pb push_back
#define P pair<int,int>
//#define cin fin
//#define cout fout
//ifstream cin("文件名.in");
//ofstream cout("文件名.out");
int a[1005];
bool expr[1005];
void solve(){
	int n;
	cin >> n;
	vector<P > ans;
	for(int i = 1; i <= n; ++i)cin >> a[i];
	for(int i = 1; i <= n; ++i){
		for(int j = a[i] + 1; j <= n; ++j){
			if(expr[j])continue;
			ans.pb(mp(j, a[i]));
		}
		expr[a[i]] = 1;
		for(int j = n; j >= 1; --j){
			if(expr[j])continue;
			ans.pb(mp(a[i], j));
		}
	}
	cout << ans.size() << endl;
	for(int i = 0; i < ans.size(); ++i)cout << ans[i].ft << ' ' << ans[i].sd << endl;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	solve();
	return 0;
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;
//unsigned long long mod = 998244353;
const int mod = (1e9) + 7;
#define MAXN 100000
#define int long long
#define sort(v) sort(v.begin(), v.end())
#define print_with_spaces(v, n) for (unsigned int i = 0; i < n; i++){cout << v[i] << " ";}
#define print_with_lines(v, n) for (unsigned int i = 0; i < n; i++){cout << v[i] << "\n";}
#define itr(i,a,b) for (unsigned int i = a; i < b; i++)
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define is_possible cout << ((possible) ? "YES\n": "NO\n")
#define ull unsigned long long
#define ll long long
#define pb push_back
#define pi pair<unsigned long long, unsigned long long>
#define pii pair<int,int>
#define vi vector<int>
#define mp make_pair
#define read(v, n) for (unsigned int i = 0; i < n; i++){cin>>v[i];}

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

	int n;cin >> n;
	vi a(n + 1);
	for (int i = 1;i <= n;i++)cin >> a[i];
	int c = 0;
	for (int i = 1;i <= n;i++)
		for (int j = i + 1;j <= n;j++){
			if (a[i] > a[j])c++;
}
	int ans = 0;
	for (int i = 1;i <= n;i++) {
		set<int, greater<int>> st;
		for (int j = i + 1;j <= n;j++) if (a[i] > a[j])st.insert(a[j]);
		for (auto& p : st) {
			//cout << a[i] << " " << p << '\n';
			ans++;
		}
	}

	cout << n*(n-1)-c << endl;
	for (int i = 1;i <= n;i++) {
		set<int, greater<int>> st;
		for (int j = i + 1;j <= n;j++) if (a[i] > a[j])st.insert(a[j]);
		for (auto& p : st) {
			cout << a[i] << " " << p << '\n';
		}
	}
	auto dfs= [&](auto &&self) {
		int k = n;
		while (k >= 2 && a[k - 1] > a[k])k--;
		if (k == 1) return;

		if (k >= 2 && a[k-1] < a[k]) {
			cout << a[k] << " " << a[k-1] << '\n';
			swap(a[k], a[k-1]);
			self(self);
			swap(a[k], a[k-1]);
			cout << a[k-1] << " " << a[k] << '\n';

		}

		};
	dfs(dfs);

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
//unsigned long long mod = 998244353;
const int mod = (1e9) + 7;
#define MAXN 100000
#define int long long
#define sort(v) sort(v.begin(), v.end())
#define print_with_spaces(v, n) for (unsigned int i = 0; i < n; i++){cout << v[i] << " ";}
#define print_with_lines(v, n) for (unsigned int i = 0; i < n; i++){cout << v[i] << "\n";}
#define itr(i,a,b) for (unsigned int i = a; i < b; i++)
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define is_possible cout << ((possible) ? "YES\n": "NO\n")
#define ull unsigned long long
#define ll long long
#define pb push_back
#define pi pair<unsigned long long, unsigned long long>
#define pii pair<int,int>
#define vi vector<int>
#define mp make_pair
#define read(v, n) for (unsigned int i = 0; i < n; i++){cin>>v[i];}

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

	int n;cin >> n;
	vi a(n + 1);
	for (int i = 1;i <= n;i++)cin >> a[i];
	int c = 0;
	for (int i = 1;i <= n;i++)
		for (int j = i + 1;j <= n;j++){
			if (a[i] > a[j])c++;
}
	int ans = 0;
	for (int i = 1;i <= n;i++) {
		set<int, greater<int>> st;
		for (int j = i + 1;j <= n;j++) if (a[i] > a[j])st.insert(a[j]);
		for (auto& p : st) {
			//cout << a[i] << " " << p << '\n';
			ans++;
		}
	}
	vi temp;

	for (int i = n;i >= 2;i--) {
		int k = i - 1;

		while (k >= 1 && a[k] < a[i]) {
			//cout << a[i] << " " << a[k] << '\n';
			temp.pb(a[k]);
			k--;
			ans += 1;
		}
		while (!temp.empty()) {
			//cout << temp.back() << " " << a[i] << '\n';
			temp.pop_back();
			ans++;
		}
		temp.clear();
	}

	cout << ans << endl;
	for (int i = 1;i <= n;i++) {
		set<int, greater<int>> st;
		for (int j = i + 1;j <= n;j++) if (a[i] > a[j])st.insert(a[j]);
		for (auto& p : st) {
			cout << a[i] << " " << p << '\n';
			//ans++;
		}
	}
	for (int i = n;i >= 2;i--) {
		int k = i-1;

		while (k>=1&& a[k ] < a[i]) {
			cout << a[i] << " " << a[k] << '\n';
			temp.pb(a[k]);
			k--;
			//ans += 1;
		}
		while (!temp.empty()) {
			cout << temp.back() << " " << a[i] << '\n';
			temp.pop_back();
			//ans++;
		}
		temp.clear();
	}

	return 0;
}

Answer: 1


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

int  main()
{
     long long int  n,t=1,i,j,m,k;
    //cin >> t ;
    while(t--)
    {
    //string s;
    cin >> n ;
    long long int ans=0,a[n],b[n+1]={};
    char op;
    //pair<long long int,long long int>a[n];
    for(i=0;i<n;i++)
    {
      cin >> a[i] ;
      ans+=max(i-i,n-a[i])+n-i-1;
    }
    cout << ans << "\n" ;
    for(i=0;i<n;i++)
    {
      //b[a[i]]++;
      for(j=a[i]+1;j<=n;j++)
      {
        if(b[j]==0)
        cout << j << " " << a[i] << "\n" ;
      }
      for(j=n;j>0;j--)
      {
        if(b[j]==0&&j!=a[i])
        cout << a[i] << " " << j << "\n" ;
      }
      b[a[i]]++;
    }
  }
  return 0;
}

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

int  main()
{
     long long int  n,t=1,i,j,m,k;
    //cin >> t ;
    while(t--)
    {
    //string s;
    cin >> n ;
    long long int ans=0,a[n],b[n+1]={},x;
    vector<long long int>c;
    char op;
    //pair<long long int,long long int>a[n];
    for(i=0;i<n;i++)
    {
      cin >> a[i] ;
      x=n-a[i];
      for(j=0;j<c.size()&&x;j++)
      {
        if(c[j]>a[i])
        x--;
      }
      ans+=x+n-i-1;
      c.push_back(a[i]);
    }
    cout << ans << "\n" ;
    for(i=0;i<n;i++)
    {
      //b[a[i]]++;
      for(j=a[i]+1;j<=n;j++)
      {
        if(b[j]==0)
        cout << j << " " << a[i] << "\n" ;
      }
      for(j=n;j>0;j--)
      {
        if(b[j]==0&&j!=a[i])
        cout << a[i] << " " << j << "\n" ;
      }
      b[a[i]]++;
    }
  }
  return 0;
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
const int N=1100;
int a[N];
int n;
int ans;
vector<pair<int,int>>cnt;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
	}
	if(n==1)
	{
		cout<<ans<<endl;return 0;
	}
	for(int i=1;i<=n-1;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			if(a[i]<a[j])
			{
				ans+=2;
				cnt.push_back({a[j],a[i]});
				cnt.push_back({a[i],a[j]});
			}
			else
			{
				ans++;
				cnt.push_back({a[i],a[j]});
			}
		}
	}
	cout<<ans<<endl;
	for(auto it:cnt)
	{
		cout<<it.first<<' '<<it.second<<endl;
	}
}

#include<bits/stdc++.h>
using namespace std;
const int N=1100;
int a[N],pos[N],b[N];
int n;
int ans;
vector<pair<int,int>>cnt;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		pos[a[i]]=i;
		b[i]=i;
	}
	if(n==1)
	{
		cout<<ans<<endl;return 0;
	}
	for(int i=n;i;i--)
	{
		int ind;
		for(int j=1;j<=n;j++)
		{
			if(b[j]==a[i])
			{
				ind=j;break;
			}
		}
		for(int j=ind-1;j;j--)
		{
			cnt.push_back({b[j+1],b[j]});
			swap(b[j+1],b[j]);
		}
		for(int j=2;j<=i;j++)
		{
			cnt.push_back({b[j],b[j-1]});
			swap(b[j-1],b[j]);
		}
		//for(int j=1;j<=n;j++)cout<<b[j]<<' ';cout<<endl;
	}
	cout<<cnt.size()<<endl;
	for(auto it:cnt)cout<<it.first<<' '<<it.second<<endl;
}

Answer: 2


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

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

#define rep(n) for (int _i_i_ = (n); _i_i_; --_i_i_)
#define mem(arr, x) memset((arr), x, sizeof (arr))
#define mkp make_pair
#define edge(y, x, ver) for (int _i_ = h[x], y = ver[_i_]; _i_; y = ver[_i_ = nxt[_i_]])
#define import mt19937
#define random rnd(chrono::system_clock::now().time_since_epoch().count())
#define setdb(v) setprecision(v)
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int maxn = 1e3 + 5;

int n;
int c, a[maxn], p[maxn];

void solve() {
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> c;
		p[c] = i;
		a[i] = n - i + 1;
	}
	vector<pii> ans;
	for (int i = 1; i <= n; ++i) {
		for (int j = i + 1; j <= n; ++j) {
			ans.emplace_back(j, i);
		}
	}
	for (int i = 1; i <= n; ++i) {
		for (int j = n; j > i; --j) {
			if (p[a[j - 1]] > p[a[j]]) {
				ans.emplace_back(a[j], a[j - 1]);
				swap(a[j - 1], a[j]);
			}
		}
	}
	cout << ans.size() << '\n';
	for (auto &[x, y] : ans) cout << x << ' ' << y << '\n';
}

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

/*
+---------------------------------------------+
|GREY|GREEN|CYAN|BLUE|PURPLE|YELLOW|ORANGE|RED|
+---------------------------------------------+
*/

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

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

#define rep(n) for (int _i_i_ = (n); _i_i_; --_i_i_)
#define mem(arr, x) memset((arr), x, sizeof (arr))
#define mkp make_pair
#define edge(y, x, ver) for (int _i_ = h[x], y = ver[_i_]; _i_; y = ver[_i_ = nxt[_i_]])
#define import mt19937
#define random rnd(chrono::system_clock::now().time_since_epoch().count())
#define setdb(v) setprecision(v)
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int maxn = 1e3 + 5;

int n;
int c, p[maxn];

void solve() {
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> c;
		p[c] = i;
	}
	vector<pii> ans;
	for (int i = 1; i <= n; ++i) {
		for (int j = i + 1; j <= n; ++j) {
			ans.emplace_back(j, i);
			if (p[i] < p[j]) ans.emplace_back(i, j);
		}
	}
	cout << ans.size() << '\n';
	for (auto &[x, y] : ans) cout << x << ' ' << y << '\n';
}

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

/*
+---------------------------------------------+
|GREY|GREEN|CYAN|BLUE|PURPLE|YELLOW|ORANGE|RED|
+---------------------------------------------+
*/

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 = 1;j < a[i];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;
	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;

int main() {
    int n;
    cin>>n;
    vector<int>arr(n);
    unordered_map<int, int>mp;
    int count = 0;
    vector<pair<int, int>>cd;
    for(int i = 0;i<n;i++)
    {
        cin>>arr[i];
        mp[arr[i]] = i+1;
    }
    for(int i = 1;i<n;i++)
    {
        for(int j = i+1;j<=n;j++)
        {
            if(mp[i] > mp[j])
            {
                count++;
                cd.push_back({j,i});
            }
            else
            {
                count += 2;
                cd.push_back({j, i});
                cd.push_back({i, j});
            }
        }
    }
    cout<<count<<endl;
    for(int i = 0;i<cd.size();i++)
    {
        cout<<cd[i].first<<" "<<cd[i].second<<endl;
    }

    return 0;
}

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

int main() {
        int n;
        cin >> n;
        vector<int>arr(n);
        for (auto &i : arr)cin >> i;
        vector<pair<int, int>>ans;
        vector<int>ou(n);
        for (int i = 0; i < n; i++)ou[i] = i + 1;
        for (int i = n; i >= 2; i--)
        {
            for (int j = i - 1; j >= 1; j--)
            {
                ans.push_back({i, j});
            }
        }
        map<int, int>mp;
        int c = 0;
        for (auto i : arr)
        {
            mp[i] = c;
            c++;
        }
        for (int i = n - 1; i > 0; i--)
        {
            int cur = i;
            for (int j = n - 1; j >= mp[i] + 1; j--)
            {
                if (arr[j] > cur)
                {
                    ans.push_back({cur, arr[j]});
                }
            }
            }

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

    return 0;
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;
#define int long long int
void print(vector<int> &a){
   // for(auto _ : a)cerr<<_<<" ";
    cerr<<endl;
}
void incheck(){
    cerr<<"inloop"<<endl;
}
void solve() {
    int n;
    cin>>n;
    vector<int> a(n+1);
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    int ans = 0;
    vector<pair<int,int>> res;
    for(int i=1;i<=n;i++){
        for(int j=1;j<n;j++){
            if(a[j] < a[j+1]){
               // incheck();
                res.push_back({a[j],a[j+1]});
                swap(a[j],a[j+1]);
            }
          // print(a);
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<n;j++){
            if(a[j] > a[j+1]){
                //incheck();
                res.push_back({a[j],a[j+1]});
                swap(a[j],a[j+1]);
            }
            //print(a);
        }
    }
    reverse(res.begin(),res.end());
    cout<<res.size()<<endl;
    for(auto it : res){
        cout<<it.first<<" "<<it.second<<endl;
    }

}

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

    int t;
    t = 1;

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

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long int
void print(vector<int> &a){
    for(auto _ : a)cerr<<_<<" ";
    cerr<<endl;
}
void incheck(){
    cerr<<"inloop"<<endl;
}
void solve() {
    int n;
    cin>>n;
    vector<int> a(n+1);
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    int ans = 0;
    vector<pair<int,int>> res;
    for(int i=1;i<=n;i++){
        for(int j=1;j<n;j++){
            if(a[j] < a[j+1]){
                //incheck();
                res.push_back({a[j+1],a[j]});
                swap(a[j+1],a[j]);
            }

        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<n;j++){
            if(a[j] > a[j+1]){
                //incheck();
                res.push_back({a[j+1],a[j]});
                swap(a[j+1],a[j]);
            }
            //print(a);
        }
    }
    cout<<res.size()<<endl;
    for(auto it : res){
        cout<<it.first<<" "<<it.second<<endl;
    }

}

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

    int t;
    cin >> t;

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

    return 0;
}

Answer: 1


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

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

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

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

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

        // v1.push_back(v[idx-1]);
        // f(j,0,v2.size()){
        //     v1.push_back(v2[j]);
        //     fe(k,v1.size()-2,0) ans.push_back({v1.back(),v1[k]});
        //     f(k,0,v1.size()-1) ans.push_back({v1[k],v1.back()});
        // }
    }
}
void solve() {
    int n;cin>>n;
    vector<int>v(n);
    map<int,int>idx;
    f(i,0,n){
        cin>>v[i];
        idx[v[i]]=i+1;
    }

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

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

Answer: 1


//Date: 2024-12-25 08:35:32
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int rd() {
    int s=0,m=0;char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-')m=1;ch=getchar();}
    while( isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
    return m?-s:s;
}
bool MBE;
namespace SOLVER {
int n,a[1005];vector<pair<int,int>> ans;
void MAIN() {
    cin>>n;for(int i=1;i<=n;i++) a[i]=rd();
    for(int i=1;i<n;i++) {
        int pos=0;for(int j=i;j<=n;j++) if(a[j]==i) pos=j;
        for(int j=pos+1;j<=n;j++) ans.emplace_back(a[j-1],a[j]),swap(a[j-1],a[j]);
        for(int j=n;j>i;j--) ans.emplace_back(a[j-1],a[j]),swap(a[j-1],a[j]);
        assert(a[i]==i);
    }
    cout<<ans.size()<<endl;
    for(int i=(int)ans.size()-1;i>=0;i--) printf("%lld %lld\n",ans[i].first,ans[i].second);
}
}
bool MED;
signed main() {
    //freopen(".in","r",stdin);freopen(".out","w",stdout);
    for(int tt=1;tt;tt--) SOLVER::MAIN();
    cerr<<(&MBE-&MED)/1024<<" KB, "<<1000*clock()/CLOCKS_PER_SEC<<" ms\n";
    return 0;
}

//Date: 2024-12-25 08:35:32
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int rd() {
    int s=0,m=0;char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-')m=1;ch=getchar();}
    while( isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
    return m?-s:s;
}
bool MBE;
namespace SOLVER {
int n,a[1005];vector<pair<int,int>> ans;
void MAIN() {
    cin>>n;for(int i=1;i<=n;i++) a[i]=rd();
    for(int i=1;i<n;i++) {
        int pos=0;for(int j=i;j<=n;j++) if(a[j]==i) pos=j;
        for(int j=pos+1;j<=n;j++) ans.emplace_back(a[j],a[j-1]),swap(a[j-1],a[j]);
        for(int j=n;j>i;j--) ans.emplace_back(a[j],a[j-1]),swap(a[j-1],a[j]);
        assert(a[i]==i);
    }
    cout<<ans.size()<<endl;
    for(auto x:ans) printf("%lld %lld\n",x.first,x.second);
}
}
bool MED;
signed main() {
    //freopen(".in","r",stdin);freopen(".out","w",stdout);
    for(int tt=1;tt;tt--) SOLVER::MAIN();
    cerr<<(&MBE-&MED)/1024<<" KB, "<<1000*clock()/CLOCKS_PER_SEC<<" ms\n";
    return 0;
}

Answer: 1


#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
class list
{
public:
    int val;
    list* next=nullptr;
};

int main() {
    int n = 0;
    cin >> n;
    vector<int> de(n);
    vector<int> pl(n+1);
    list* head=new list;

    head->next = new list;
    list* now = head->next;
    now->val = 1;
    vector<pair<int,int>> im;
    for (int i = 0; i < n;  ++i)
    {
        cin >> de[i];
    }
    for (int i = 0; i < n; ++i)
    {
        pl[de[i]] = i ;
    }

    for (int i = 2; i <= n; ++i)
    {
        int first=0;
        list* now = head->next;
        while (now != nullptr)
        {
            stack<int> aa;
                if (pl[now->val] > pl[i])
                {
                    {
                    pair<int, int> ppp(i, now->val);

                    im.push_back(ppp);
                    }
                    if (now->next == nullptr)
                    {
                        now->next = new list;
                        now->next->val = i;
                        break;
                    }
                }
            else
            {
                pair<int, int> ppp(i, now->val);
                aa.push(now->val);
                im.push_back(ppp);
                if (first == 0)
                {
                    list* nxt = new list;
                    nxt->val = now->val;
                    nxt->next = now->next;
                    now->val = i;
                    now->next = nxt;
                    first = 1;
                }
                now = now->next;
                while (now->next != nullptr)
                {
                    aa.push(now->val);
                    pair<int, int> ppp(i, now->val);
                    im.push_back(ppp);
                    now = now->next;
                }
                while (!aa.empty())
                {
                    pair<int, int> ppp(aa.top(), i); im.push_back(ppp);
                    aa.pop();
                }
            }
                if (now != nullptr)
                {
                    {
                        now = now->next;
                    }
                }
        }
    }
    cout << im.size() << endl;
    for (auto a : im)
    {
        cout << a.first << " " << a.second << endl;
    }

    return 0;
 }

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
class list
{
public:
    int val;
    list* next=nullptr;
};

int main() {
    int n = 0;
    cin >> n;
    vector<int> de(n);
    vector<int> pl(n+1);
    list* head=new list;

    head->next = new list;
    list* now = head->next;
    now->val = 1;
    vector<pair<int,int>> im;
    for (int i = 0; i < n;  ++i)
    {
        cin >> de[i];
    }
    for (int i = 0; i < n; ++i)
    {
        pl[de[i]] = i ;
    }

    for (int i = 2; i <= n; ++i)
    {
        int first=0;
        list* now = head->next;
        while (now != nullptr)
        {
            stack<int> aa;
                if (pl[now->val] > pl[i])
                {
                    {
                    pair<int, int> ppp(i, now->val);

                    im.push_back(ppp);
                    }
                    if (now->next == nullptr)
                    {
                        now->next = new list;
                        now->next->val = i;
                        break;
                    }
                }
            else
            {
                pair<int, int> ppp(i, now->val);
                aa.push(now->val);
                im.push_back(ppp);
                if (first == 0)
                {
                    list* nxt = new list;
                    nxt->val = now->val;
                    nxt->next = now->next;
                    now->val = i;
                    now->next = nxt;
                    first = 1;
                }
                now = now->next;
                now = now->next;
                while (now!= nullptr)
                {
                    aa.push(now->val);
                    pair<int, int> ppp(i, now->val);
                    im.push_back(ppp);
                    now = now->next;
                }
                while (!aa.empty())
                {
                    pair<int, int> ppp(aa.top(), i); im.push_back(ppp);
                    aa.pop();
                }
            }
                if (now != nullptr)
                {
                    {
                        now = now->next;
                    }
                }
        }
    }
    cout << im.size() << endl;
    for (auto a : im)
    {
        cout << a.first << " " << a.second << endl;
    }
    //cout << n * (n - 1);
    return 0;
 }

Answer: 2


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int x[]={0,0,-1,1};
int y[]={-1,1,0,0};
char dir[]={'L','R','U','D'};
using pi = pair<ll,ll>;

void fast() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

void solve() {
   ll n;
    cin>>n;
    vector<ll> v(n);
    for(ll i=0;i<n;i++) {
        cin>>v[i];
    }
    vector<pi>ans;
    for(ll i=n-1;i>=0;i--) {
        for(ll j=i+1;j<n;j++) {
           ans.push_back(pi(v[i],v[j]));
            ans.push_back(pi(v[j],v[i]));
        }
    }
reverse(ans.begin(),ans.end());

cout<<ans.size()<<endl;
    for(ll i=0;i<ans.size();i++) {
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    }

}

int main() {
    fast();

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

    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int x[]={0,0,-1,1};
int y[]={-1,1,0,0};
char dir[]={'L','R','U','D'};
using pi = pair<ll,ll>;

void fast() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

void solve() {
   ll n;
    cin>>n;
    vector<pi> ans;
    map<ll,ll> vis;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        vis[x] = 1;
        for (int j = x + 1; j <= n; j++) {
            if (vis[j]) continue;
            ans.push_back({j, x});
        }
        for (int j = n; j > x; j--) {
            if (vis[j]) continue;
            ans.push_back({x, j});
        }
        for (int j = x - 1; j; j--) {
            if (vis[j]) continue;
            ans.push_back({x, j});
        }
    }
    cout << ans.size();
    for (auto &i : ans) cout << '\n' << i.first << ' ' << i.second;

}

int main() {
    fast();

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

    }
    return 0;
}

Answer: 2


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

public class Main
{
    public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static  BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    public static StringTokenizer tokenizer;
    public static void read() throws IOException {
        tokenizer = new StringTokenizer(br.readLine());
    }
    public static int Int() throws IOException {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) read();
        return Integer.parseInt(tokenizer.nextToken());
    }
    public static long Long() throws IOException {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) read();
        return Long.parseLong(tokenizer.nextToken());
    }
    public static String Str() throws IOException {
        return br.readLine();
    }

    public static Character Char() throws IOException {
        return (char)br.read();
    }

    public static long[] read(int n) throws IOException {
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = Long() - 1;
        }
        return a;
    }

    //-----------------------------------------------

    public static long MOD = (long) 1e9 + 7;
    public static long quickPow(long pow, long num, long MOD) {
        if (pow == 0) {
            return 1;
        }
        if (pow == 1) {
            return num % MOD;
        }
        long half = quickPow(pow / 2, num, MOD);
        long twoHalf = (half * half) % MOD;
        return (twoHalf * quickPow(pow % 2, num, MOD)) % MOD;
    }

    public static void remove(TreeMap<Long, Long> map, long key) {
        map.put(key, map.get(key) - 1);
        if (map.get(key) == 0) map.remove(key);
    }

    public static void remove(HashMap<Long, Long> map, long key) {
        map.put(key, map.get(key) - 1);
        if (map.get(key) == 0) map.remove(key);
    }

    public static void add(TreeMap<Long, Long> map, long key) {
        map.put(key, map.getOrDefault(key, 0L) + 1);
    }

    public static void add(HashMap<Long, Long> map, long key) {
        map.put(key, map.getOrDefault(key, 0L) + 1);
    }

    public static void main(String[] args) throws IOException {
        int T = 1;
        while (T > 0) {
            T--;
            int n = Int();
            int[] a = new int[n];
            int[] start = new int[n];
            HashMap<Integer, Integer> pos = new HashMap<>();
            for (int i = 0; i < n; i++) {
                a[i] = Int();
                start[i] = i + 1;
                pos.put(i + 1, i);
            }
            ArrayList<int[]> res = new ArrayList<>();
            for (int i = n - 1; i >= 0; i--) {
                int position = pos.get(a[i]);
                for (int j = position; j > 0; j--) {
                    res.add(new int[] {start[j], start[j - 1]});
                    pos.put(start[j], j - 1);
                    pos.put(start[j - 1], j);
                    int tmp = start[j];
                    start[j] = start[j - 1];
                    start[j - 1] = tmp;
                }
            }
            System.out.println(res.size());
            for (int i = 0; i < res.size(); i++) {
                System.out.println(res.get(i)[0] + " " + res.get(i)[1]);
            }
        }
    }
}

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

public class Main
{
    public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static  BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    public static StringTokenizer tokenizer;
    public static void read() throws IOException {
        tokenizer = new StringTokenizer(br.readLine());
    }
    public static int Int() throws IOException {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) read();
        return Integer.parseInt(tokenizer.nextToken());
    }
    public static long Long() throws IOException {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) read();
        return Long.parseLong(tokenizer.nextToken());
    }
    public static String Str() throws IOException {
        return br.readLine();
    }

    public static Character Char() throws IOException {
        return (char)br.read();
    }

    public static long[] read(int n) throws IOException {
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = Long() - 1;
        }
        return a;
    }

    //-----------------------------------------------

    public static long MOD = (long) 1e9 + 7;
    public static long quickPow(long pow, long num, long MOD) {
        if (pow == 0) {
            return 1;
        }
        if (pow == 1) {
            return num % MOD;
        }
        long half = quickPow(pow / 2, num, MOD);
        long twoHalf = (half * half) % MOD;
        return (twoHalf * quickPow(pow % 2, num, MOD)) % MOD;
    }

    public static void remove(TreeMap<Long, Long> map, long key) {
        map.put(key, map.get(key) - 1);
        if (map.get(key) == 0) map.remove(key);
    }

    public static void remove(HashMap<Long, Long> map, long key) {
        map.put(key, map.get(key) - 1);
        if (map.get(key) == 0) map.remove(key);
    }

    public static void add(TreeMap<Long, Long> map, long key) {
        map.put(key, map.getOrDefault(key, 0L) + 1);
    }

    public static void add(HashMap<Long, Long> map, long key) {
        map.put(key, map.getOrDefault(key, 0L) + 1);
    }

    public static void main(String[] args) throws IOException {
        int T = 1;
        while (T > 0) {
            T--;
            int n = Int();
            int[] a = new int[n];
            int[] start = new int[n];
            HashMap<Integer, Integer> pos = new HashMap<>();
            for (int i = 0; i < n; i++) {
                a[i] = Int();
                pos.put(a[i], i);
                start[i] = i + 1;
            }
            ArrayList<int[]> res = new ArrayList<>();
            for (int i = n - 1; i >= 0; i--) {
                int position = pos.get(a[i]);
                for (int j = position; j > 0; j--) {
                    res.add(new int[] {start[j], start[j - 1]});
                    pos.put(start[j], j - 1);
                    pos.put(start[j - 1], j);
                    int tmp = start[j];
                    start[j] = start[j - 1];
                    start[j - 1] = tmp;
                }
            }
            System.out.println(res.size());
            for (int i = 0; i < res.size(); i++) {
                System.out.println(res.get(i)[0] + " " + res.get(i)[1]);
            }
        }
    }
}

Answer: 1


#include<bits/stdc++.h>
#define Akhaja ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
#include<math.h>
#define ll long long
using namespace std;

void init(){
    // Now its my turn
}

void solve(){
    ll n;
    cin>>n;
    vector<ll>a(n);
    for(ll i=0;i<n;i++){
        cin>>a[i];
    }
    set<pair<ll,ll>> mali;
    vector<pair<ll,ll>> ans;
    vector<ll> pos(n);
    map<ll,ll> mp;
    for(ll i=0;i<n;i++){
        pos[i]=(i+1);
        mp[i+1]=i;
    }

    for(ll i=0;i<n-1;i++){
        ll bando=a[i];
        ll idx=mp[bando];
        for(ll j=idx+1;j<n;j++){
            if(mali.find({pos[j],bando})==mali.end()){
                ans.push_back({pos[j],bando});
                mali.insert({pos[j],bando});
            }
        }
        for(ll j=n-1;j>=idx+1;j--){
            if(mali.find({bando,pos[j]})==mali.end()){
                ans.push_back({bando,pos[j]});
                mali.insert({bando,pos[j]});
            }
        }
        for(ll j=idx-1;j>=0;j--){
            if(mali.find({bando,pos[j]})==mali.end()){
                ans.push_back({bando,pos[j]});
                mali.insert({bando,pos[j]});
            }
            mp[bando]--;
            mp[pos[j]]++;
            pos[j+1]=pos[j];
        }
        pos[i]=bando;

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

int main(){
    Akhaja;
    // init();
    // ll t;
    // cin>>t;
    // while(t--){
        solve();
    // }
    return 0;
}

#include<bits/stdc++.h>
#define Akhaja ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
#include<math.h>
#define ll long long
using namespace std;

void init(){
    // Now its my turn
}

void solve(){
    ll n;
    cin>>n;
    vector<ll>a(n);
    for(ll i=0;i<n;i++){
        cin>>a[i];
    }
    set<pair<ll,ll>> mali;
    vector<pair<ll,ll>> ans;
    vector<ll> pos(n);
    map<ll,ll> mp;
    for(ll i=0;i<n;i++){
        pos[i]=(i+1);
        mp[i+1]=i;
    }

    for(ll i=0;i<n-1;i++){
        ll bando=a[i];
        ll idx=mp[bando];
        for(ll j=idx+1;j<n;j++){
            if(mali.find({pos[j],bando})==mali.end()){
                ans.push_back({pos[j],bando});
                mali.insert({pos[j],bando});
            }
        }
        for(ll j=n-1;j>=idx+1;j--){
            if(mali.find({bando,pos[j]})==mali.end()){
                ans.push_back({bando,pos[j]});
                mali.insert({bando,pos[j]});
            }
        }
        for(ll j=idx-1;j>=i;j--){
            if(mali.find({bando,pos[j]})==mali.end()){
                ans.push_back({bando,pos[j]});
                mali.insert({bando,pos[j]});
            }
            mp[bando]--;
            mp[pos[j]]++;
            pos[j+1]=pos[j];
        }
        pos[i]=bando;

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

int main(){
    Akhaja;
    // init();
    // ll t;
    // cin>>t;
    // while(t--){
        solve();
    // }
    return 0;
}

Answer: 2


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

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

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

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

    system("pause");
    return 0;
}

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

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

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

    system("pause");
    return 0;
}

Answer: 2


#include<iostream>
#include<math.h>
#include<unordered_set>
#include<unordered_map>
#include<list>
#include<string>
#include<limits.h>
#include<vector>
#include<queue>
#include<algorithm>
#include<stack>
#include<set>
#include<sstream>
#include<utility>
#include<cstring>
#define int long long
using namespace std;

int cnt = 1;
void solve() {
	int n;
	cin >> n;
	int final[n];
	vector<int> cur(n);
	for (auto& e : final) {
		cin >> e;
	}
	for (int i = 0; i < n; i++) {
		cur[i] = cnt++;
	}

	cnt = 0;
	/*int overtake[n + 1][n + 1];
	for (int i = 0; i <= n; i++) {
		memset(overtake[i], 0, sizeof(overtake[i]));
	}*/
	for (int i = 0; i < n; i++) {
		int index;
		for (int j = n - 1; j >= 0; j--) {
			if (cur[j] == final[i]) {
				index = j;
				break;
			}
		}
		for (int j = index + 1; j < n; j++) {
			cout << cur[j] << ' ' << final[i] << endl;
			cnt++;
		}
		for (int j = n - 1; j > index; j--) {
			cout << final[i] << ' ' << cur[j] << endl;
			cnt++;
		}
		for (int j = index - 1; j >= i; j--) {
			cout << final[i] << ' ' << cur[j] << endl;
			cnt++;
		}
		cur.erase(cur.begin() + index);
		cur.insert(cur.begin() + i, final[i]);
	}
	cout << cnt;
}

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

#include<iostream>
#include<math.h>
#include<unordered_set>
#include<unordered_map>
#include<list>
#include<string>
#include<limits.h>
#include<vector>
#include<queue>
#include<algorithm>
#include<stack>
#include<set>
#include<sstream>
#include<utility>
#include<cstring>
#define int long long
using namespace std;

int cnt = 1;
void solve() {
	int n;
	cin >> n;
	int final[n];
	vector<int> cur(n);
	for (auto& e : final) {
		cin >> e;
	}
	for (int i = 0; i < n; i++) {
		cur[i] = cnt++;
	}

	cnt = 0;
	vector<int> res;
	/*int overtake[n + 1][n + 1];
	for (int i = 0; i <= n; i++) {
		memset(overtake[i], 0, sizeof(overtake[i]));
	}*/
	for (int i = 0; i < n; i++) {
		int index;
		for (int j = n - 1; j >= 0; j--) {
			if (cur[j] == final[i]) {
				index = j;
				break;
			}
		}
		for (int j = index + 1; j < n; j++) {
			//cout << cur[j] << ' ' << final[i] << endl;
			res.push_back(cur[j]);
			res.push_back(final[i]);
			cnt++;
		}
		for (int j = n - 1; j > index; j--) {
			//cout << final[i] << ' ' << cur[j] << endl;
			res.push_back(final[i]);
			res.push_back(cur[j]);
			cnt++;
		}
		for (int j = index - 1; j >= i; j--) {
			//cout << final[i] << ' ' << cur[j] << endl;
			res.push_back(final[i]);
			res.push_back(cur[j]);
			cnt++;
		}
		cur.erase(cur.begin() + index);
		cur.insert(cur.begin() + i, final[i]);
	}

	cout << cnt << endl;
	for (int i = 0; i < res.size(); i += 2) {
		cout << res[i] << " " << res[i + 1] << endl;
	}
}

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

Answer: 2


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    vector<int> pos(n+1);
    vector<int> v(n+1);

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

    vector<pair<int,int> > ans;

    for(int i=1;i<=n;i++){
        int car = i;
        stack<pair<int,int> > st1;

        for(int j = pos[car] + 1; j<=n;j++){
            if(v[j] < car){
                // cout<<car<<" : "<<car<<" -> "<<j<<endl;
                st1.push(make_pair(car, v[j]));
            }
        }

        while(!st1.empty()){
            pair<int,int> p = st1.top();
            st1.pop();
            // cout<<i<<" "<<car<<" -> "<<j<<endl;
            ans.push_back(make_pair(p.first, p.second));
        }

        stack<pair<int,int> > st;

        for(int j = pos[car]-1; j>=1; j--){
            if(v[j] < car){
                // cout<<i<<" "<<car<<" -> "<<j<<endl;
                ans.push_back(make_pair(car, v[j]));
                st.push(make_pair(car, v[j]));
            }
        }

        while(!st.empty()){
            pair<int,int> p = st.top();
            st.pop();
            // cout<<i<<" "<<car<<" -> "<<j<<endl;
            ans.push_back(make_pair(p.second, p.first));
        }

    }

    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>
#define ll long long int
using namespace std;
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    vector<int> pos(n+1);

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

    vector<pair<int,int> > ans;

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

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

    return 0;
}

Answer: 1


#include <stdio.h>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
	long long n, i, j;

	cin >> n;

	long long* c = new long long[n];

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

	long long num_inverse = 0;

	for (i = 0; i < n; i++)
	{
		for (j = i; j < n; j++)
		{
			if (c[i] > c[j])
			{
				num_inverse++;
			}
		}
	}

	long long num_pass = n * (n - 1) - num_inverse;

	vector<long long> arr;

	long long* pos_arr = new long long[n + 1];

	for (i = 0; i < n; i++)
	{
		arr.push_back(i + 1);

		pos_arr[i + 1] = i;
	}

	cout << num_pass << endl;

	for (i = 1; i < n + 1; i++)
	{
		long long pos = pos_arr[c[i - 1]];

		for (j = pos + 1; j < n; j++)
		{
			cout << arr[j] << " " << c[i - 1] << endl;
		}

		for (j = n - 1; j >= i; j--)
		{
			if (j != pos)
			{
				cout << c[i - 1] << " " << arr[j] << endl;
			}

			if (j < pos)
			{
				pos_arr[arr[j]]++;
			}
		}

		arr.erase(arr.begin() + pos);

		arr.insert(arr.begin() + pos, c[i - 1]);

		pos_arr[c[i - 1]] = i - 1;
	}

	return 0;
}

#include <stdio.h>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
	long long n, i, j;

	cin >> n;

	long long* c = new long long[n];

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

	long long num_inverse = 0;

	for (i = 0; i < n; i++)
	{
		for (j = i; j < n; j++)
		{
			if (c[i] > c[j])
			{
				num_inverse++;
			}
		}
	}

	long long num_pass = n * (n - 1) - num_inverse;

	vector<long long> arr;

	long long* pos_arr = new long long[n + 1];

	for (i = 0; i < n; i++)
	{
		arr.push_back(i + 1);

		pos_arr[i + 1] = i;
	}

	cout << num_pass << endl;

	for (i = 1; i < n + 1; i++)
	{
		long long pos = pos_arr[c[i - 1]];

		for (j = pos + 1; j < n + 1 - i; j++)
		{
			cout << arr[j] << " " << c[i - 1] << endl;
		}

		for (j = n - i; j >= 0; j--)
		{
			if (j != pos)
			{
				cout << c[i - 1] << " " << arr[j] << endl;
			}

			if (j > pos)
			{
				pos_arr[arr[j]]--;
			}
		}

		arr.erase(arr.begin() + pos);
	}

	return 0;
}

Answer: 2


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

#define pb push_back
#define ll long long int
#define mp make_pair
#define MAXN 100001

#define sz(x) static_cast<int>((x).size())
#define all(x) (x).begin(), (x).end()
#define input(v) for(ll i=0;i<(long long int)v.size();i++) cin>>v[i];
ll MOD=998244353;
void YES() {cout<<"YES"<<endl;}
void NO(){cout<<"NO"<<endl;}
ll max(ll a,ll b){
    if(a>=b) return a;
    return b;
}
ll gcd(ll a,ll b)
{
    ll x1=min(a,b);
    ll x2=max(a,b);
    if(x1<=0) return x2;
    ll x=x2%x1;
    return gcd(x,x1);
}
void print(vector<ll>&v){
    ll n=(ll)v.size();
    for(ll j=0;j<n;j++) cout<<v[j]<<" ";
    cout<<"\n";
    return;
}
ll lcm(ll a,ll b){
    if(a==0) return b;
    if(b==0) return a;
    ll g=gcd(a,b);
    return (a*b)/g;
}
ll onesComplement(ll n){
    ll number_of_bits = floor(log2(n)) + 1;
    return ((1 << number_of_bits) - 1) ^ n;
}
ll powmod(ll n,ll k){
    ll ans=1;
    if(k<0) return 0;
    while(k--){
        ans*=n;
        ans%=MOD;
    }
    return ans;
}

ll power(ll x, ll y, ll p){
    ll res = 1;
    x = x % p;
    while (y > 0) {
        if (y & 1)
            res = (res * x) % p;
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}
ll modInverse(ll n, ll p){
    return power(n, p - 2, p);
}

void solve(){
    ll n;
    cin>>n;
    vector<ll>a(n);
    input(a);
    vector<pair<ll,ll>>v;
    map<ll,ll>m;
    for(ll i=0;i<n;i++) m[a[i]]=i;
    for(ll i=1;i<=n;i++){
        for(ll j=i+1;j<=n;j++){
            v.pb({j,i});
            if(m[i]<m[j]) v.pb({i,j});
        }
    }
    cout<<v.size()<<"\n";
    for(auto i:v) cout<<i.first<<" "<<i.second<<"\n";
    return;
}

int main ()
{
    #ifndef ONLINE_JUDGE
        // For getting input from input.txt file
        freopen("input.txt", "r", stdin);
        // Printing the Output to output.txt file
        freopen("output.txt", "w", stdout);
        // Printing the run time of code in Terminal
        class Timer { chrono::time_point <chrono::steady_clock> B, E; public: Timer () : B(), E()
        { B = chrono::steady_clock::now(); } ~Timer () { E = chrono::steady_clock::now();
        cerr << "\nDuration: " << ((chrono::duration <double>)(E - B)).count() << "s\n"; } } TimerT;
    #endif
       // For faster Input/Output
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t = 1;
    // cin>>t;
    while(t--) solve();
}

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

#define pb push_back
#define ll long long int
#define mp make_pair
#define MAXN 100001

#define sz(x) static_cast<int>((x).size())
#define all(x) (x).begin(), (x).end()
#define input(v) for(ll i=0;i<(long long int)v.size();i++) cin>>v[i];
ll MOD=998244353;
void YES() {cout<<"YES"<<endl;}
void NO(){cout<<"NO"<<endl;}
ll max(ll a,ll b){
    if(a>=b) return a;
    return b;
}
ll gcd(ll a,ll b)
{
    ll x1=min(a,b);
    ll x2=max(a,b);
    if(x1<=0) return x2;
    ll x=x2%x1;
    return gcd(x,x1);
}
void print(vector<ll>&v){
    ll n=(ll)v.size();
    for(ll j=0;j<n;j++) cout<<v[j]<<" ";
    cout<<"\n";
    return;
}
ll lcm(ll a,ll b){
    if(a==0) return b;
    if(b==0) return a;
    ll g=gcd(a,b);
    return (a*b)/g;
}
ll onesComplement(ll n){
    ll number_of_bits = floor(log2(n)) + 1;
    return ((1 << number_of_bits) - 1) ^ n;
}
ll powmod(ll n,ll k){
    ll ans=1;
    if(k<0) return 0;
    while(k--){
        ans*=n;
        ans%=MOD;
    }
    return ans;
}

ll power(ll x, ll y, ll p){
    ll res = 1;
    x = x % p;
    while (y > 0) {
        if (y & 1)
            res = (res * x) % p;
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}
ll modInverse(ll n, ll p){
    return power(n, p - 2, p);
}

void solve(){
    ll n;
    cin>>n;
    vector<ll>a(n);
    input(a);
    set<pair<ll,ll>>s;
    map<ll,ll>m;
    for(ll i=0;i<n;i++) m[a[i]]=i;
    for(ll i=1;i<=n;i++){
        for(ll j=i+1;j<=n;j++){
            s.insert({j,i});
            if(m[i]<m[j]) s.insert({i,j});
        }
    }
    cout<<s.size()<<"\n";
    vector<ll>v1;
    for(ll i=1;i<=n;i++) v1.pb(i);
    ll i=0;
    vector<pair<ll,ll>>ans;
    while(i<n){
        if(i-1>=0){
            pair<ll,ll>p={v1[i],v1[i-1]};
            if(s.find(p)!=s.end()){
                swap(v1[i-1],v1[i]);
                i--;
                s.erase(p);
                ans.pb(p);
                continue;
            }
        }
        if(i+1<n){
            pair<ll,ll>p={v1[i+1],v1[i]};
            if(s.find(p)!=s.end()){
                swap(v1[i],v1[i+1]);
                s.erase(p);
                ans.pb(p);
                continue;
            }
        }
        i++;
    }
    for(auto i:ans) cout<<i.first<<" "<<i.second<<"\n";
    return;
}

int main ()
{
    #ifndef ONLINE_JUDGE
        // For getting input from input.txt file
        freopen("input.txt", "r", stdin);
        // Printing the Output to output.txt file
        freopen("output.txt", "w", stdout);
        // Printing the run time of code in Terminal
        class Timer { chrono::time_point <chrono::steady_clock> B, E; public: Timer () : B(), E()
        { B = chrono::steady_clock::now(); } ~Timer () { E = chrono::steady_clock::now();
        cerr << "\nDuration: " << ((chrono::duration <double>)(E - B)).count() << "s\n"; } } TimerT;
    #endif
       // For faster Input/Output
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t = 1;
    // cin>>t;
    while(t--) solve();
}

Answer: 2


//starting with the name of almighty ALLAH
// Practice is the only shortcut to improve
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define vc vector
#define vi vc<int>
#define vl vc<ll>
#define mp(x,y) make_pair(x,y)
#define yes cout<<"YES"<<'\n';
#define no cout<<"NO"<<'\n';
#define tst int t;cin>>t;while(t--)
#define srt(v) sort(v.begin(),v.end());
#define rsrt(v) sort(v.rbegin(),v.rend());
#define rj ios::sync_with_stdio(false);cin.tie(0);
#define rvs(v) reverse(v.begin(),v.end());
#define F first
#define S second
#define MOD 1000000007
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define PI 2*acos(0.0)
#define pii pair<int,int>
#define fr(i,a,b) for(int i=a;i<=b;i++)
#define coutv(v) for(auto it:v)cout<<it<<' ';cout<<endl;
#define cinv(v) for(auto &it:v)cin>>it;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ld long double
#define nl '\n'
const int N = 1e5;
using namespace std;
/* #ifndef ONLINE_JUDGE
#include "algo/debug.h"
#else
#define dbg(x...)
#endif */
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int my_rand(int l, int r)
{
    return uniform_int_distribution<int>(l, r) (rng);
}
void solve()
{
    int a;
    cin >> a;;
    vector<int>v(a + 1), in(a + 1);
    for (int i = 1; i <= a; i++)
    {
        cin >> v[i];
        in[v[i]] = i;
    }
    vector<pii>an;
    for (int i = 1; i <= a; i++)
    {
        while (in[i] < a)
        {
            int nx = v[in[i] + 1];
            an.push_back({i, nx});
            swap(in[i], in[nx]);
            swap(v[in[i]], v[in[nx]]);
        }
        while (in[i] > i)
        {
            int nx = v[in[i] - 1];
            an.push_back({nx, i});
            swap(in[i], in[nx]);
            swap(v[in[i]], v[in[nx]]);
        }
    }
    rvs(an)
    cout << (int)an.size() << nl;
    for (auto &it : an)
    {
        cout << it.F << ' ' << it.S << nl;
    }
}
int main()
{
    /*#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif // ONLINE_JUDGE*/
    rj
    //int t;cin>>t;fr(i,1,t) cout<<"Case "<<i<<": ",solve();
    //ll t;scanf("%lld",&t);fr(i,1,t) printf("Case %lld: ",i),solve();
    solve();
    return 0;
}

//starting with the name of almighty ALLAH
// Practice is the only shortcut to improve
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define vc vector
#define vi vc<int>
#define vl vc<ll>
#define mp(x,y) make_pair(x,y)
#define yes cout<<"YES"<<'\n';
#define no cout<<"NO"<<'\n';
#define tst int t;cin>>t;while(t--)
#define srt(v) sort(v.begin(),v.end());
#define rsrt(v) sort(v.rbegin(),v.rend());
#define rj ios::sync_with_stdio(false);cin.tie(0);
#define rvs(v) reverse(v.begin(),v.end());
#define F first
#define S second
#define MOD 1000000007
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define PI 2*acos(0.0)
#define pii pair<int,int>
#define fr(i,a,b) for(int i=a;i<=b;i++)
#define coutv(v) for(auto it:v)cout<<it<<' ';cout<<endl;
#define cinv(v) for(auto &it:v)cin>>it;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ld long double
#define nl '\n'
const int N = 1e5;
using namespace std;
/* #ifndef ONLINE_JUDGE
#include "algo/debug.h"
#else
#define dbg(x...)
#endif */
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int my_rand(int l, int r)
{
    return uniform_int_distribution<int>(l, r) (rng);
}
void solve()
{
    int a;
    cin >> a;;
    vector<int>v(a + 1), in(a + 1);
    for (int i = 1; i <= a; i++)
    {
        cin >> v[i];
        in[v[i]] = i;
    }
    vector<pii>an;
    for (int i = 1; i <= a; i++)
    {
        while (in[i] < a)
        {
            int nx = v[in[i] + 1];
            an.push_back({nx, i});
            swap(in[i], in[nx]);
            swap(v[in[i]], v[in[nx]]);
        }
        while (in[i] > i)
        {
            int nx = v[in[i] - 1];
            an.push_back({i, nx});
            swap(in[i], in[nx]);
            swap(v[in[i]], v[in[nx]]);
        }
    }
    cout << (int)an.size() << nl;
    for (auto &it : an)
    {
        cout << it.F << ' ' << it.S << nl;
    }
}
int main()
{
    /*#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif // ONLINE_JUDGE*/
    rj
    //int t;cin>>t;fr(i,1,t) cout<<"Case "<<i<<": ",solve();
    //ll t;scanf("%lld",&t);fr(i,1,t) printf("Case %lld: ",i),solve();
    solve();
    return 0;
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> ns;
vector<int> cur;
vector<int> pos;
vector<int> x;
vector<vector<bool>> v;
bool d = false;

void f(int it, int y, int j){
    int co = 0;
    it--;
    if(it != - 1){
        while((cur[it] <= j && !v[cur[it]][cur[it+1]] && !v[cur[it+1]][cur[it]]) || (d && it >= y)){
            v[cur[it+1]][cur[it]] = true;
            ns.push_back({cur[it+1], cur[it]});
            swap(cur[it], cur[it+1]);
            it--;
            if(it == -1){
                break;
            }
        }
    }
    it++;
    while(it < y){
        v[cur[it+1]][cur[it]] = true;
        ns.push_back({cur[it+1], cur[it]});
        swap(cur[it], cur[it+1]);
        it++;
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n; cin >> n;
    v.resize(n+1, vector<bool>(n+1));
    x.resize(n);
    pos.resize(n+1);
    for(int i = 0; i < n; i++){
        cin >> x[i];
        pos[x[i]] = i;
    }
    cur.resize(n);
    for(int i = 0; i < n; i++){
        cur[i] = i+1;
    }
    for(int y = 0; y < n; y++){
        int it = 0;
        for(int i = 0; i < n; i++){
            if(cur[i] == x[y]){
                it = i;
                break;
            }
        }
        for(int i = y; i <= it; i++){
            d = false;
            if(i == it){
                d = true;
            }
            f(i, y, cur[i]);
        }
    }
    cout << ns.size() << "\n";
    for(int y = 0; y < ns.size(); y++){
        cout << ns[y].first << " " << ns[y].second << "\n";
    }
    return 0;
}

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

#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> ns;
vector<int> cur;
vector<int> x;

void f(int it, int y, int j){
    int co = 0;
    it--;
    if(it != - 1){
        while(cur[it] <= j){
            ns.push_back({cur[it+1], cur[it]});
            swap(cur[it], cur[it+1]);
            it--;
            if(it == -1){
                break;
            }
        }
    }
    it++;
    while(it < y){
        ns.push_back({cur[it+1], cur[it]});
        swap(cur[it], cur[it+1]);
        it++;
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n; cin >> n;
    x.resize(n);
    for(int i = 0; i < n; i++){
        cin >> x[i];
    }
    cur.resize(n);
    for(int i = 0; i < n; i++){
        cur[i] = i+1;
    }
    for(int y = 0; y < n; y++){
        int it = 0;
        for(int i = 0; i < n; i++){
            if(cur[i] == x[y]){
                it = i;
                break;
            }
        }
        for(int i = y; i <= it; i++){
            f(i, y, cur[i]);
        }
    }
    cout << ns.size() << "\n";
    for(int i = 0; i < ns.size(); i++){
        cout << ns[i].first << " " << ns[i].second << "\n";
    }
    return 0;
}

//5
//1 2 3
//2 3 1
//

Answer: 1


#include<bits/stdc++.h>

using namespace std;

void solve(){
    int64_t n;
    cin >> n;
    vector<int64_t>c(n);
    for (int i = 0; i < n; i++){
        cin >> c[i];
    }
    if (n == 1){
        cout << 0;
    }
    else{
        int ans = 0;
        vector<vector<int64_t>>matrix(n + 1, vector<int64_t>(n + 1, 0));
        vector<int64_t>start(n, n);
        for (int i = 0; i < n; i++){
            start[i] = i + 1;
        }
        vector<pair<int64_t, int64_t>>ans_;
        for (int i = 0; i < n; i++){
            while(c[i] != start[i]){
                int j = i;
                while(c[j] != start[j]){
                    matrix[min(start[j], start[j + 1])][max(start[j], start[j + 1])] = 1;
                    pair<int64_t, int64_t>temp;
                    temp.first = start[j + 1];
                    temp.second = start[j];
                    ans_.push_back(temp);
                    ans += 1;
                    swap(start[j], start[j + 1]);
                    j += 1;
                }
            }
        }
        for (int i = 1; i < n; i++){
            pair<int64_t, int64_t>temp;
            int64_t j = i - 1;
            while(j >= 0){
                if (matrix[min(c[i], c[j])][max(c[i], c[j])] == 0){
                    ans += 1;
                    temp.first = c[i];
                    temp.second = c[j];
                    ans_.push_back(temp);
                    j -= 1;
                }
                else{
                    break;
                }
            }
            j += 1;
            for (int64_t z = j; z < i; z++){
                ans += 1;
                temp.first = c[z];
                temp.second = c[i];
                ans_.push_back(temp);
            }
        }
        cout << ans << endl;
        for (int i = 0; i < ans; i++){
            cout << ans_[i].first << ' ' << ans_[i].second << endl;
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0), cout.precision(16);
    int t = 1;
    // cin >> t;
    while(t--){
        solve();
        cout << endl;
    }
}

#include<bits/stdc++.h>

using namespace std;

void solve(){
    int64_t n;
    cin >> n;
    vector<int64_t>c(n);
    for (int i = 0; i < n; i++){
        cin >> c[i];
    }
    if (n == 1){
        cout << 0;
    }
    else{
        int ans = 0;
        vector<vector<int64_t>>matrix(n + 1, vector<int64_t>(n + 1, 0));
        vector<int64_t>start(n, n);
        for (int i = 0; i < n; i++){
            start[i] = i + 1;
        }
        vector<pair<int64_t, int64_t>>ans_;
        while (start[0] != n){
            int i = 0;
            int j = n - start[0];
            while (i != j){
                pair<int64_t, int64_t>temp;
                temp.first = start[i + 1];
                temp.second = start[i];
                ans_.push_back(temp);
                ans += 1;
                swap(start[i], start[i + 1]);
                i += 1;
            }
        }
        for (int i = 0; i < n; i++){
            int j = 0;
            while (start[j] != c[i]){
                j += 1;
            }
            if (j > i){
                while(j != i){
                    matrix[min(start[j], start[j - 1])][max(start[j], start[j - 1])] = 1;
                    pair<int64_t, int64_t>temp;
                    temp.first = start[j];
                    temp.second = start[j - 1];
                    ans_.push_back(temp);
                    ans += 1;
                    swap(start[j], start[j - 1]);
                    j -= 1;
                }
            }
            else{
                while(j != i){
                    matrix[min(start[j], start[j + 1])][max(start[j], start[j + 1])] = 1;
                    pair<int64_t, int64_t>temp;
                    temp.first = start[j + 1];
                    temp.second = start[j];
                    ans_.push_back(temp);
                    ans += 1;
                    swap(start[j], start[j + 1]);
                    j += 1;
                }
            }
        }
        cout << ans << endl;
        for (int i = 0; i < ans; i++){
            cout << ans_[i].first << ' ' << ans_[i].second << endl;
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0), cout.precision(16);
    int t = 1;
    // cin >> t;
    while(t--){
        solve();
        cout << endl;
    }
}

Answer: 2


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

using ll = long long;
const ll mod = 1e9 + 7;
const ll modd = 998244353;

void solve() {
    ll n;
    cin>>n;
    vector<ll>a(n), b;
    for(auto&i:a) cin>>i;
    b = a;
    vector<pair<ll, ll>>v;
    for(ll i = 0; i<n; i++) {
        for(ll j = 0; j<n-1; j++) {
            if(b[j]<b[j+1]){
                swap(b[j], b[j+1]);
                v.push_back({b[j], b[j+1]});
            }
        }
    }
    for(ll i = 0; i<n; i++) {
        for(ll j = 0; j<n-1; j++) {
            if(b[j]>b[j+1]){
                swap(b[j], b[j+1]);
                v.push_back({b[j], b[j+1]});
            }
        }
    }
    reverse(v.begin(), v.end());

    cout<<v.size()<<"\n";
    for(auto&i:v) cout<<i.first<<" "<<i.second<<"\n";
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

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

    while (t--) solve();

    return 0;
}

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

using ll = long long;
const ll mod = 1e9 + 7;
const ll modd = 998244353;

void solve() {
    ll n;
    cin>>n;
    vector<ll>a(n), b;
    for(auto&i:a) cin>>i;
    b = a;
    vector<pair<ll, ll>>v;
    for(ll i = 0; i<n; i++) {
        for(ll j = 0; j<n-1; j++) {
            if(b[j]<b[j+1]){
                swap(b[j], b[j+1]);
                v.push_back({b[j], b[j+1]});
            }
        }
    }
    for(ll i = 0; i<n; i++) {
        for(ll j = 0; j<n-1; j++) {
            if(b[j]>b[j+1]){
                swap(b[j], b[j+1]);
                v.push_back({b[j], b[j+1]});
            }
        }
    }
    reverse(v.begin(), v.end());

    cout<<v.size()<<"\n";
    for(auto&i:v) cout<<i.second<<" "<<i.first<<"\n";
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

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

    while (t--) solve();

    return 0;
}

Answer: 2


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

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

    // ll cnt=0;
    vector<pair<int,int>> ans;

    set<int> s;
    for (int i = 0; i < n; i++)
    {
        for (int j = v[i]+1; j <= n; j++)
        {
            if(s.find(j)!=s.end()) continue;
            ans.push_back({j,v[i]});
        }
        for (int j = n; j >= 1; j--)
        {
            if(j==v[i]) continue;
            if(s.find(j)!=s.end()) continue;
            ans.push_back({v[i],j});
        }
        s.insert(v[i]);
    }

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

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

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

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

    // ll cnt=0;
    vector<pair<int,int>> ans;
    for (int i = 1; i <= n; i++)
    {
        // bool flag=true;
        for (int j = 0; j < n; j++)
        {
            if(v[j]==i){
                // flag=false;
                break;
            }
            if(v[j]>i){
                ans.push_back({v[j],i});
            }
            else if(v[j]<i){
                ans.push_back({i,v[j]});
                ans.push_back({v[j],i});
            }
        }

    }

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

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

Answer: 1


#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	vector<int>arr(n+1,0);
	vector<int>brr(n);
	int cnt=0;
	for(int i=0;i<n;i++){
		cin>>brr[i];
		arr[brr[i]]=1;
		for(int j=1;j<brr[i];j++){
			if(arr[j]==0){
				cnt++;
			}
		}
		for(int j=brr[i];j<=n;j++){
			if(arr[j]==0){
				cnt++;
			}
		}
		for(int j=brr[i];j<=n;j++){
			if(arr[j]==0){
				cnt++;
			}
		}

}

	for(int i=0;i<=n;i++){
		arr[i]=0;
	}

	cout<<cnt<<endl;

	for(int i=0;i<n;i++){
		cin>>brr[i];
		arr[brr[i]]=1;

		for(int j=brr[i];j<=n;j++){
			if(arr[j]==0){
				cout<<j<<" "<<brr[i]<<endl;
			}
		}
		for(int j=brr[i];j<=n;j++){
			if(arr[j]==0){
				cout<<brr[i]<<" "<<j<<endl;
			}
		}
		for(int j=1;j<brr[i];j++){
			if(arr[j]==0){
				cout<<brr[i]<<" "<<j<<endl;
			}
		}
	}

}

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	vector<int>arr(n+1,0);
	vector<int>brr(n);
	int cnt=0;
	for(int i=0;i<n;i++){
		cin>>brr[i];
		arr[brr[i]]=1;
		for(int j=1;j<brr[i];j++){
			if(arr[j]==0){
				cnt++;
			}
		}
		for(int j=brr[i];j<=n;j++){
			if(arr[j]==0){
				cnt++;
			}
		}
		for(int j=brr[i];j<=n;j++){
			if(arr[j]==0){
				cnt++;
			}
		}

}

	for(int i=0;i<=n;i++){
		arr[i]=0;
	}

	cout<<cnt<<endl;

	for(int i=0;i<n;i++){
		cin>>brr[i];
		arr[brr[i]]=1;

		for(int j=brr[i];j<=n;j++){
			if(arr[j]==0){
				cout<<j<<" "<<brr[i]<<endl;
			}
		}
		for(int j=n;j>=brr[i];j--){
			if(arr[j]==0){
				cout<<brr[i]<<" "<<j<<endl;
			}
		}
		for(int j=brr[i]-1;j>0;j--){
			if(arr[j]==0){
				cout<<brr[i]<<" "<<j<<endl;
			}
		}
	}

}

Answer: 2