CF1759G.Restore the Permutation

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

A sequence of nn numbers is called permutation if it contains all numbers from 11 to nn exactly once. For example, the sequences [3,1,4,23, 1, 4, 2], [11] and [2,12,1] are permutations, but [1,2,11,2,1], [0,10,1] and [1,3,41,3,4] — are not.

For a permutation pp of even length nn you can make an array bb of length n2\frac{n}{2} such that:

  • bi=max⁡(p2i−1,p2i)b_i = \max(p_{2i - 1}, p_{2i}) for 1≤i≤n21 \le i \le \frac{n}{2}

For example, if pp = [2,4,3,1,5,62, 4, 3, 1, 5, 6], then:

  • b1=max⁡(p1,p2)=max⁡(2,4)=4b_1 = \max(p_1, p_2) = \max(2, 4) = 4
  • b2=max⁡(p3,p4)=max⁡(3,1)=3b_2 = \max(p_3, p_4) = \max(3,1)=3
  • b3=max⁡(p5,p6)=max⁡(5,6)=6b_3 = \max(p_5, p_6) = \max(5,6) = 6

As a result, we made bb = [4,3,6][4, 3, 6].

For a given array bb, find the lexicographically minimal permutation pp such that you can make the given array bb from it.

If bb = [4,3,64,3,6], then the lexicographically minimal permutation from which it can be made is pp = [1,4,2,3,5,61,4,2,3,5,6], since:

  • b1=max⁡(p1,p2)=max⁡(1,4)=4b_1 = \max(p_1, p_2) = \max(1, 4) = 4
  • b2=max⁡(p3,p4)=max⁡(2,3)=3b_2 = \max(p_3, p_4) = \max(2, 3) = 3
  • b3=max⁡(p5,p6)=max⁡(5,6)=6b_3 = \max(p_5, p_6) = \max(5, 6) = 6

A permutation x1,x2,…,xnx_1, x_2, \dots, x_n is lexicographically smaller than a permutation y1,y2…,yny_1, y_2 \dots, y_n if and only if there exists such ii (1≤i≤n1 \le i \le n) that x1=y1,x2=y2,…,xi−1=yi−1x_1=y_1, x_2=y_2, \dots, x_{i-1}=y_{i-1} and xi<yix_i \lt y_i.

一个包含 nn 个数的序列被称为排列(permutation),当且仅当它恰好包含从 11 到 nn 的所有整数各一次。例如,序列 [3,1,4,2][3, 1, 4, 2]、[1][1] 和 [2,1][2,1] 是排列,但 [1,2,1][1,2,1]、[0,1][0,1] 和 [1,3,4][1,3,4] 不是。

对于长度为偶数 nn 的排列 pp,可构造一个长度为 n2\frac{n}{2} 的数组 bb,使得:

  • bi=max⁡(p2i−1,p2i)b_i = \max(p_{2i - 1}, p_{2i}),其中 1≤i≤n21 \le i \le \frac{n}{2}

例如,若 p=[2,4,3,1,5,6]p = [2, 4, 3, 1, 5, 6],则:

  • b1=max⁡(p1,p2)=max⁡(2,4)=4b_1 = \max(p_1, p_2) = \max(2, 4) = 4
  • b2=max⁡(p3,p4)=max⁡(3,1)=3b_2 = \max(p_3, p_4) = \max(3,1)=3
  • b3=max⁡(p5,p6)=max⁡(5,6)=6b_3 = \max(p_5, p_6) = \max(5,6) = 6

最终得到 b=[4,3,6]b = [4, 3, 6]。

给定数组 bb,请找出字典序最小的排列 pp,使得能由该 pp 构造出给定的 bb。

若 b=[4,3,6]b = [4,3,6],则能构造出它的字典序最小的排列为 p=[1,4,2,3,5,6]p = [1,4,2,3,5,6],因为:

  • b1=max⁡(p1,p2)=max⁡(1,4)=4b_1 = \max(p_1, p_2) = \max(1, 4) = 4
  • b2=max⁡(p3,p4)=max⁡(2,3)=3b_2 = \max(p_3, p_4) = \max(2, 3) = 3
  • b3=max⁡(p5,p6)=max⁡(5,6)=6b_3 = \max(p_5, p_6) = \max(5, 6) = 6

排列 x1,x2,…,xnx_1, x_2, \dots, x_n 的字典序小于排列 y1,y2…,yny_1, y_2 \dots, y_n,当且仅当存在某个下标 ii(1≤i≤n1 \le i \le n),满足 x1=y1, x2=y2, …, xi−1=yi−1x_1=y_1,\, x_2=y_2,\, \dots,\, x_{i-1}=y_{i-1},且 xi<yix_i < y_i。

输入格式

The first line of input data contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The description of the test cases follows.

The first line of each test case contains one even integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5).

The second line of each test case contains exactly n2\frac{n}{2} integers bib_i (1≤bi≤n1 \le b_i \le n) — elements of array bb.

It is guaranteed that the sum of nn values over all test cases does not exceed 2⋅1052 \cdot 10^5.

输入数据的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示测试用例的数量。

接下来是各测试用例的描述。

每个测试用例的第一行包含一个偶数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)。

每个测试用例的第二行包含恰好 n2\frac{n}{2} 个整数 bib_i(1≤bi≤n1 \le b_i \le n),即数组 bb 的元素。

保证所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, print on a separate line:

  • lexicographically minimal permutation pp such that you can make an array bb from it;
  • or a number -1 if the permutation you are looking for does not exist.

对于每个测试用例,在单独一行中输出:

  • 字典序最小的排列 pp,使得可以从它构造出数组 bb;
  • 或者如果所求的排列不存在,则输出数字 −1-1。

输入输出样例

  • 输入#1

    6
    6
    4 3 6
    4
    2 4
    8
    8 7 2 3
    6
    6 4 2
    4
    4 4
    8
    8 7 4 5

    输出#1

    1 4 2 3 5 6 
    1 2 3 4 
    -1
    5 6 3 4 1 2 
    -1
    1 8 6 7 2 4 3 5

说明/提示

The first test case is parsed in the problem statement.

第一个测试用例已在题目描述中解析。

输入解题思路,AI测评打分。不知道怎么写?

首页