CF1699D.Almost Triple Deletions

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an integer nn and an array a1,a2,…,ana_1,a_2,\ldots,a_n.

In one operation, you can choose an index ii (1≤i<n1 \le i \lt n) for which ai≠ai+1a_i \neq a_{i+1} and delete both aia_i and ai+1a_{i+1} from the array. After deleting aia_i and ai+1a_{i+1}, the remaining parts of the array are concatenated.

For example, if a=[1,4,3,3,6,2]a=[1,4,3,3,6,2], then after performing an operation with i=2i=2, the resulting array will be [1,3,6,2][1,3,6,2].

What is the maximum possible length of an array of equal elements obtainable from aa by performing several (perhaps none) of the aforementioned operations?

给你一个整数 nn 和一个数组 a1,a2,…,ana_1,a_2,\ldots,a_n。

在一次操作中,你可以选择一个下标 ii(满足 1≤i<n1 \le i \lt n)使得 ai≠ai+1a_i \neq a_{i+1},然后将 aia_i 和 ai+1a_{i+1} 同时从数组中删除。删除后,数组剩余的两部分会直接拼接起来。

例如,若 a=[1,4,3,3,6,2]a=[1,4,3,3,6,2],则对 i=2i=2 执行一次操作后,得到的新数组为 [1,3,6,2][1,3,6,2]。

通过执行若干次(可能为零次)上述操作,你能得到的所有元素均相等的数组的最大可能长度是多少?

输入格式

Each test contains multiple test cases. The first line of input contains one integer tt (1≤t≤10001 \le t \le 1000) — the number of test cases. The following lines contain the descriptions of the test cases.

The first line of each test case contains a single integer nn (1≤n≤50001 \le n \le 5000) — the length of array aa.

The second line of each test case contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (1≤ai≤n1 \le a_i \le n) — the elements of array aa.

It is guaranteed that the sum of nn across all test cases does not exceed 10 00010\,000.

每个测试包含多个测试用例。输入的第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000),表示测试用例的数量。接下来的行描述各个测试用例。

每个测试用例的第一行包含一个整数 nn(1≤n≤50001 \le n \le 5000),表示数组 aa 的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(1≤ai≤n1 \le a_i \le n),表示数组 aa 的元素。

保证所有测试用例中 nn 的总和不超过 10 00010\,000。

输出格式

For each testcase, print a single integer, the maximum possible length of an array of equal elements obtainable from aa by performing a sequence of operations.

对于每个测试用例,输出一个整数,表示通过对数组 aa 执行一系列操作所能得到的、由相等元素构成的数组的最大可能长度。

输入输出样例

  • 输入#1

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

    输出#1

    3
    1
    0
    4
    2

说明/提示

For the first testcase, an optimal sequence of operations would be: [1,2,3,2,1,3,3]→[3,2,1,3,3]→[3,3,3][1,2,3,2,1,3,3] \rightarrow [3,2,1,3,3] \rightarrow [3,3,3].

For the second testcase, all elements in the array are already equal.

For the third testcase, the only possible sequence of operations is: [1,1,1,2,2,2]→[1,1,2,2]→[1,2]→[][1,1,1,2,2,2] \rightarrow [1,1,2,2] \rightarrow [1,2] \rightarrow []. Note that, according to the statement, the elements deleted at each step must be different.

For the fourth testcase, the optimal sequence of operations is: [1,1,2,2,3,3,1,1]→[1,1,2,3,1,1]→[1,1,1,1][1,1,2,2,3,3,1,1] \rightarrow [1,1,2,3,1,1] \rightarrow [1,1,1,1].

For the fifth testcase, one possible reachable array of two equal elements is [4,4][4,4].

对于第一个测试用例,一种最优的操作序列如下:[1,2,3,2,1,3,3]→[3,2,1,3,3]→[3,3,3][1,2,3,2,1,3,3] \rightarrow [3,2,1,3,3] \rightarrow [3,3,3]。

对于第二个测试用例,数组中所有元素已经相等。

对于第三个测试用例,唯一可能的操作序列是:[1,1,1,2,2,2]→[1,1,2,2]→[1,2]→[][1,1,1,2,2,2] \rightarrow [1,1,2,2] \rightarrow [1,2] \rightarrow []。注意,根据题面要求,每一步删除的元素必须互不相同。

对于第四个测试用例,最优的操作序列是:[1,1,2,2,3,3,1,1]→[1,1,2,3,1,1]→[1,1,1,1][1,1,2,2,3,3,1,1] \rightarrow [1,1,2,3,1,1] \rightarrow [1,1,1,1]。

对于第五个测试用例,一个可达的、包含两个相等元素的数组是 [4,4][4,4]。

输入解题思路,AI测评打分。不知道怎么写?

首页