QOJ.ac

QOJ

时间限制: 4 s 内存限制: 1024 MB 总分: 100

#4930. 排列的 LCS

统计

对于两个序列 $x$ 和 $y$,我们定义 $LCS(x, y)$ 为它们的最长公共子序列的长度。

给定 4 个整数 $n, a, b, c$。请判断是否存在 3 个从 $1$ 到 $n$ 的排列 $p, q, r$,使得: $LCS(p, q) = a$ $LCS(p, r) = b$ * $LCS(q, r) = c$

如果存在这样的排列,请找出任意一组这样的排列。

$1$ 到 $n$ 的排列 $p$ 是一个长度为 $n$ 的序列,其中所有元素都是范围 $[1, n]$ 内互不相同的整数。例如,$(2, 4, 3, 5, 1)$ 是 $1$ 到 $5$ 的一个排列,而 $(1, 2, 1, 3, 5)$ 和 $(1, 2, 3, 4, 6)$ 则不是。

如果序列 $c$ 可以通过删除序列 $d$ 中的若干(可能为零或全部)元素得到,则称 $c$ 是 $d$ 的子序列。例如,$(1, 3, 5)$ 是 $(1, 2, 3, 4, 5)$ 的子序列,而 $(3, 1)$ 则不是。

序列 $x$ 和 $y$ 的最长公共子序列是既是 $x$ 的子序列又是 $y$ 的子序列的最长序列 $z$。例如,序列 $x = (1, 3, 2, 4, 5)$ 和 $y = (5, 2, 3, 4, 1)$ 的最长公共子序列是 $z = (2, 4)$,因为它是两个序列的公共子序列且长度最长。$LCS(x, y)$ 即为最长公共子序列的长度,在上述例子中为 $2$。

输入格式

输入的第一行包含一个整数 $t$ ($1 \le t \le 10^5$),表示测试用例的数量。接下来是各测试用例的描述。

每个测试用例仅一行,包含 5 个整数 $n, a, b, c, output$ ($1 \le a \le b \le c \le n \le 2 \cdot 10^5$, $0 \le output \le 1$)。

如果 $output = 0$,只需判断是否存在这样的排列。如果 $output = 1$,若存在,你还需要找出这样的一组排列。

保证所有测试用例中 $n$ 的总和不超过 $2 \cdot 10^5$。

输出格式

对于每个测试用例,第一行输出 "YES"(如果存在这样的排列 $p, q, r$),否则输出 "NO"。如果 $output = 1$ 且存在这样的排列,则额外输出三行:

第一行输出 $n$ 个整数 $p_1, p_2, \dots, p_n$,表示排列 $p$ 的元素。 第二行输出 $n$ 个整数 $q_1, q_2, \dots, q_n$,表示排列 $q$ 的元素。 第三行输出 $n$ 个整数 $r_1, r_2, \dots, r_n$,表示排列 $r$ 的元素。

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

你可以以任意大小写形式输出(例如,"YES", "Yes", "yes", "yEs" 均会被识别为肯定回答)。

样例

输入 1

8
1 1 1 1 1
4 2 3 4 1
6 4 5 5 1
7 1 2 3 1
1 1 1 1 0
4 2 3 4 0
6 4 5 5 0
7 1 2 3 0

输出 1

YES
1
1
1
NO
YES
1 3 5 2 6 4
3 1 5 2 4 6
1 3 5 2 4 6
NO
YES
NO
YES
NO

说明

在第一个测试用例中,$LCS((1), (1))$ 为 $1$。

在第二个测试用例中,可以证明不存在这样的排列。

在第三个测试用例中,其中一个示例为 $p = (1, 3, 5, 2, 6, 4), q = (3, 1, 5, 2, 4, 6), r = (1, 3, 5, 2, 4, 6)$。容易看出: $LCS(p, q) = 4$(最长公共子序列之一为 $(1, 5, 2, 6)$) $LCS(p, r) = 5$(最长公共子序列之一为 $(1, 3, 5, 2, 4)$) * $LCS(q, r) = 5$(最长公共子序列之一为 $(3, 5, 2, 4, 6)$)

在第四个测试用例中,可以证明不存在这样的排列。

子任务

  1. (3 分): $a = b = 1, c = n, output = 1$
  2. (8 分): $n \le 6, output = 1$
  3. (10 分): $c = n, output = 1$
  4. (17 分): $a = 1, output = 1$
  5. (22 分): $output = 0$
  6. (40 分): $output = 1$

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

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.