#abc233f. [abc233_f]Swap and Sort
[abc233_f]Swap and Sort
Problem Statement
We have a permutation of .
There are kinds of operations available to you. Operation swaps the -th and -th elements of .
Is it possible to sort in ascending order by doing at most operations in total in any order?
If it is possible, give one such sequence of operations. Otherwise, report so.
Constraints
- is a permutation of .
- $1\\leq M \\leq \\min(2\\times 10^5, \\frac{N(N-1)}{2})$
- if .
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
If it is possible to sort in ascending order, print the following:
Here, represents the number of operations to do, and means the -th operation to do is Operation .
Note that must hold.
If it is impossible to sort in ascending order, print -1
.
Sample Input 1
6
5 3 2 4 6 1
4
1 5
5 6
1 2
2 3
Sample Output 1
3
4 2 1
changes as follows: $(5,3,2,4,6,1)\\to (5,2,3,4,6,1)\\to (5,2,3,4,1,6)\\to (1,2,3,4,5,6)$.
Sample Input 2
5
3 4 1 2 5
2
1 3
2 5
Sample Output 2
-1
We cannot sort in ascending order.
Sample Input 3
4
1 2 3 4
6
1 2
1 3
1 4
2 3
2 4
3 4
Sample Output 3
0
may already be sorted in ascending order.
Additionally, here is another accepted output:
4
5 5 5 5
Note that it is not required to minimize the number of operations.