codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1611C.md (3656B)


      1 Source: https://codeforces.com/problemset/problem/1611/C
      2 
      3 C. Polycarp Recovers the Permutation
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Polycarp wrote on a whiteboard an array $p$ of length $n$, which is a permutation of numbers from $1$ to $n$. In other words, in $p$ each number from $1$ to $n$ occurs exactly once.
     22 
     23 He also prepared a resulting array $a$, which is initially empty (that is, it has a length of $0$).
     24 
     25 After that, he did exactly $n$ steps. Each step looked like this:
     26 
     27 * Look at the leftmost and rightmost elements of $p$, and pick the smaller of the two.
     28 * If you picked the leftmost element of $p$, append it to the left of $a$; otherwise, if you picked the rightmost element of $p$, append it to the right of $a$.
     29 * The picked element is erased from $p$.
     30 
     31 Note that on the last step, $p$ has a length of $1$ 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 $a$ both on the left and on the right (at the discretion of Polycarp).
     32 
     33 Let's look at an example. Let $n=4$, $p=[3, 1, 4, 2]$. Initially $a=[]$. Then:
     34 
     35 * During the first step, the minimum is on the right (with a value of $2$), so after this step, $p=[3,1,4]$ and $a=[2]$ (he added the value $2$ to the right).
     36 * During the second step, the minimum is on the left (with a value of $3$), so after this step, $p=[1,4]$ and $a=[3,2]$ (he added the value $3$ to the left).
     37 * During the third step, the minimum is on the left (with a value of $1$), so after this step, $p=[4]$ and $a=[1,3,2]$ (he added the value $1$ to the left).
     38 * During the fourth step, the minimum is both left and right (this value is $4$). Let's say Polycarp chose the right option. After this step, $p=[]$ and $a=[1,3,2,4]$ (he added the value $4$ to the right).
     39 
     40 Thus, a possible value of $a$ after $n$ steps could be $a=[1,3,2,4]$.
     41 
     42 You are given the final value of the resulting array $a$. Find any possible initial value for $p$ that can result the given $a$, or determine that there is no solution.
     43 
     44 Input
     45 
     46 The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
     47 
     48 Each test case consists of two lines. The first of them contains an integer $n$ ($1 \le n \le 2\cdot10^5$) — the length of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the elements of the array $a$. All elements of the $a$ array are distinct numbers.
     49 
     50 It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$.
     51 
     52 Output
     53 
     54 Print $t$ lines, each of the lines must contain the answer to the corresponding set of input data: numbers $p_1, p_2, \dots, p_n$  — any of the possible initial values of the array $p$, which will lead to the given array $a$. All elements of $p$ are distinct integers from $1$ to $n$. Thus, if there are several solutions, print any. If there is no solution, then print -1 on the line.
     55 
     56 Example
     57 
     58 Input
     59 
     60 ```
     61 4
     62 4
     63 1 3 2 4
     64 1
     65 1
     66 5
     67 1 3 5 4 2
     68 3
     69 3 2 1
     70 ```
     71 
     72 Output
     73 
     74 ```
     75 3 1 4 2
     76 1
     77 -1
     78 2 3 1
     79 ```
     80 
     81 Note
     82 
     83 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.
     84 
     85 In the second test case, $n=1$. Thus, there is only one permutation that can be the answer: $p=[1]$. Indeed, this is the answer to this test case.
     86 
     87 In the third test case of the example, no matter what permutation you take as $p$, after applying the $n$ steps, the result will differ from $a=[1, 3, 5, 4, 2]$.