CF1906E.Merge Not Sort
普及+/提高
通过率:0%
时间限制:1.00s
内存限制:1024MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are currently researching the Merge Sort algorithm. Merge Sort is a sorting algorithm that is based on the principle of Divide and Conquer. It works by dividing an array into two subarrays of equal length, sorting each subarrays, then merging the sorted subarrays back together to form the final sorted array.
You are particularly interested in the merging routine. Common merge implementation will combine two subarrays by iteratively comparing their first elements, and move the smaller one to a new merged array. More precisely, the merge algorithm can be presented by the following pseudocode.
Merge(A[1..N], B[1..N]):
C = []
i = 1
j = 1
while i <= N AND j <= N:
if A[i] < B[j]:
append A[i] to C
i = i + 1
else:
append B[j] to C
j = j + 1
while i <= N:
append A[i] to C
i = i + 1
while j <= N:
append B[j] to C
j = j + 1
return C
During your research, you are keen to understand the behaviour of the merge algorithm when arrays A and B are not necessarily sorted. For example, if A=[3,1,6] and B=[4,5,2], then Merge(A,B)=[3,1,4,5,2,6].
To further increase the understanding of the merge algorithm, you decided to work on the following problem. You are given an array C of length 2⋅N such that it is a permutation of 1 to 2⋅N. Construct any two arrays A and B of the same length N, such that Merge(A,B)=C, or determine if it is impossible to do so.
你目前正在研究归并排序(Merge Sort)算法。归并排序是一种基于“分治”(Divide and Conquer)思想的排序算法。其工作原理是:将一个数组划分为两个长度相等的子数组,分别对这两个子数组进行排序,再将已排序的子数组合并成最终的有序数组。
你尤其关注其中的合并(merge)过程。常见的合并实现方式是:通过迭代比较两个子数组的首元素,将较小者移入一个新的合并数组中。更精确地,该合并算法可用如下伪代码描述:
Merge(A[1..N], B[1..N]):
C = []
i = 1
j = 1
while i <= N AND j <= N:
if A[i] < B[j]:
append A[i] to C
i = i + 1
else:
append B[j] to C
j = j + 1
while i <= N:
append A[i] to C
i = i + 1
while j <= N:
append B[j] to C
j = j + 1
return C
在研究过程中,你特别希望理解当数组 A 和 B 不一定有序时,该合并算法的行为。例如,若 A=[3,1,6] 且 B=[4,5,2],则 Merge(A,B)=[3,1,4,5,2,6]。
为进一步加深对合并算法的理解,你决定研究如下问题:给定一个长度为 2⋅N 的数组 C,且 C 是 1 到 2⋅N 的一个排列。请构造任意两个长度均为 N 的数组 A 和 B,使得 Merge(A,B)=C;若不存在这样的 A 和 B,则判定其不可能。
输入格式
The first line consists of an integer N (1≤N≤1000).
The following line consists of 2⋅N integers Ci. The array C is a permutation of 1 to 2⋅N.
第一行包含一个整数 N(1≤N≤1000)。
接下来的一行包含 2⋅N 个整数 Ci。数组 C 是 1 到 2⋅N 的一个排列。
输出格式
If it is impossible to construct two arrays A and B of length N such that Merge(A,B)=C, then output -1.
Otherwise, output the arrays A and B in two lines. The first line consists of N integers Ai. The second line consists of N integers Bi. If there are several possible answers, output any of them.
如果无法构造两个长度为 N 的数组 A 和 B,使得 Merge(A,B)=C,则输出 -1。
否则,在两行中分别输出数组 A 和 B。第一行包含 N 个整数 Ai,第二行包含 N 个整数 Bi。若存在多种可能的答案,输出任意一种即可。
输入输出样例
输入#1
3 3 1 4 5 2 6
输出#1
3 1 6 4 5 2
输入#2
4 1 2 3 4 5 6 7 8
输出#2
2 3 5 7 1 4 6 8
输入#3
2 4 3 2 1
输出#3
-1
说明/提示
Explanation for the sample input/output #1
The solution A=[3,1,4] and B=[5,2,6] is also correct.
Explanation for the sample input/output #2
The solution A=[1,2,3,4] and B=[5,6,7,8] is also correct.
样例输入/输出 #1 的说明
解 A=[3,1,4] 和 B=[5,2,6] 同样正确。
样例输入/输出 #2 的说明
解 A=[1,2,3,4] 和 B=[5,6,7,8] 同样正确。
输入解题思路,AI测评打分。不知道怎么写?