Skip to content
AlgoArena

Adrenaline Rush

Very hard 1 s 256 MBconstructive-algorithmssortingpermutationsgreedy

Alice’s friend is a big fan of the Adrenaline Rush racing competition and always strives to attend every race. However, this time, Alice is the one watching the race. To ensure her friend does not miss any important details, Alice decides to take notes on everything that happens on the track.

The first thing Alice notices before the race begins is the numbering of the cars. All the cars line up in front of the starting line in a specific order. The car closest to the line is numbered 11, the second car is numbered 22, and so on, up to the last car, which is numbered nn.

The race begins with the countdown: "Three! Two! One! Go!". Alice observes that the cars start in their original order. However, as the race progresses, their order changes. She records whenever one car overtakes another, essentially swapping places with it on the track.

During the race, Alice notices something curious: no car overtakes another more than once. In other words, for any two cars xx and yy, there can be at most two overtakes between them during the race: "xx overtakes yy" and/or "yy overtakes xx".

At the end of the race, Alice carefully writes down the final order of the cars c1,c2,…,cnc_1, c_2, \dots, c_n, where c1c_1 represents the winner of the race.

Alice’s friend, however, is only interested in the final ranking and discards all of Alice’s notes except for the final ordering. As Alice is quite curious, she wonders: What is the maximum possible number of overtakes she could have observed during the race? Your task is to help Alice answer this question.

Input

  • The first line of the input contains a single integer nn (1≤n≤10001 \le n \le 1000) — the number of cars in the race.

  • The second line contains a permutation c1,c2,…,cnc_1, c_2, \dots, c_n (1≤ci≤n1 \le c_i \le n, ci≠cjc_i \neq c_j) — the final order of the cars from 1st to nn-th place.

Output

  • The first line should contain a single integer mm — the maximum possible number of overtakes.

  • Each of the next mm lines should contain two integers xx and yy (1≤x,y≤n,x≠y1 \le x, y \le n, x \neq y), representing an overtake event where car xx overtakes car yy. This means car xx was directly behind car yy and passes it.

  • If there are multiple maximum-length valid overtake sequences, output any of them.

Notes

  • Two cars can swap places only if they are currently adjacent on the track.

  • No ordered pair of cars (x,y)(x, y) can occur as an overtake more than once (i.e., car xx cannot overtake car yy a second time).

Log in to rate this problem5.0 average · 1 ratings

Examples

Input 1
3
2 3 1
Output 1
4
2 1
3 1
3 2
2 3

Explanation

  1. Initial State: The cars start in order: [1, 2, 3].

  2. Inversion Count: Comparing the final target order [2, 3, 1] with the initial sorted order [1, 2, 3], we find 2 inversions (pairs where elements are out of their original relative positions): (2, 1) and (3, 1).

  3. Maximum Overtakes Formula:

Max Overtakes=n(n−1)−I=3×(3−1)−2=6−2=4\text{Max Overtakes} = n(n-1) - I = 3 \times (3-1) - 2 = 6 - 2 = \mathbf{4}

Step-by-Step Trace of the 4 Overtakes:

  • Step 1 (2 1): Car 2 passes Car 1. Array state becomes [2, 1, 3].

  • Step 2 (3 1): Car 3 passes Car 1. Array state becomes [2, 3, 1] (The cars are now in their final target order!).

  • Step 3 (3 2): Car 3 passes Car 2. Array state becomes [3, 2, 1].

  • Step 4 (2 3): Car 2 passes Car 3. Array state becomes [2, 3, 1] (Returns back to the final target order, maximizing the total valid overtake pairs).

Input 2
1
1
Output 2
0
Input 3
2
1 2
Output 3
2
2 1
1 2