#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 pp, and your task is to output the kk-th smallest closest derangement in lexicographical order. If there are fewer than kk closest derangements of pp, output −1-1.

A permutation of length nn refers to a sequence of length nn where all elements are distinct and are positive integers from 11 to nn. Permutations can be sorted in lexicographical order. Let aa and bb be two distinct permutations of length nn. Then, a<ba < b if and only if at the smallest index ii where ai≠bia_i \neq b_i, it holds that ai<bia_i < b_i.

输入格式

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 nn (2≤n≤2⋅1052\le n \le 2 \cdot 10^5) and kk (1≤k≤1091\le k \le 10^9). The second line contains nn positive integers p1,p2,…,pnp_1, p_2, \ldots, p_n, representing the permutation pp.

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6.

输出格式

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, [2,3,1][2,3,1] and [3,1,2][3,1,2] are closest derangements of pp, and [3,1,2][3,1,2] is larger than [2,3,1][2,3,1] in lexicographical order. 标签: P15075|2024|ICPC|成都

来源

P15075|[ICPC 2024 Chengdu R] Closest Derangement