Cards

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

요약
두 순열 a와 b가 주어질 때, 카드 쌍의 순서를 정해 앞면과 뒷면 순열의 역전 개수가 같아지도록 배열하고, 불가능하면 No를 출력한다.
난이도

어려움10점 중 8점

유형
정렬, 그리디, 조합론, 수학
정답자
아직 제출이 없습니다

문제

Diana is a student who likes to play various types of board games. Today, she receives a deck of cards from her teacher as her birthday gift!

The deck of cards is special: there are nn cards in the deck, and each card has a number on its front and another number on its back. Each number on the front or the back is an integer from 11 to nn. Furthermore, all nn numbers on the front are unique, and so are the nn numbers on the back. In other words, numbers on the front and the back are two different permutations of numbers from 11 to nn.

Apart from board games, Diana is also interested in mathematics and computer science. While she is playing with those cards, the concept of inversions in a permutation comes to her mind. An inversion is defined as a pair of indices (i,j)(i, j) such that i\<ji\<j and the element at position ii is greater than the element at position jj. In other words, an inversion represents a situation where two elements are “out of order” relative to their positions. A permutation has inversion count cc if there are cc inversions can be found within it.

Diana wonders if she could rearrange the cards in some order so that the permutation on the front has the same inversion count as the permutation on the back (she cannot flip or throw away some cards). She cannot solve the problem in a while, so she wants to hear your solution.

In formal, you are given two permutations of integers from 11 to nn: a_1,a_2,…,a_na\_1, a\_2, \dots ,a\_n and b_1,b_2,…,b_nb\_1, b\_2, \dots ,b\_n. You have to find another permutation of the first nn positive integers p_1,p_2,…,p_np\_1, p\_2, \dots ,p\_n, such that a′=\[a_p_1,a_p_2,…,a_p_n]a' = \[a\_{p\_1} , a\_{p\_2} ,\dots ,a\_{p\_n}] and b′=\[b_p_1,b_p_2,…,b_p_n]b' = \[b\_{p\_1} , b\_{p\_2} ,\dots ,b\_{p\_n} ] have the same inversion count. Output the sequences a′a' and b′b'.

입력

The first line of the input contains an integer nn, denoting the number cards in the deck. The second line of the input contains nn integers a_1,a_2,…,a_na\_1, a\_2,\dots ,a\_n, where a_ia\_i is the number on the front of the ii-th card. The third line of the input contains nn integers b_1,b_2,…,b_nb\_1, b\_2,\dots ,b\_n, where b_ib\_i is the number on the back of the ii-th card.

출력

If it is impossible to rearrange the cards so that the aforementioned condition is satisfied, print No. Otherwise, print Yes in the first line of the output. Then in the second line of the output, print nn integers a′_1,a′_2,…,a′_na'\_1, a'\_2, \dots ,a'\_n, denoting the numbers on the front of the cards after the rearrangement. In the third line of the output, print nn integers b′_1,b′_2,…,b′_nb'\_1, b'\_2, \dots , b'\_n, denoting the numbers on the back of the cards after the rearrangement.

If there are multiple possible solutions, print any of them.

제한

  • 1≤n≤5×1051 ≤ n ≤ 5 \times 10^5
  • 1≤a_i≤n1 ≤ a\_i ≤ n for i∈1,2,…,ni \in \\{1, 2, \dots ,n\\}
  • 1≤b_i≤n1 ≤ b\_i ≤ n for i∈1,2,…,ni \in \\{1, 2,\dots ,n\\}
  • It is guaranteed that a_1,a_2,…,a_na\_1, a\_2,\dots ,a\_n and b_1,b_2,…,b_nb\_1, b\_2,\dots ,b\_n are both permutations of integers 1,2,…,n1, 2, \dots ,n.

예제4

  1. 예제 1

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

    입력
    4
    2 4 1 3
    3 1 2 4
    
    예상 출력
    No
    
  3. 예제 3

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

    입력
    7
    1 2 3 4 5 6 7
    1 2 3 4 5 6 7
    
    예상 출력
    Yes
    1 2 3 4 5 6 7
    1 2 3 4 5 6 7