C. Polycarp Recovers the Permutation

Problem - 1611C - Codeforces

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.

Input

The first line of the input contains an integer tt (1≤t≤1041≤t≤104) — 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≤n≤2⋅105) — the length of the array aa. The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤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⋅105.

Output

Print tt lines, each of the lines must contain the answer to the corresponding set of input data: numbers p1,p2,…,pnp1,p2,…,pn  — 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.

昨天div3的一道题

题解

        题目要求我们根据他的操作来逆推出原数组。

        以题目所给的例子为例,原p数组为3,1,4,2

        第一步取出两端中的较小值,放到a数组中,[2]

        此时p为[3,1,4],接着进行两次同样的操作后p为[4] a为[1,3,2]

        最后一个元素放在a数组左端右端都可以,如果放在右端,最后a为[1,3,2,4];

        现在给出a,要你求p。

        从上面的操作可以发现每次取出来的都是小值,所以最后一个元素必为最大值,如果a的两端的元素不为最大值则表明a不符合该操作,此时这个p不存在就输出-1.

        我们取出a中的最大值最先放到p数组的最右端中,然后再把a中的元素依次的压入p(第一个元素放到最大值的左边,依次类推,第二个元素放到第一个元素左边....), 经过我们的操作,题例中给出的a[1,3,2,4]可以得到p[2,3,1,4],你可能会说与题目给的p不同,但实际上我们求出的p经过操作后得到的a是相同的,即可得到p,该p经过题目所给的操作后可以得到a,满足题目要求

代码如下

#include<bits/stdc++.h>
using namespace std;

int main() {
	int t;
	cin >> t;
	while (t--) {
		int n, spot = 0,max=0;
		cin >> n;
		int p[200000], ans[200000];
		for (int i = 0; i < n; i++) {
			cin >> p[i];
			if (p[i] > max) {
				max = p[i];
			}
		}
		if (p[n - 1] > p[0] && p[n - 1] == max) spot = n - 1;
		else if (p[n - 1] < p[0] && p[0] == max) spot = 0;
		else if (n == 1) { cout << n<< endl; continue; }
		else {
			cout << "-1" << endl;
			continue;
		}
		if (spot ==n - 1) {
			ans[n - 1] = max;
			for (int i = 0; i <n - 1; i++) {
				ans[n - 2 - i] = p[i];
			}
		}
		if (spot ==0) {
			ans[n-1] = max;
			for (int i = 0; i < n - 1; i++) {
				ans[n-2-i] = p[i+1];
			}
		}
		for (int i = 0; i < n; i++) {
			cout << ans[i] << " ";
		}
		cout << endl;
	}
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Krito.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值