QOJ.ac

QOJ

Límite de tiempo: 3.0 s Límite de memoria: 1024 MB Puntuación total: 100 Hackeable ✓

#9538. 最近错排

Estadísticas

Blackbird 有一个长度为 $n$ 的排列 $p$。他想要找到 $p$ 的一个错排 $q$,即对于所有的 $i = 1, 2, \dots, n$,满足 $q_i \neq p_i$。同时,需要使 $\sum_{i=1}^n |p_i - q_i|$ 最小化。满足上述条件的排列 $q$ 被称为 $p$ 的最近错排。

$p$ 的最近错排可能不止一个,你的任务是输出字典序第 $k$ 小的最近错排。如果 $p$ 的最近错排少于 $k$ 个,则输出 $-1$。

长度为 $n$ 的排列是指一个长度为 $n$ 的序列,其中所有元素均为 $1$ 到 $n$ 之间的不同正整数。排列可以按字典序排序。设 $a$ 和 $b$ 是两个不同的长度为 $n$ 的排列。当且仅当在 $a_i \neq b_i$ 的最小下标 $i$ 处,$a_i < b_i$ 时,称 $a < b$。

输入格式

第一行包含一个整数 $T$ ($1 \le T \le 10^4$),表示测试用例的数量。

对于每个测试用例,第一行包含两个正整数 $n$ ($2 \le n \le 2 \cdot 10^5$) 和 $k$ ($1 \le k \le 10^9$)。

第二行包含 $n$ 个正整数 $p_1, p_2, \dots, p_n$,表示排列 $p$。

保证所有测试用例中 $n$ 的总和不超过 $10^6$。

输出格式

对于每个测试用例,如果至少存在 $k$ 个最近错排,则在一行中输出 $n$ 个由空格分隔的正整数 $q_1, q_2, \dots, q_n$,表示字典序第 $k$ 小的最近错排。否则,输出 $-1$。

样例

样例输入 1

2
2 2
2 1
3 2
1 2 3

样例输出 1

-1
3 1 2

说明

对于第一个测试用例,$[1, 2]$ 是唯一的最近错排,因此输出 $-1$。

对于第二个测试用例,$[2, 3, 1]$ 和 $[3, 1, 2]$ 是 $p$ 的最近错排,且在字典序中 $[3, 1, 2]$ 大于 $[2, 3, 1]$。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.