#P15075. [ICPC 2024 Chengdu R] Closest Derangement

[ICPC 2024 Chengdu R] Closest Derangement

题目描述

Blackbird has a permutation pp of length nn. He wants to find a derangement qq of pp, which means qq is another permutation of length nn satisfying qipiq_i\neq p_i for each i=1,2,,ni=1,2,\ldots,n. At the same time, i=1npiqi\sum_{i=1}^{n}|p_i-q_i| should be minimized. A permutation qq that satisfies the above conditions is called the closest derangement of pp.

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 aibia_i \neq b_i, it holds that ai<bia_i < b_i.

输入格式

The first line contains an integer TT (1T1041\le T\le 10^4), representing the number of test cases.

For each test case, the first line contains two positive integers nn (2n21052\le n \le 2 \cdot 10^5) and kk (1k1091\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 kk closest derangements, output nn positive integers q1,q2,,qnq_1, q_2, \ldots, q_n in a single line separated by spaces, representing the kk-th smallest closest derangement of pp in lexicographical order. Otherwise, output 1-1.

2
2 2
2 1
3 2
1 2 3
-1
3 1 2

提示

For the first test case, [1,2][1,2] is the only closest derangement, so output 1-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.