QOJ.ac

QOJ

Limite de temps : 1.0 s Limite de mémoire : 256 MB Points totaux : 100 Hackable ✓

#11301. 美丽的项链

Statistiques

Busy Beaver 收集了一些海螺对,他正试图将它们制作成两条美丽的项链!

他有 $N$ 对海螺,其中第 $i$ 对中的两枚海螺类型均为 $a_i$。他想制作两条项链,使得每条项链都包含每一对海螺中的一枚。Busy Beaver 为这对美丽的项链制定了他自己的衡量标准,即最小化两条项链之间最长公共子序列的长度。

形式化地,令 $s$ 和 $t$ 为原始数组 $a$ 的两个排列。我们想要找到 $(s, t)$,使得其循环最长公共子序列的长度 $f(s, t)$ 最小,定义如下:

  • 考虑数组 $t$ 的所有循环移位,记为 $t_1, t_2, \dots, t_n$。
  • 令 $\text{LCS}(s, t)$ 为 $s$ 和 $t$ 之间的最长公共子序列长度。则 $$f(s, t) = \max_{1 \le i \le n} \text{LCS}(s, t_i)$$

不幸的是,Busy Beaver 手头的海螺太多,无法手工找到最美丽的项链对。请帮帮他!

输入格式

每个测试包含多个测试用例。输入的第一行包含一个整数 $T$ ($1 \le T \le 500$),表示测试用例的数量。接下来是每个测试用例的描述。

每个测试用例的第一行包含一个正整数 $N$ ($1 \le N \le 500$)。

每个测试用例的第二行包含 $N$ 个整数 $a_1, a_2, \dots, a_N$ ($1 \le a_i \le 10^9$),表示 Busy Beaver 收集的海螺类型。

保证所有测试用例的 $N$ 之和不超过 $500$。

输出格式

对于每个测试用例,输出两行。第一行包含 $N$ 个整数 $s_1, s_2, \dots, s_N$,第二行包含 $N$ 个整数 $t_1, t_2, \dots, t_N$,表示使 $f(s, t)$ 最小化的某一组 $(s, t)$。

如果存在多种解,输出任意一种即可。

样例

输入 1

1
3
1 2 3

输出 1

1 2 3
1 3 2

说明

注意 $f([1, 2, 3], [1, 3, 2])$ 为 $2$,因为 $\text{LCS}([1, 2, 3], [1, 3, 2]) = 2$($[1, 2]$ 是一个公共子序列)。这是 $t$ 的所有循环移位中最大的 LCS:

  • $\text{LCS}([1, 2, 3], [1, 3, 2]) = 2$($[1, 2]$ 是一个公共子序列)。
  • $\text{LCS}([1, 2, 3], [3, 2, 1]) = 1$($[1]$ 是一个公共子序列)。
  • $\text{LCS}([1, 2, 3], [2, 1, 3]) = 2$($[2, 3]$ 是一个公共子序列)。

可以证明,不存在 $s$ 和 $t$ 作为 $[1, 2, 3]$ 的排列,使得 $f(s, t) \le 1$。


*如果数组 $s$ 可以通过从数组 $t$ 中删除一些(可能为零或全部)元素,且不改变剩余元素的顺序而得到,则称 $s$ 是 $t$ 的子序列。 †数组 $t = [t^{(1)}, t^{(2)}, \dots, t^{(n)}]$ 循环移位 $i$ 位后的结果为 $[t^{(1+i)}, t^{(2+i)}, \dots, t^{(n+i)}]$,其中索引对 $n$ 取模。

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.