CF1152E.Neko and Flashback
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 bi=min(ai,ai+1).
- Constructed an array c of length n−1, where ci=max(ai,ai+1).
- Constructed an array b′ of length n−1, where bi′=bpi.
- Constructed an array c′ of length n−1, where ci′=cpi.
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.
长度为 k 的排列是由 1 到 k 的整数构成的序列,其中每个整数恰好出现一次。例如,序列 [3,1,2] 是一个长度为 3 的排列。
当 Neko 五岁时,他构思了一个由 n 个正整数组成的数组 a,以及一个长度为 n−1 的排列 p。随后,他执行了以下操作:
- 构造一个长度为 n−1 的数组 b,其中 bi=min(ai,ai+1);
- 构造一个长度为 n−1 的数组 c,其中 ci=max(ai,ai+1);
- 构造一个长度为 n−1 的数组 b′,其中 bi′=bpi;
- 构造一个长度为 n−1 的数组 c′,其中 ci′=cpi。
例如,若数组 a 为 [3,4,6,5,7],排列 p 为 [2,4,1,3],则 Neko 将构造出如下数组:
- b=[3,4,5,5]
- c=[4,6,6,7]
- b′=[4,5,3,5]
- c′=[6,7,4,6]
接着,他将两个数组 b′ 和 c′ 写在一张纸上并遗忘了此事。14 年后,当他打扫房间时,发现了这张写有数组 b′ 和 c′ 的旧纸条。但他已记不得当初所用的数组 a 和排列 p。
如果 Neko 出错了,即不存在任何数组 a 和排列 p 能够生成给定的 b′ 和 c′,则输出 -1;否则,请帮助他恢复出任意一个可能的数组 a。
输入格式
The first line contains an integer n (2≤n≤105) — the number of elements in array a.
The second line contains n−1 integers b1′,b2′,…,bn−1′ (1≤bi′≤109).
The third line contains n−1 integers c1′,c2′,…,cn−1′ (1≤ci′≤109).
第一行包含一个整数 n(2≤n≤105)—— 表示数组 a 的元素个数。
第二行包含 n−1 个整数 b1′,b2′,…,bn−1′(1≤bi′≤109)。
第三行包含 n−1 个整数 c1′,c2′,…,cn−1′(1≤ci′≤109)。
输出格式
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 ai (1≤ai≤109), denoting the elements of the array a.
If there are multiple possible solutions, print any of them.
如果 Neko 犯了一个错误,使得不存在数组 a 和排列 p 能够生成 b′ 和 c′,则输出 -1。否则,输出 n 个正整数 ai(1≤ai≤109),表示数组 a 的元素。
若存在多个可能的解,输出任意一个即可。
输入输出样例
输入#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]
第一个示例在题目描述中已作解释。
在第三个示例中,对于 a=[3,4,5,2,1,4,3,2],一个可能的排列 p 是 [7,1,5,4,3,2,6]。此时,Neko 将构造如下数组:
- 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]
输入解题思路,AI测评打分。不知道怎么写?