QOJ.ac

QOJ

حد الوقت: 2 s حد الذاكرة: 2048 MB مجموع النقاط: 100

#15128. 卡牌

الإحصائيات

Diana 是一名喜欢玩各种桌游的学生。今天,她收到了老师送给她的一份生日礼物——一副卡牌!

这副卡牌很特别:整副牌共有 $n$ 张,每张卡牌的正反面各有一个数字。正面或反面的每个数字都是 $1$ 到 $n$ 之间的整数。此外,正面所有的 $n$ 个数字互不相同,反面所有的 $n$ 个数字也互不相同。换句话说,正面和反面的数字分别是 $1$ 到 $n$ 的两个不同的排列。

除了桌游,Diana 对数学和计算机科学也很感兴趣。当她玩这些卡牌时,脑海中浮现出了排列中“逆序对”(inversions)的概念。一个逆序对被定义为一对下标 $(i, j)$,满足 $i < j$ 且位置 $i$ 的元素大于位置 $j$ 的元素。换句话说,逆序对表示两个元素相对于它们的位置而言是“无序”的。如果一个排列中可以找到 $c$ 个逆序对,则该排列的逆序对数为 $c$。

Diana 想知道她是否能将这些卡牌按某种顺序重新排列,使得正面排列的逆序对数与反面排列的逆序对数相同(她不能翻转卡牌,也不能丢弃任何卡牌)。她一时半会解决不了这个问题,所以想听听你的解决方案。

形式化地,给定两个 $1$ 到 $n$ 的整数排列:$a_1, a_2, \dots, a_n$ 和 $b_1, b_2, \dots, b_n$。你需要找到前 $n$ 个正整数的一个排列 $p_1, p_2, \dots, p_n$,使得 $a' = [a_{p_1}, a_{p_2}, \dots, a_{p_n}]$ 和 $b' = [b_{p_1}, b_{p_2}, \dots, b_{p_n}]$ 具有相同的逆序对数。输出序列 $a'$ 和 $b'$。

输入格式

输入的第一行包含一个整数 $n$,表示卡牌的数量。

输入的第二行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$,其中 $a_i$ 是第 $i$ 张卡牌正面的数字。

输入的第三行包含 $n$ 个整数 $b_1, b_2, \dots, b_n$,其中 $b_i$ 是第 $i$ 张卡牌反面的数字。

输出格式

如果无法通过重新排列卡牌来满足上述条件,则输出 No

否则,在输出的第一行打印 Yes。接着在输出的第二行打印 $n$ 个整数 $a'_1, a'_2, \dots, a'_n$,表示重新排列后卡牌正面的数字。在输出的第三行打印 $n$ 个整数 $b'_1, b'_2, \dots, b'_n$,表示重新排列后卡牌反面的数字。

如果存在多个可能的解,输出其中任意一个即可。

数据范围

  • $1 \le n \le 5 \times 10^5$
  • 对于 $i \in \{1, 2, \dots, n\}$,有 $1 \le a_i \le n$
  • 对于 $i \in \{1, 2, \dots, n\}$,有 $1 \le b_i \le n$
  • 保证 $a_1, a_2, \dots, a_n$ 和 $b_1, b_2, \dots, b_n$ 均为 $1, 2, \dots, n$ 的排列。

样例

输入 1

5
2 5 1 4 3
4 2 5 3 1

输出 1

Yes
3 1 5 2 4
1 5 2 4 3

输入 2

4
2 4 1 3
3 1 2 4

输出 2

No

输入 3

10
7 4 3 1 6 10 5 2 9 8
8 6 2 9 5 10 7 1 4 3

输出 3

Yes
2 3 8 1 4 5 9 6 7 10
1 2 3 9 6 7 4 5 8 10

输入 4

7
1 2 3 4 5 6 7
1 2 3 4 5 6 7

输出 4

Yes
1 2 3 4 5 6 7
1 2 3 4 5 6 7

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.