QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 2048 MB Total points: 100

#10562. Merge Not Sort

統計

你正在研究归并排序算法。归并排序是一种基于分治思想的排序算法。它的工作原理是将一个数组分成两个等长的子数组,分别对每个子数组进行排序,然后将排好序的子数组合并在一起,形成最终的有序数组。

你对合并过程特别感兴趣。常见的合并实现方式是通过迭代比较两个子数组的首元素,并将较小的一个移动到新的合并数组中。更准确地说,合并算法可以用以下伪代码表示:

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]$,那么 $\text{Merge}(A, B) = [3, 1, 4, 5, 2, 6]$。

为了进一步加深对合并算法的理解,你决定解决以下问题:给定一个长度为 $2 \cdot N$ 的数组 $C$,它是 $1$ 到 $2 \cdot N$ 的一个排列。请构造任意两个长度均为 $N$ 的数组 $A$ 和 $B$,使得 $\text{Merge}(A, B) = C$,或者确定这是否不可能实现。

输入格式

第一行包含一个整数 $N$ ($1 \le N \le 1000$)。

接下来一行包含 $2 \cdot N$ 个整数 $C_i$。数组 $C$ 是 $1$ 到 $2 \cdot N$ 的一个排列。

输出格式

如果无法构造出两个长度为 $N$ 的数组 $A$ 和 $B$ 使得 $\text{Merge}(A, B) = C$,则输出 -1。

否则,分两行输出数组 $A$ 和 $B$。第一行包含 $N$ 个整数 $A_i$。第二行包含 $N$ 个整数 $B_i$。如果存在多种可能的答案,输出其中任意一个即可。

样例

输入 1

3
3 1 4 5 2 6

输出 1

3 1 6
4 5 2

说明 1

解 $A = [3, 1, 4]$ 和 $B = [5, 2, 6]$ 也是正确的。

输入 2

4
1 2 3 4 5 6 7 8

输出 2

2 3 5 7
1 4 6 8

说明 2

解 $A = [1, 2, 3, 4]$ 和 $B = [5, 6, 7, 8]$ 也是正确的。

输入 3

2
4 3 2 1

输出 3

-1

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.