Sorting 2
Time limit1sMemory limit512 MB
Choose one swap per round after the fixed rival swap to sort the permutation in the fewest rounds, with the lexicographically smallest choice list on ties.
- Level
Hard8 of 10
- Topics
- BFS, Shortest path, Sorting
- Solved
- No attempts yet
Problem
Aizan has a sequence of length that contains each of the integers 0 to exactly once. Aizan wants to sort it into increasing order by swapping the values at two positions.
Ermac, an old friend of Aizan, also swaps the values at two positions of the same sequence. Ermac picks the positions at random and has no intention of sorting anything.
The two of them repeat rounds. In one round Ermac swaps first, then Aizan swaps. A swap means choosing two positions and exchanging the values stored there. The two chosen positions may be equal, and then the sequence stays as it is.
Aizan knows Ermac's whole plan before the first round starts. The plan covers rounds numbered 0 to . In round Ermac swaps the values at positions and .
Before a round starts, if the sequence is already in increasing order, Aizan stops there. Let be the number of rounds played up to that point. If is already sorted at the beginning, then . If the sequence is sorted right after Ermac's swap in some round, Aizan can choose the same position twice, and the sequence is sorted at the end of that round.
You are given the sequence and Ermac's plan , , . Find the two positions Aizan chooses in each round so that is as small as possible.
Input
The first line contains the length of the sequence.
The second line contains to , separated by spaces.
The third line contains the number of rounds that Ermac planned.
The -th of the next lines contains and , separated by a space.
- contains each integer from 0 to exactly once
- Aizan can always sort the sequence within rounds
Output
On the first line, print the smallest possible number of rounds .
On each of the next lines, print the positions Aizan chooses. The -th of those lines contains the two positions and chosen in round , separated by a space, with . Writing means a swap that leaves the sequence unchanged.
If several answers reach the minimum , print the one whose sequence , read in that order, is lexicographically smallest.
If is 0, print only the first line.