Merge Not Sort

시간 제한2초메모리 제한1024 MB

요약
1부터 2N까지의 순열 C가 주어질 때, 표준 병합 알고리즘이 C를 출력하도록 만드는 길이 N의 배열 A와 B를 찾는다.
난이도

보통10점 중 6점

유형
그리디, 구현, 시뮬레이션
정답자
아직 제출이 없습니다

문제

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 AA and BB are not necessarily sorted. For example, if A=\[3,1,6]A = \[3, 1, 6] and B=\[4,5,2]B = \[4, 5, 2], then Merge(A,B)=\[3,1,4,5,2,6]\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 CC of length 2⋅N2 \cdot N such that it is a permutation of 11 to 2⋅N2 \cdot N. Construct any two arrays AA and BB of the same length NN, such that Merge(A,B)=C\text{Merge}(A, B) = C, or determine if it is impossible to do so.

입력

The first line consists of an integer NN (1≤N≤10001 ≤ N ≤ 1000).

The following line consists of 2⋅N2 \cdot N integers C_iC\_i. The array CC is a permutation of 11 to 2⋅N2 \cdot N.

출력

If it is impossible to construct two arrays AA and BB of length NN such that Merge(A,B)=C\text{Merge}(A, B) = C, then output -1.

Otherwise, output the arrays AA and BB in two lines. The first line consists of NN integers A_iA\_i. The second line consists of NN integers B_iB\_i. If there are several possible answers, output any of them.

예제3

  1. 예제 1

    입력
    3
    3 1 4 5 2 6
    
    예상 출력
    3 1 6
    4 5 2
    
  2. 예제 2

    입력
    4
    1 2 3 4 5 6 7 8
    
    예상 출력
    2 3 5 7
    1 4 6 8
    
  3. 예제 3

    입력
    2
    4 3 2 1
    
    예상 출력
    -1