#P15686. [ICPC 2023 Jakarta R] Merge Not Sort
[ICPC 2023 Jakarta R] Merge Not Sort
Description
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 and are not necessarily sorted. For example, if and , then .
To further increase the understanding of the merge algorithm, you decided to work on the following problem. You are given an array of length such that it is a permutation of to . Construct any two arrays and of the same length , such that , or determine if it is impossible to do so.
Input Format
The first line consists of an integer ( ).
The following line consists of integers . The array is a permutation of to .
Output Format
If it is impossible to construct two arrays and of length such that , then output -1.
Otherwise, output the arrays and in two lines. The first line consists of integers . The second line consists of integers . If there are several possible answers, output any of them.
3
3 1 4 5 2 6
3 1 6
4 5 2
4
1 2 3 4 5 6 7 8
2 3 5 7
1 4 6 8
2
4 3 2 1
-1
Hint
Explanation for the sample input/output #1
The solution and is also correct.
Explanation for the sample input/output #2
The solution and is also correct.
京公网安备 11011102002149号