Logo
(追記) (追記ここまで)

31774번 - Farmer John's Favorite Permutation 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 1024 MB156504539.823%

문제

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

Let the remaining elements of $p$ be $p'_1, p'_2, \dots , p'_n,ドル

  • If $p'_1 > p'_n,ドル he writes down $p'_2$ and removes $p'_1$ from the permutation.
  • Otherwise, he writes down $p'_{n-1}$ and removes $p'_n$ from the permutation.

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

입력

Each input consists of $T$ independent test cases (1ドル\le T\le 10$). Each test case is described as follows:

The first line contains $N$.

The second line contains $N - 1$ integers $h_1, h_2, \dots, h_{N-1}$ (1ドル\le h_i\le N$).

출력

Output $T$ lines, one for each test case.

If there is a permutation $p$ of 1ドル\dots N$ consistent with $h,ドル output the lexicographically smallest such $p$. If no such $p$ exists, output $-1$.

제한

예제 입력 1

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

예제 출력 1

1 2
-1
-1
3 1 2 4
1 2 3 4

For the fourth test case, if $p=[3,1,2,4]$ then FN will have written down $h=[2,1,1]$.

p' = [3,1,2,4]
p_1' < p_n' -> h_1 = 2
p' = [3,1,2]
p_1' > p_n' -> h_2 = 1
p' = [1,2]
p_1' < p_n' -> h_3 = 1
p' = [1]

Note that the permutation $p=[4,2,1,3]$ would also produce the same $h,ドル but $[3,1,2,4]$ is lexiocgraphically smaller.

For the second test case, there is no $p$ consistent with $h$; both $p=[1,2]$ and $p=[2,1]$ would produce $h=[1],ドル not $h=[2]$.

힌트

출처

Olympiad > USA Computing Olympiad > 2023-2024 Season > USACO 2024 US Open Contest > Bronze 3번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /