CF1699D.Almost Triple Deletions

普及/提高-

通过率:0%

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 ( 1i<n1 \le i \lt n ) for which aiai+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?

输入格式

Each test contains multiple test cases. The first line of input contains one integer tt ( 1t10001 \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 ( 1n50001 \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 ( 1ain1 \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 1000010\,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.

输入输出样例

  • 输入#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] .

首页