#P15075. [ICPC 2024 Chengdu R] Closest Derangement
[ICPC 2024 Chengdu R] Closest Derangement
题目描述
Blackbird has a permutation of length . He wants to find a derangement of , which means is another permutation of length satisfying for each . At the same time, should be minimized. A permutation that satisfies the above conditions is called the closest derangement of .
There may be multiple closest derangements of , and your task is to output the -th smallest closest derangement in lexicographical order. If there are fewer than closest derangements of , output .
A permutation of length refers to a sequence of length where all elements are distinct and are positive integers from to . Permutations can be sorted in lexicographical order. Let and be two distinct permutations of length . Then, if and only if at the smallest index where , it holds that .
输入格式
The first line contains an integer (), representing the number of test cases.
For each test case, the first line contains two positive integers () and (). The second line contains positive integers , representing the permutation .
It is guaranteed that the sum of over all test cases does not exceed .
输出格式
For each test case, if there are at least closest derangements, output positive integers in a single line separated by spaces, representing the -th smallest closest derangement of in lexicographical order. Otherwise, output .
2
2 2
2 1
3 2
1 2 3
-1
3 1 2
提示
For the first test case, is the only closest derangement, so output .
For the second test case, and are closest derangements of , and is larger than in lexicographical order.