Hakuna Matata Sort
Time limit2sMemory limit1024 MB
Given a permutation of masses 1 to n, output swaps between beetles whose masses differ by at most one that sort the row in increasing order.
- Level
Medium5 of 10
- Topics
- Sorting, Implementation, Array
- Solved
- No attempts yet
Problem
After a pleasant walk through the African valley, Timon and Pumbaa decided to have lunch. They took beetles out from under a rock and placed them on a nearby log. The beetles have distinct integer masses from to . To make lunch more enjoyable, Pumbaa suggested eating the beetles in increasing order of mass.
To sort the beetles by increasing mass, Timon and Pumbaa use Hakuna Matata sort. In each move, they pick two beetles whose masses differ by at most one, and then swap them. For example, if Pumbaa picks the beetle with mass 2, Timon can take the beetle with mass 1 or mass 3. The two friends have used this sort many times, so they know it always works.
Zazu watched the whole process closely. He had never seen anything like it and wondered how beetles could be sorted this way. Your task is to write a program that takes the initial arrangement of the beetles and prints a sequence of moves that sorts them by increasing mass.
Input
The first line contains the number of beetles (). The second line contains distinct integers () separated by spaces, where is the mass of the beetle with number .
Output
Print the number of moves on the first line. On each of the next lines, print two integers and , meaning that on move Pumbaa took the beetle at position and Timon took the beetle at position . Any sequence of moves that uses at most 50000 operations is accepted.