QOJ.ac

QOJ

Límite de tiempo: 2 s Límite de memoria: 1024 MB Puntuación total: 100

#4929. 最长不友好子序列

Estadísticas

如果一个序列 $b_1, b_2, \dots, b_m$ 满足以下条件,我们称其为不友好序列(unfriendly):

  • 若 $1 \le i < j \le m$ 且 $j - i \le 2$,则 $b_i \neq b_j$。

换句话说,如果一个序列中任意两个距离不超过 2 的元素都不相等,则该序列是不友好的。

给定一个序列 $a_1, a_2, \dots, a_n$,求其最长不友好子序列的长度。

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

输入格式

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

每个测试用例的第一行包含一个整数 $n$ ($1 \le n \le 2 \cdot 10^5$),表示序列的长度。

每个测试用例的第二行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$),表示序列 $a$ 的元素。

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

输出格式

对于每个测试用例,输出一个整数,表示序列 $a$ 的最长不友好子序列的长度。

样例

输入 1

3
5
1 2 1 2 1
7
1 2 3 2 1 2 3
8
1 10 10 1 1 100 100 1

输出 1

2
6
4

说明

在第一个测试用例中,最长不友好子序列为 $(1, 2)$ 和 $(2, 1)$。例如,子序列 $(1, 2, 1)$ 不是不友好的,因为它的第 1 个元素和第 3 个元素相等。

在第二个测试用例中,最长不友好子序列为 $(1, 2, 3, 1, 2, 3)$。显然,由整个序列组成的子序列不是不友好的,因此答案为 6。

在第三个测试用例中,最长不友好子序列为 $(1, 10, 100, 1)$。

子任务

  1. (3 分): $a_i \le a_{i+1}$
  2. (6 分): $n \le 8$
  3. (8 分): 所有测试用例中 $n$ 的总和不超过 500
  4. (10 分): $a_i \le 3$
  5. (10 分): $a_i \le 10$
  6. (20 分): 所有测试用例中 $n$ 的总和不超过 10000
  7. (43 分): 无附加限制

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.