#P5862. [IOI 2015] sorting
[IOI 2015] sorting
Description
Aizhan has a sequence consisting of distinct integers, where is in the range . Aizhan tries to sort this sequence in increasing order by swapping some pairs of elements. Aizhan’s friend Ermek also wants to swap some pairs of elements, and Ermek’s swaps may not help Aizhan sort the sequence.
Ermek and Aizhan plan to modify this sequence through several rounds. In each round, Ermek makes one swap first, and then Aizhan makes another swap. More precisely, the person who performs a swap chooses two valid indices and swaps the elements at those indices. Note that the two indices may be the same. If they are equal, it is a swap of an element with itself and does not change the sequence.
Aizhan knows that Ermek does not care about sorting the sequence . She also knows which indices Ermek will choose. Ermek plans to take part in rounds of swaps, numbered from to . For each between and , in round , Ermek will swap the elements at indices and .
Aizhan wants to sort the sequence in increasing order. Before each round, if Aizhan sees that the current sequence is already sorted in increasing order, she will stop the sorting process. Given the initial sequence and the indices Ermek will choose, you need to find a sequence of swaps so that Aizhan can finish sorting . In addition, in some subtasks, you also need to find a swap sequence that is as short as possible. The problem guarantees that can be sorted in or fewer rounds.
Note that if Aizhan finds that after Ermek’s swap, the sequence is already sorted, then Aizhan may choose to swap two identical indices (for example and ). This way, is still sorted after this round, and Aizhan’s goal is achieved. Also, if the initial sequence is already sorted, then the minimum number of sorting rounds needed is .
Input Format
- Line contains a positive integer , the length of the sequence .
- Line contains positive integers , the initial sequence .
- Line contains a positive integer , the number of swaps Ermek plans to perform.
- Lines to each contain two positive integers , , meaning that for , in round , Ermek plans to swap the elements at indices and .
Output Format
- Line : the length of the swap sequence .
- Line (): , .
Note: and are two integer arrays. Use these two arrays to report one possible swap sequence with which Aizhan can finish sorting . Suppose the length of this swap sequence is . For each from to , the indices Aizhan chooses in round will be stored in and . You may assume that arrays and have each been allocated elements.
5
4 3 2 1 0
6
0 1
1 2
2 3
3 4
0 1
1 2
3
0 4
1 3
3 4
5
3 0 4 2 1
5
1 1
4 0
2 3
1 4
0 4
3
1 4
4 2
2 2
Hint
For of the testdata, , . It is required that be minimized.
Translated by ChatGPT 5
京公网安备 11011102002149号