#abc233f. F - Swap and Sort
F - Swap and Sort
Score : points
问题描述
我们有一个由 形成的排列 。
有 种操作可供选择。第 种操作会交换 中的第 项和第 项。
是否存在一种可能,通过以任意顺序执行总共最多 次操作,将排列 按升序排序?
如果可能,请给出这样一组操作序列。否则,请报告无法完成。
以上为通义千问 qwen-max 翻译,仅供参考。
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
.
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.
update @ 2024/3/10 10:09:11