#include <bits/stdc++.h>
#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")

using namespace std;

#define pb push_back
#define all(a) (a).begin(), (a).end()
#define endl '\n'
#define F first
#define S second
#define rs resize

using ll = long long;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using ppi = pair<int, pair<int, int>>;
using ppl = pair<ll, pair<ll, ll>>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vi>;
using vvpi = vector<vpi>;
using vpl = vector<pl>;
using vvl = vector<vl>;
using vvpl = vector<vpl>;
using vppl = vector<ppl>;

mt19937 rnd(time(nullptr));
struct it{
    it *l;
    it *r;
    int cnt, prior, val, mx;
    it(){}
    it(int x){
        l=nullptr;
        r=nullptr;
        cnt=1;
        val=mx=x;
        prior=rnd();
    }
};
typedef it *pit;
typedef pair<pit, pit> Pair;
int cnt(pit v){
    if(v) return v->cnt;
    else return 0;
}
int mx(pit v){
    if(v) return v->mx;
    else return 0;
}
void updcnt(pit v){
    if(v){
        v->cnt=cnt(v->l)+cnt(v->r)+1;
        v->mx=max(v->val, max(mx(v->l), mx(v->r)));
    }
}
pit Merge(pit l, pit r){
    if(!l) return r;
    if(!r) return l;
    if(l->prior>r->prior){
        l->r=Merge(l->r, r);
        updcnt(l);
        return l;
    }
    else{
        r->l=Merge(l, r->l);
        updcnt(r);
        return r;
    }
}

Pair Split(pit v, int pos){
    if(!v) return {nullptr, nullptr};
    if(cnt(v->l)>=pos){
        Pair t=Split(v->l, pos);
        v->l=t.S;
        updcnt(v);
        return {t.F, v};
    }
    else{
        Pair t=Split(v->r, pos-cnt(v->l)-1);
        v->r=t.F;
        updcnt(v);
        return {v, t.S};
    }
}
void Insert(pit& v, int pos, int val){
    Pair t=Split(v, pos);
    pit nd=new it(val);
    v=Merge(t.F, Merge(nd, t.S));
}
int rmq(pit& v, int l, int r){
    Pair t=Split(v, r);
    Pair q=Split(t.F, l-1);
    int res=mx(q.S);
    v=Merge(q.F, Merge(q.S, t.S));
    return res;
}
void gto(pit& v, int pos){
    int sz=cnt(v);
    Pair t=Split(v, sz-1);
    Pair q=Split(t.F, pos-1);
    v=Merge(q.F, Merge(t.S, q.S));
}
void solve()
{
    pit tr=nullptr;
    int n;
    cin>>n;
    vl a(n);
    vpl tp;
    for(int i=0;i<n;i++){
        cin>>a[i];
        int l=1, r=i+1;
        while(r-l>0){
            int md=(r+l-1)/2;
            if(rmq(tr, 1, md)>a[i]){
                r=md;
            }
            else{
                l=md+1;
            }
        }
        Insert(tr, i+1, a[i]);
        if(l!=i+1){
            tp.pb({l, i+1});
            if(i+1-l>1){
                tp.pb({l+1, i+1});
            }
            gto(tr, l);
        }
    }
    vpl tp2;
    pit tr2=nullptr;
    for(int i=0;i<n;i++){
        int pss=n-i-1;
        int l=1, r=i+1;
        while(r-l>0){
            int md=(r+l-1)/2;
            if(rmq(tr2, 1, md)>a[pss]){
                r=md;
            }
            else{
                l=md+1;
            }
        }
        Insert(tr2, i+1, a[pss]);
        if(l!=i+1){
            tp2.pb({l, i+1});
            if(i+1-l>1){
                tp2.pb({l+1, i+1});
            }
            gto(tr2, l);
        }
    }
    tp2.pb({1, n});
    ll ans1=0, ans2=0;
    for(auto i:tp){
        ans1+=i.S-i.F+1;
    }
    for(auto i:tp2){
        ans2+=i.S-i.F+1;
    }
    if(ans1>ans2){
        cout<<tp2.size()<<endl;
        for(auto i:tp2){
            cout<<i.F<<" "<<i.S<<endl;
        }
    }
    else{
        cout<<tp.size()<<endl;
        for(auto i:tp){
            cout<<i.F<<" "<<i.S<<endl;
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    //cin>>t;
    while (t--)
        solve();

}
/*
5
2
4
3
5
1
7
2 3
4 5
2 5
3 4
3 5
1 5
2 4
*/
