#7374. [ICPC 2024 Chengdu R] Closest Derangement
[ICPC 2024 Chengdu R] Closest Derangement
[ICPC 2024 Chengdu R] Closest Derangement
题目描述
Blackbird has a permutation $p$ of length $n$. He wants to find a derangement $q$ of $p$, which means $q$ is another permutation of length $n$ satisfying $q_i\neq p_i$ for each $i=1,2,\ldots,n$. At the same time, $\sum_{i=1}^{n}|p_i-q_i|$ should be minimized. A permutation $q$ that satisfies the above conditions is called the closest derangement of $p$.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 $T$ ($1\le T\le 10^4$), 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 $k$ closest derangements, output $n$ positive integers $q_1, q_2, \ldots, q_n$ in a single line separated by spaces, representing the $k$-th smallest closest derangement of $p$ in lexicographical order. Otherwise, output $-1$.2
2 2
2 1
3 2
1 2 3
-1
3 1 2
提示
For the first test case, $[1,2]$ is the only closest derangement, so output $-1$.For the second test case, and are closest derangements of , and is larger than in lexicographical order. 标签: P15075|2024|ICPC|成都