Under construction

Still being built. Things may change

Adrenaline Rush

Time limit3sMemory limit2048 MB

Summary
Given the final order of cars, find the longest sequence of overtakes in which each pair of cars overtakes each other at most twice, and print that sequence.
Level

Medium7 of 10

Topics
Array, Sorting, Greedy
Solved
No attempts yet

Problem

Alice's friend is a big fan of the Adrenaline Rush racing competition and tries to attend every race. This time, Alice is the one watching. So that her friend misses no important details, Alice decides to note everything that happens on the track.

Before the race starts, the first thing Alice notices is the car numbering. All cars line up in front of the starting line in a fixed order. The car closest to the line is numbered 11, the second car is numbered 22, and so on up to the last car, nn. How convenient, Alice thinks.

The race begins with a countdown: "Three! Two! One! Go!" Alice sees the cars start in their original order. As the race goes on, the order changes. Each time one car overtakes another, the two swap places on the track, and Alice records it.

Alice notices something unusual during the race. No car overtakes another car more than once. For any two cars xx and yy, at most two overtakes happen between them: "xx overtakes yy" and/or "yy overtakes xx".

At the end, Alice carefully writes the final order c1,c2,…,cnc_1, c_2, \ldots, c_n, where c1c_1 is the winner.

Alice's friend only cares about the final ranking and throws away all of Alice's notes except that order. Alice is curious, though. What is the longest possible sequence of overtakes she could have observed? Help her find out.

Input

The first line contains a single integer nn (1≤n≤10001 \le n \le 1000), the number of cars.

The second line contains a permutation c1,c2,…,cnc_1, c_2, \ldots, c_n (1≤ci≤n1 \le c_i \le n, ci≠cjc_i \ne c_j), the final order of the cars.

Output

The first line of the output is a single integer mm, the maximum possible number of overtakes.

Each of the next mm lines contains two integers xx and yy (1≤x,y≤n1 \le x, y \le n, x≠yx \ne y), meaning car xx overtakes car yy. At the moment of the overtake, car xx is directly behind car yy. List the overtakes in the order they occur.

After all mm overtakes, the cars must finish in the order c1,c2,…,cnc_1, c_2, \ldots, c_n. No car xx may overtake the same car yy more than once. If several longest sequences exist, output any of them.

Examples3

  1. Example 1

    Input
    3
    2 3 1
    
    Expected output
    4
    2 1
    3 1
    3 2
    2 3
    
  2. Example 2

    Input
    1
    1
    
    Expected output
    0
    
  3. Example 3

    Input
    2
    1 2
    
    Expected output
    2
    2 1
    1 2