#P7650. [BalticOI 2007] Ranklist Sorting (Day 1)

[BalticOI 2007] Ranklist Sorting (Day 1)

Description

In a contest, you are given the scores of several players. Your task is to create a ranklist of the players sorted by decreasing score.
Unfortunately, the data structure used for the ranklist supports only one operation: move a player from position ii to position jj without changing the relative order of the other players. If i>ji > j, then the players in positions between jj and i1i - 1 increase their positions by 11; otherwise, if i<ji < j, then the players in positions between i+1i + 1 and jj decrease their positions by 11.
This operation takes ii steps to locate the player to be moved, and jj steps to locate the position to move him or her to, so the total cost of moving a player from position ii to position jj is i+ji + j. Positions are numbered starting from 11.
Determine a sequence of moves to create the ranklist such that the sum of move costs is minimized.

Input Format

The first line contains an integer nn, the number of players.
Each of the next nn lines contains a non-negative integer sis_i, the score of a player in the current order. You may assume that all scores are distinct.

Output Format

Output the number of moves used to create the ranklist on the first line. The following lines should specify the moves in the order they are applied. Each move should be described on one line containing two integers ii and jj, meaning that the player at position ii is moved to position jj. The numbers ii and jj must be separated by a single space.

5
20
30
5
15
10
2
2 1
3 5

Hint

Constraints

For 30%30 \% of the testdata, n10n \le 10, 0si1060 \le s_i \le 10^6.
For 100%100 \% of the testdata, 2n1032 \le n \le 10^3, 0si1060 \le s_i \le 10^6.

Notes

From Baltic Olympiad in Informatics 2007, Day 1:sorting.
Translated and organized by @求学的企鹅.

Translated by ChatGPT 5