Codeforces 1842C Tenzing and Balls

题目描述:

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 aiai.

Tenzing can do the following operation any number of times:

  • select ii and jj such that 1≤i<j≤|a|1≤i<j≤|a| and ai=ajai=aj,
  • remove ai,ai+1,…,ajai,ai+1,…,aj from the array (and decrease the indices of all elements to the right of ajaj by j−i+1j−i+1).

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

Input

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

The first line contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of balls.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n) — the color of the balls.

It is guaranteed that sum of nn of all test cases will not exceed 2⋅1052⋅105.

Output

For each test case, output the maximum number of balls Tenzing can remove.

input

2
5
1 2 2 3 3
4
1 2 1 2

output

4
3

思路分析:dp,dp[i]代表区间[1,i]所能删除的最大数量。

代码:

#include<iostream>
#include<cstring>
using namespace std;
int a[200005],dp[200005],b[200005];
int main(){
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			dp[i]=0;
			b[a[i]]=-1;
		}
		dp[0]=0;
		for(int i=1;i<=n;i++){
			if(b[a[i]]!=-1)
			{
				int ans=max(dp[b[a[i]]-1]+i+1-b[a[i]],dp[b[a[i]]]+i-b[a[i]]);
				dp[i]=max(dp[i-1],ans);
			}
			else
			dp[i]=dp[i-1];
			b[a[i]]=i;
		}
		cout<<dp[n]<<endl;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值