#P5862. [IOI 2015] sorting

[IOI 2015] sorting

Description

Aizhan has a sequence S[0],S[1],,S[N1]S[0],S[1],\cdots,S[N-1] consisting of NN distinct integers, where S[i]S[i] is in the range [0,N1][0,N-1]. 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 SS. She also knows which indices Ermek will choose. Ermek plans to take part in MM rounds of swaps, numbered from 00 to M1M-1. For each ii between 00 and M1M-1, in round ii, Ermek will swap the elements at indices X[i]X[i] and Y[i]Y[i].

Aizhan wants to sort the sequence SS 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 SS and the indices Ermek will choose, you need to find a sequence of swaps so that Aizhan can finish sorting SS. In addition, in some subtasks, you also need to find a swap sequence that is as short as possible. The problem guarantees that SS can be sorted in MM or fewer rounds.

Note that if Aizhan finds that after Ermek’s swap, the sequence SS is already sorted, then Aizhan may choose to swap two identical indices (for example 00 and 00). This way, SS is still sorted after this round, and Aizhan’s goal is achieved. Also, if the initial sequence SS is already sorted, then the minimum number of sorting rounds needed is 00.

Input Format

  • Line 11 contains a positive integer NN, the length of the sequence SS.
  • Line 22 contains NN positive integers S[0],,S[N1]S[0],\cdots,S[N-1], the initial sequence SS.
  • Line 33 contains a positive integer MM, the number of swaps Ermek plans to perform.
  • Lines 44 to M+3M+3 each contain two positive integers X[i]X[i], Y[i]Y[i], meaning that for 0iM10\le i\le M-1, in round ii, Ermek plans to swap the elements at indices X[i]X[i] and Y[i]Y[i].

Output Format

  • Line 11: the length of the swap sequence RR.
  • Line 2+i2+i (0i<R0\le i < R): P[i]P[i], Q[i]Q[i].

Note: PP and QQ are two integer arrays. Use these two arrays to report one possible swap sequence with which Aizhan can finish sorting SS. Suppose the length of this swap sequence is RR. For each ii from 00 to R1R-1, the indices Aizhan chooses in round ii will be stored in P[i]P[i] and Q[i]Q[i]. You may assume that arrays PP and QQ have each been allocated MM 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 100%100\% of the testdata, 1N2×1051 \le N\le 2 \times 10^5, 1M6×1051 \le M \le 6 \times 10^5. It is required that RR be minimized.

Translated by ChatGPT 5