QOJ.ac

QOJ

时间限制: 2.0 s 内存限制: 512 MB 总分: 100

#454. 遗忘所有余下

统计

给定两个整数数组:长度为 $n$ 的数组 $a$ 和长度为 $m$ 的数组 $b$。两个数组中的所有整数均互不相同。

两个数组的交错序列(interleaving)是一个大小为 $n + m$ 的数组 $c$,使得数组 $a$ 和 $b$ 分别是 $c$ 的不相交子序列。形式化地,存在下标 $i_1 < i_2 < \dots < i_n$ 使得 $c_{i_1} = a_1, c_{i_2} = a_2, \dots, c_{i_n} = a_n$,以及下标 $j_1 < j_2 < \dots < j_m$ 使得 $c_{j_1} = b_1, c_{j_2} = b_2, \dots, c_{j_m} = b_m$。对于这些下标,满足对于所有 $x = 1, 2, \dots, n$ 和 $y = 1, 2, \dots, m$,都有 $i_x \neq j_y$。

显然,将数组 $a$ 和 $b$ 进行交错的方式通常有很多种。请找到一种方式,使得交错序列 $c$ 的最长上升子序列长度最大。

输入格式

第一行包含一个整数 $n$ ($1 \le n \le 5 \cdot 10^5$),表示数组 $a$ 的长度。 第二行包含 $n$ 个整数 $a_i$ ($1 \le a_i \le 10^9$)。 第三行包含一个整数 $m$ ($1 \le m \le 5 \cdot 10^5$),表示数组 $b$ 的长度。 第四行包含 $m$ 个整数 $b_j$ ($1 \le b_j \le 10^9$)。

保证两个数组中的数字互不相同:对于 $i \neq j$ 有 $a_i \neq a_j$,对于 $i \neq j$ 有 $b_i \neq b_j$,且对于所有合法的 $i$ 和 $j$ 有 $a_i \neq b_j$。

输出格式

输出一个整数:在 $a$ 和 $b$ 的某种交错序列中,最长上升子序列的最大长度。

样例

输入 1

2
1 7
3
6 10 11

输出 1

5

输入 2

3
7 1 5
3
9 8 6

输出 2

3

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.