A12487 | Neko and Flashback
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
A permutation of length $k$ is a sequence of $k$ integers from $1$ to $k$ containing each integer exactly once. For example, the sequence $[3, 1, 2]$ is a permutation of length $3$ .
When Neko was five, he thought of an array $a$ of $n$ positive integers and a permutation $p$ of length $n - 1$ . Then, he performed the following:
- Constructed an array $b$ of length $n-1$ , where $b_i = \min(a_i, a_{i+1})$ .
- Constructed an array $c$ of length $n-1$ , where $c_i = \max(a_i, a_{i+1})$ .
- Constructed an array $b'$ of length $n-1$ , where $b'_i = b_{p_i}$ .
- Constructed an array $c'$ of length $n-1$ , where $c'_i = c_{p_i}$ .
For example, if the array $a$ was $[3, 4, 6, 5, 7]$ and permutation $p$ was $[2, 4, 1, 3]$ , then Neko would have constructed the following arrays:
- $b = [3, 4, 5, 5]$
- $c = [4, 6, 6, 7]$
- $b' = [4, 5, 3, 5]$
- $c' = [6, 7, 4, 6]$
Then, he wrote two arrays $b'$ and $c'$ on a piece of paper and forgot about it. 14 years later, when he was cleaning up his room, he discovered this old piece of paper with two arrays $b'$ and $c'$ written on it. However he can't remember the array $a$ and permutation $p$ he used.
In case Neko made a mistake and there is no array $a$ and permutation $p$ resulting in such $b'$ and $c'$ , print -1. Otherwise, help him recover any possible array $a$ .
When Neko was five, he thought of an array $a$ of $n$ positive integers and a permutation $p$ of length $n - 1$ . Then, he performed the following:
- Constructed an array $b$ of length $n-1$ , where $b_i = \min(a_i, a_{i+1})$ .
- Constructed an array $c$ of length $n-1$ , where $c_i = \max(a_i, a_{i+1})$ .
- Constructed an array $b'$ of length $n-1$ , where $b'_i = b_{p_i}$ .
- Constructed an array $c'$ of length $n-1$ , where $c'_i = c_{p_i}$ .
For example, if the array $a$ was $[3, 4, 6, 5, 7]$ and permutation $p$ was $[2, 4, 1, 3]$ , then Neko would have constructed the following arrays:
- $b = [3, 4, 5, 5]$
- $c = [4, 6, 6, 7]$
- $b' = [4, 5, 3, 5]$
- $c' = [6, 7, 4, 6]$
Then, he wrote two arrays $b'$ and $c'$ on a piece of paper and forgot about it. 14 years later, when he was cleaning up his room, he discovered this old piece of paper with two arrays $b'$ and $c'$ written on it. However he can't remember the array $a$ and permutation $p$ he used.
In case Neko made a mistake and there is no array $a$ and permutation $p$ resulting in such $b'$ and $c'$ , print -1. Otherwise, help him recover any possible array $a$ .
输入格式
The first line contains an integer $n$ ( $2 \leq n \leq 10^5$ ) — the number of elements in array $a$ .
The second line contains $n-1$ integers $b'_1, b'_2, \ldots, b'_{n-1}$ ( $1 \leq b'_i \leq 10^9$ ).
The third line contains $n-1$ integers $c'_1, c'_2, \ldots, c'_{n-1}$ ( $1 \leq c'_i \leq 10^9$ ).
The second line contains $n-1$ integers $b'_1, b'_2, \ldots, b'_{n-1}$ ( $1 \leq b'_i \leq 10^9$ ).
The third line contains $n-1$ integers $c'_1, c'_2, \ldots, c'_{n-1}$ ( $1 \leq c'_i \leq 10^9$ ).
输出格式
If Neko made a mistake and there is no array $a$ and a permutation $p$ leading to the $b'$ and $c'$ , print -1. Otherwise, print $n$ positive integers $a_i$ ( $1 \le a_i \le 10^9$ ), denoting the elements of the array $a$ .
If there are multiple possible solutions, print any of them.
If there are multiple possible solutions, print any of them.
输入输出样例
输入 #1
5 4 5 3 5 6 7 4 6
输出 #1
3 4 6 5 7
输入 #2
3 2 4 3 2
输出 #2
-1
输入 #3
8 2 3 1 1 2 4 3 3 4 4 2 5 5 4
输出 #3
3 4 5 2 1 4 3 2
The first example is explained is the problem statement.
In the third example, for $a = [3, 4, 5, 2, 1, 4, 3, 2]$ , a possible permutation $p$ is $[7, 1, 5, 4, 3, 2, 6]$ . In that case, Neko would have constructed the following arrays:
- $b = [3, 4, 2, 1, 1, 3, 2]$
- $c = [4, 5, 5, 2, 4, 4, 3]$
- $b' = [2, 3, 1, 1, 2, 4, 3]$
- $c' = [3, 4, 4, 2, 5, 5, 4]$
In the third example, for $a = [3, 4, 5, 2, 1, 4, 3, 2]$ , a possible permutation $p$ is $[7, 1, 5, 4, 3, 2, 6]$ . In that case, Neko would have constructed the following arrays:
- $b = [3, 4, 2, 1, 1, 3, 2]$
- $c = [4, 5, 5, 2, 4, 4, 3]$
- $b' = [2, 3, 1, 1, 2, 4, 3]$
- $c' = [3, 4, 4, 2, 5, 5, 4]$
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted