CF1842C.Tenzing and Balls
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Enjoy erasing Tenzing, identified as Accepted!
Tenzing has n balls arranged in a line. The color of the i -th ball from the left is ai .
Tenzing can do the following operation any number of times:
- select i and j such that 1≤i<j≤∣a∣ and ai=aj ,
- remove ai,ai+1,…,aj from the array (and decrease the indices of all elements to the right of aj by j−i+1 ).
Tenzing wants to know the maximum number of balls he can remove.
输入格式
Each test contains multiple test cases. The first line of input contains a single integer t ( 1≤t≤103 ) — the number of test cases. The description of test cases follows.
The first line contains a single integer n ( 1≤n≤2⋅105 ) — the number of balls.
The second line contains n integers a1,a2,…,an ( 1≤ai≤n ) — the color of the balls.
It is guaranteed that sum of n of all test cases will not exceed 2⋅105 .
输出格式
For each test case, output the maximum number of balls Tenzing can remove.
输入输出样例
输入#1
2 5 1 2 2 3 3 4 1 2 1 2
输出#1
4 3
说明/提示
In the first example, Tenzing will choose i=2 and j=3 in the first operation so that a=[1,3,3] . Then Tenzing will choose i=2 and j=3 again in the second operation so that a=[1] . So Tenzing can remove 4 balls in total.
In the second example, Tenzing will choose i=1 and j=3 in the first and only operation so that a=[2] . So Tenzing can remove 3 balls in total.