Codeforces Round 804 (Div. 2)
D
Almost Triple Deletions
Codeforces
2300
原题
← CE →
翻译中…

You are given an integer $n$ and an array $a_1,a_2,\ldots,a_n$.

翻译中…

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

翻译中…

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

翻译中…

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

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line of input contains one integer $t$ ($1 \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 $n$ ($1 \le n \le 5000$) — the length of array $a$.

翻译中…

The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le n$) — the elements of array $a$.

翻译中…

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

翻译中…

Output

翻译中…

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

翻译中…

Note

翻译中…

For the first testcase, an optimal sequence of operations would be: $[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] \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] \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]$.

翻译中…

样例

Input #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
Output #1
3
1
0
4
2
InputCopy OutputCopy
题目信息
题号
1699D
时间限制
2 s
内存限制
256 MB
标签
数据结构
动态规划
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。