atcoder#ABC233F. [ABC233F] Swap and Sort
[ABC233F] Swap and Sort
Score : points
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 .
- 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
.
6
5 3 2 4 6 1
4
1 5
5 6
1 2
2 3
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)$.
5
3 4 1 2 5
2
1 3
2 5
-1
We cannot sort in ascending order.
4
1 2 3 4
6
1 2
1 3
1 4
2 3
2 4
3 4
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.