CF1842C.Tenzing and Balls

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

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 1≤i<j≤∣a∣1\leq i \lt 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 j−i+1j-i+1).

Tenzing wants to know the maximum number of balls he can remove.

享受擦除腾津,状态为“已通过”!

腾津有 nn 个球排成一行。从左往右数第 ii 个球的颜色为 aia_i。

腾津可以执行以下操作任意多次:

  • 选择满足 1≤i<j≤∣a∣1\leq i \lt j \leq |a| 且 ai=aja_i=a_j 的下标 ii 和 jj;
  • 将子数组 ai,ai+1,…,aja_i,a_{i+1},\ldots,a_j 从数组中移除(并将所有位于 aja_j 右侧的元素下标减去 j−i+1j-i+1)。

腾津想知道他最多能移除多少个球。

输入格式

Each test contains multiple test cases. The first line of input contains a single integer tt (1≤t≤1031\leq t\leq 10^3) — the number of test cases. The description of test cases follows.

The first line contains a single integer nn (1≤n≤2⋅1051\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 (1≤ai≤n1\leq a_i \leq n) — the color of the balls.

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

每个测试包含多个测试用例。输入的第一行包含一个整数 tt(1≤t≤1031\leq t\leq 10^3),表示测试用例的数量。随后是各测试用例的描述。

第一行包含一个整数 nn(1≤n≤2⋅1051\leq n\leq 2\cdot 10^5),表示球的数量。

第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(1≤ai≤n1\leq a_i \leq n),表示各个球的颜色。

保证所有测试用例的 nn 之和不超过 2⋅1052\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.

在第一个例子中,丹增在第一次操作中选择 i=2i=2 和 j=3j=3,使得 a=[1,3,3]a=[1,3,3];然后在第二次操作中再次选择 i=2i=2 和 j=3j=3,使得 a=[1]a=[1]。因此,丹增总共可以移除 44 个球。

在第二个例子中,丹增在唯一的一次操作中选择 i=1i=1 和 j=3j=3,使得 a=[2]a=[2]。因此,丹增总共可以移除 33 个球。

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

首页