Time limit
1s
Memory limit
128 MB
A developer is building a feature that shows user rankings by sorting scores in descending order.
The data structure that stores the scores supports only one operation: remove the current i-th user and insert that user at the current j-th position. The relative order of all other users does not change.
If i > j, the users that were from position j through position i - 1 each move one position back. If i < j, the users that were from position i + 1 through position j each move one position forward.
One operation takes i steps to find the user to move and j steps to find the destination, so moving the current i-th user to the current j-th position costs i + j. Positions and ranks are 1-indexed.
Given the scores in their current order, find a sequence of moves with minimum total cost that sorts all users by descending score.
The first line contains the number of stored scores n. (2 <= n <= 1000)
The next input values are n scores s_i in their current order. (0 <= s_i <= 1,000,000)
No two scores are equal.
Print one sequence of moves whose total cost is minimum.
On the first line, print the number of moves k. On each of the next k lines, print two integers i and j in the order the moves are performed. If the current i-th score is moved to the current j-th position, print i j.