A16464 | Merge Not Sort
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
```
<pre class="verbatim"><br></br> Merge(A[1..N], B[1..N]):<br></br> C = []<br></br> i = 1<br></br> j = 1<br></br> while i <= N AND j <= N:<br></br> if A[i] < B[j]:<br></br> append A[i] to C<br></br> i = i + 1<br></br> else:<br></br> append B[j] to C<br></br> j = j + 1 <br></br> while i <= N:<br></br> append A[i] to C<br></br> i = i + 1 <br></br> while j <= N:<br></br> append B[j] to C<br></br> j = j + 1 <br></br> return C<br></br>
```
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 $\text{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 \cdot N$ such that it is a permutation of $1$ to $2 \cdot N$ . Construct any two arrays $A$ and $B$ of the same length $N$ , such that $\text{Merge}(A, B) = C$ , or determine if it is impossible to do so.
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.
```
<pre class="verbatim"><br></br> Merge(A[1..N], B[1..N]):<br></br> C = []<br></br> i = 1<br></br> j = 1<br></br> while i <= N AND j <= N:<br></br> if A[i] < B[j]:<br></br> append A[i] to C<br></br> i = i + 1<br></br> else:<br></br> append B[j] to C<br></br> j = j + 1 <br></br> while i <= N:<br></br> append A[i] to C<br></br> i = i + 1 <br></br> while j <= N:<br></br> append B[j] to C<br></br> j = j + 1 <br></br> return C<br></br>
```
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 $\text{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 \cdot N$ such that it is a permutation of $1$ to $2 \cdot N$ . Construct any two arrays $A$ and $B$ of the same length $N$ , such that $\text{Merge}(A, B) = C$ , or determine if it is impossible to do so.
输入格式
The first line consists of an integer $N$ ( $1 \leq N \leq 1000$ ).
The following line consists of $2 \cdot N$ integers $C_i$ . The array $C$ is a permutation of $1$ to $2 \cdot N$ .
The following line consists of $2 \cdot N$ integers $C_i$ . The array $C$ is a permutation of $1$ to $2 \cdot N$ .
输出格式
If it is impossible to construct two arrays $A$ and $B$ of length $N$ such that $\text{Merge}(A, B) = C$ , then output -1.
Otherwise, output the arrays $A$ and $B$ in two lines. The first line consists of $N$ integers $A_i$ . The second line consists of $N$ integers $B_i$ . If there are several possible answers, output any of them.
Otherwise, output the arrays $A$ and $B$ in two lines. The first line consists of $N$ integers $A_i$ . The second line consists of $N$ integers $B_i$ . If there are several possible answers, output any of them.
输入输出样例
输入 #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.
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted