CF1611C.Polycarp Recovers the Permutation

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Polycarp wrote on a whiteboard an array pp of length nn, which is a permutation of numbers from 11 to nn. In other words, in pp each number from 11 to nn occurs exactly once.

He also prepared a resulting array aa, which is initially empty (that is, it has a length of 00).

After that, he did exactly nn steps. Each step looked like this:

  • Look at the leftmost and rightmost elements of pp, and pick the smaller of the two.
  • If you picked the leftmost element of pp, append it to the left of aa; otherwise, if you picked the rightmost element of pp, append it to the right of aa.
  • The picked element is erased from pp.

Note that on the last step, pp has a length of 11 and its minimum element is both leftmost and rightmost. In this case, Polycarp can choose what role the minimum element plays. In other words, this element can be added to aa both on the left and on the right (at the discretion of Polycarp).

Let's look at an example. Let n=4n=4, p=[3,1,4,2]p=[3, 1, 4, 2]. Initially a=[]a=[]. Then:

  • During the first step, the minimum is on the right (with a value of 22), so after this step, p=[3,1,4]p=[3,1,4] and a=[2]a=[2] (he added the value 22 to the right).
  • During the second step, the minimum is on the left (with a value of 33), so after this step, p=[1,4]p=[1,4] and a=[3,2]a=[3,2] (he added the value 33 to the left).
  • During the third step, the minimum is on the left (with a value of 11), so after this step, p=[4]p=[4] and a=[1,3,2]a=[1,3,2] (he added the value 11 to the left).
  • During the fourth step, the minimum is both left and right (this value is 44). Let's say Polycarp chose the right option. After this step, p=[]p=[] and a=[1,3,2,4]a=[1,3,2,4] (he added the value 44 to the right).

Thus, a possible value of aa after nn steps could be a=[1,3,2,4]a=[1,3,2,4].

You are given the final value of the resulting array aa. Find any possible initial value for pp that can result the given aa, or determine that there is no solution.

Polycarp 在白板上写下一个长度为 nn 的数组 pp,该数组是 11 到 nn 这些数的一个排列。换言之,pp 中每个从 11 到 nn 的数恰好出现一次。

他还准备了一个结果数组 aa,其初始为空(即长度为 00)。

随后,他恰好执行了 nn 步操作。每一步操作如下:

  • 观察 pp 的最左端和最右端元素,并从中选出较小者;
  • 若选出的是 pp 的最左端元素,则将其添加到 aa 的左侧;否则(即选出的是 pp 的最右端元素),则将其添加到 aa 的右侧;
  • 将被选出的元素从 pp 中删除。

注意:在最后一步中,pp 的长度为 11,此时其唯一元素既是左端点也是右端点,因此该元素自然是最小值。此时 Polycarp 可自由决定该元素是添加到 aa 的左侧还是右侧。

我们来看一个例子:设 n=4n=4,p=[3,1,4,2]p=[3, 1, 4, 2],初始时 a=[]a=[]。之后:

  • 第一步:左右两端分别为 33 和 22,较小者为右端的 22,因此此步后 p=[3,1,4]p=[3,1,4],a=[2]a=[2](将 22 添加至右侧);
  • 第二步:左右两端分别为 33 和 44,较小者为左端的 33,因此此步后 p=[1,4]p=[1,4],a=[3,2]a=[3,2](将 33 添加至左侧);
  • 第三步:左右两端分别为 11 和 44,较小者为左端的 11,因此此步后 p=[4]p=[4],a=[1,3,2]a=[1,3,2](将 11 添加至左侧);
  • 第四步:pp 中仅剩 44,它同时是最左和最右元素。假设 Polycarp 选择将其添加至右侧,则此步后 p=[]p=[],a=[1,3,2,4]a=[1,3,2,4](将 44 添加至右侧)。

因此,经过 nn 步后,aa 的一种可能取值为 a=[1,3,2,4]a=[1,3,2,4]。

现给你最终得到的数组 aa,请找出任意一个可能的初始数组 pp,使得按上述规则操作后能得到该 aa;若不存在这样的 pp,则判定无解。

输入格式

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

Each test case consists of two lines. The first of them contains an integer nn (1≤n≤2⋅1051 \le n \le 2\cdot10^5) — the length of the array aa. The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤n1 \le a_i \le n) — the elements of the array aa. All elements of the aa array are distinct numbers.

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

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

每个测试用例由两行组成。第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2\cdot10^5)——数组 aa 的长度。第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤n1 \le a_i \le n)——数组 aa 的元素。数组 aa 的所有元素互不相同。

保证所有测试用例的 nn 值之和不超过 2⋅1052\cdot10^5。

输出格式

Print tt lines, each of the lines must contain the answer to the corresponding set of input data: numbers p1,p2,…,pnp_1, p_2, \dots, p_n — any of the possible initial values of the array pp, which will lead to the given array aa. All elements of pp are distinct integers from 11 to nn. Thus, if there are several solutions, print any. If there is no solution, then print -1 on the line.

输出 tt 行,每行包含对应输入数据组的答案:数字 p1,p2,…,pnp_1, p_2, \dots, p_n —— 即数组 pp 的任意一种可能的初始取值,使得其最终得到给定的数组 aa。pp 的所有元素均为 11 到 nn 之间的互异整数。因此,若存在多个解,输出任意一个即可;若无解,则在该行输出 -1。

输入输出样例

  • 输入#1

    4
    4
    1 3 2 4
    1
    1
    5
    1 3 5 4 2
    3
    3 2 1

    输出#1

    3 1 4 2
    1
    -1
    2 3 1

说明/提示

The first test case in the example is clarified in the main section of the problem statement. There may be other correct answers for this test set.

In the second test case, n=1n=1. Thus, there is only one permutation that can be the answer: p=[1]p=[1]. Indeed, this is the answer to this test case.

In the third test case of the example, no matter what permutation you take as pp, after applying the nn steps, the result will differ from a=[1,3,5,4,2]a=[1, 3, 5, 4, 2].

示例中的第一个测试用例已在题目陈述的主部分中阐明。对于该测试集,可能存在其他正确的答案。

在第二个测试用例中,n=1n=1。因此,唯一可能作为答案的排列是 p=[1]p=[1]。事实上,这正是该测试用例的答案。

在示例的第三个测试用例中,无论你选取何种排列作为 pp,经过 nn 步操作后,结果都将不同于 a=[1,3,5,4,2]a=[1, 3, 5, 4, 2]。

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

首页