欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1611C.Polycarp Recovers the Permutation
Polycarp Recovers the Permutation
Polycarp wrote on a whiteboard an array of length , which is a permutation of numbers from to . In other words, in each number from to occurs exactly once.
He also prepared a resulting array , which is initially empty (that is, it has a length of ).
After that, he did exactly steps. Each step looked like this:
- Look at the leftmost and rightmost elements of , and pick the smaller of the two.
- If you picked the leftmost element of , append it to the left of ; otherwise, if you picked the rightmost element of , append it to the right of .
- The picked element is erased from .
Note that on the last step, has a length of 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 both on the left and on the right (at the discretion of Polycarp).
Let's look at an example. Let , . Initially . Then:
- During the first step, the minimum is on the right (with a value of ), so after this step, and (he added the value to the right).
- During the second step, the minimum is on the left (with a value of ), so after this step, and (he added the value to the left).
- During the third step, the minimum is on the left (with a value of ), so after this step, and (he added the value to the left).
- During the fourth step, the minimum is both left and right (this value is ). Let's say Polycarp chose the right option. After this step, and (he added the value to the right).
Thus, a possible value of after steps could be .
You are given the final value of the resulting array . Find any possible initial value for that can result the given , or determine that there is no solution.
Input
The first line of the input contains an integer () — the number of test cases in the test.
Each test case consists of two lines. The first of them contains an integer () — the length of the array . The second line contains integers () — the elements of the array . All elements of the array are distinct numbers.
It is guaranteed that the sum of the values over all test cases in the test does not exceed .
Output
Print lines, each of the lines must contain the answer to the corresponding set of input data: numbers — any of the possible initial values of the array , which will lead to the given array . All elements of are distinct integers from to . Thus, if there are several solutions, print any. If there is no solution, then print -1 on the line.
Note
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, . Thus, there is only one permutation that can be the answer: . Indeed, this is the answer to this test case.
In the third test case of the example, no matter what permutation you take as , after applying the steps, the result will differ from .
Samples
4
4
1 3 2 4
1
1
5
1 3 5 4 2
3
3 2 1
3 1 4 2
1
-1
2 3 1
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |