#P15907. [TOPC 2024] Cards

[TOPC 2024] Cards

Description

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: a1,a2,,ana_1, a_2, \dots, a_n and b1,b2,,bnb_1, b_2, \dots, b_n. You have to find another permutation of the first nn positive integers p1,p2,,pnp_1, p_2, \dots, p_n, such that a=[ap1,ap2,,apn]a' = [a_{p_1}, a_{p_2}, \dots, a_{p_n}] and b=[bp1,bp2,,bpn]b' = [b_{p_1}, b_{p_2}, \dots, b_{p_n}] have the same inversion count. Output the sequences aa' and bb'.

Input Format

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 a1,a2,,ana_1, a_2, \dots, a_n, where aia_i is the number on the front of the ii-th card. The third line of the input contains nn integers b1,b2,,bnb_1, b_2, \dots, b_n, where bib_i is the number on the back of the ii-th card.

Output Format

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 a1,a2,,ana'_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 b1,b2,,bnb'_1, b'_2, \cdots , b'_n, denoting the numbers on the back of the cards after the rearrangement.

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

5
2 5 1 4 3
4 2 5 3 1
Yes
3 1 5 2 4
1 5 2 4 3
4
2 4 1 3
3 1 2 4
No
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
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

Hint

  • 1n5×1051 \le n \le 5 \times 10^5
  • 1ain1 \le a_i \le n for i{1,2,,n}i \in \{1, 2, \dots, n\}
  • 1bin1 \le b_i \le n for i{1,2,,n}i \in \{1, 2, \dots, n\}
  • It is guaranteed that a1,a2,,ana_1, a_2, \dots, a_n and b1,b2,,bnb_1, b_2, \dots, b_n are both permutations of integers 1,2,,n1, 2, \dots, n.