Adrenaline Rush
Time limit3sMemory limit2048 MB
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.
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 , the second car is numbered , and so on up to the last car, . 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 and , at most two overtakes happen between them: " overtakes " and/or " overtakes ".
At the end, Alice carefully writes the final order , where 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 (), the number of cars.
The second line contains a permutation (, ), the final order of the cars.
Output
The first line of the output is a single integer , the maximum possible number of overtakes.
Each of the next lines contains two integers and (, ), meaning car overtakes car . At the moment of the overtake, car is directly behind car . List the overtakes in the order they occur.
After all overtakes, the cars must finish in the order . No car may overtake the same car more than once. If several longest sequences exist, output any of them.