CF1842C.Tenzing and Balls

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

Enjoy erasing Tenzing, identified as Accepted!

Tenzing has nn balls arranged in a line. The color of the ii -th ball from the left is aia_i .

Tenzing can do the following operation any number of times:

  • select ii and jj such that 1i<ja1\leq i < j \leq |a| and ai=aja_i=a_j ,
  • remove ai,ai+1,,aja_i,a_{i+1},\ldots,a_j from the array (and decrease the indices of all elements to the right of aja_j by ji+1j-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 tt ( 1t1031\leq t\leq 10^3 ) — the number of test cases. The description of test cases follows.

The first line contains a single integer nn ( 1n21051\leq n\leq 2\cdot 10^5 ) — the number of balls.

The second line contains nn integers a1,a2,,ana_1,a_2,\ldots,a_n ( 1ain1\leq a_i \leq n ) — the color of the balls.

It is guaranteed that sum of nn of all test cases will not exceed 21052\cdot 10^5 .

输出格式

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=2i=2 and j=3j=3 in the first operation so that a=[1,3,3]a=[1,3,3] . Then Tenzing will choose i=2i=2 and j=3j=3 again in the second operation so that a=[1]a=[1] . So Tenzing can remove 44 balls in total.

In the second example, Tenzing will choose i=1i=1 and j=3j=3 in the first and only operation so that a=[2]a=[2] . So Tenzing can remove 33 balls in total.

首页