RiseOJ는 solved.ac와 제휴 관계가 없습니다. 티어 아이콘 © solved.ac. solved.ac
포럼
USACO0617

Farmer John's Favorite Permutation

Bronze II 브론즈 II
난이도
2s
시간 제한
256MB
메모리 제한
0
맞았습니다!!
0
제출 수
0.0%
정답률
레이팅

의견: 0

설명

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.

Problem credits: Chongtian Ma

제약

SCORING

  • Input 2: \(N\le 8\)
  • Inputs 3-6: \(N\le 100\)
  • Inputs 7-11: No additional constraints.

Problem credits: Chongtian Ma

입력 형식

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 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]\).

문제 정보

riseoj 작성

출처 올림피아드 > USACO > 2023-2024 > US Open > Bronze

태그

평가 및 의견

Farmer John's Favorite Permutation

개요
출제자 난이도 Bronze II 브론즈 II 의견 0 / 50 공개 집계 (커뮤니티 난이도, 주요 주제, 품질)는 의견이 충분히 모이면 공개됩니다.

Log in to rate problems.

개별 의견

아직 의견이 없습니다. 자격이 된다면 위 양식에서 가장 먼저 평가해 보세요.

풀이 제출

Farmer John's Favorite Permutation

게스트로 둘러보고 있습니다. 로그인하면 풀이를 제출하고 진행 상황을 확인할 수 있습니다. 로그인하고 제출하기
공개
C++20 Tab 들여쓰기 · Ctrl+/ 주석 토글 · Enter 자동 들여쓰기
1 1 1 0 공백: 4 · UTF-8