Farmer John's Favorite Permutation

시간 제한2초메모리 제한1024 MB

요약
덱 양 끝에서 제거하며 남긴 N-1개의 힌트가 주어질 때, 이와 일치하는 가장 사전순으로 작은 순열을 구하거나 불가능하면 -1을 출력한다.
난이도

어려움10점 중 8점

유형
배열, 그리디, 구현, 완전 탐색
정답자
아직 제출이 없습니다

문제

Farmer John has a permutation pp of length NN (2≤N≤105)2 \leq N \leq 10^5), containing each positive integer from 11 to NN exactly once. However, Farmer Nhoj has broken into FJ's barn and disassembled pp. To not be too cruel, FN has written some hints that will help FJ reconstruct pp. While there is more than one element remaining in pp, FN does the following:

Let the remaining elements of pp be p′_1,p′_2,…,p′_np'\_1, p'\_2, \dots , p'\_n,

  • If p′_1>p′_np'\_1 > p'\_n, he writes down p′_2p'\_2 and removes p′_1p'\_1 from the permutation.
  • Otherwise, he writes down p′_n−1p'\_{n-1} and removes p′_np'\_n from the permutation.

At the end, Farmer Nhoj will have written down N−1N - 1 integers h_1,h_2,…,h_N−1h\_1, h\_2, \dots, h\_{N-1}, in that order. Given hh, Farmer John wants to enlist your help to reconstruct the lexicographically minimum pp consistent with Farmer Nhoj's hints, or determine that Farmer Nhoj must have made a mistake. Recall that if you are given two permutations pp and p′p', pp is lexicographically smaller than p′p' if p_i<p′_ip\_i < p'\_i at the first position ii where the two differ.

입력

Each input consists of TT independent test cases (1≤T≤101\le T\le 10). Each test case is described as follows:

The first line contains NN.

The second line contains N−1N - 1 integers h_1,h_2,…,h_N−1h\_1, h\_2, \dots, h\_{N-1} (1≤h_i≤N1\le h\_i\le N).

출력

Output TT lines, one for each test case.

If there is a permutation pp of 1…N1\dots N consistent with hh, output the lexicographically smallest such pp. If no such pp exists, output −1-1.

예제1

  1. 예제 1

    입력
    5
    2
    1
    2
    2
    4
    1 1 1
    4
    2 1 1
    4
    3 2 1
    
    예상 출력
    1 2
    -1
    -1
    3 1 2 4
    1 2 3 4