UVA 12385 Interesting Sequences

I - Interesting Sequences

For a sequence of integer numbers <x1,x2,…,xn>, a contiguous subsequence <xi,xi+1,…,xj> where i<j≤n, is called "interesting" if  its first and last elements are equal (i.e., xi=xj). Two interesting subsequences S1=<xi,xi+1,…,xj> and S2=<xa,xa+1,…,xb> are called conflict-free if either a≥j or i≥b.

For a given sequence of known size, find the maximum number of interesting subsequences which are pairwise conflict-free. 

Input

The first line of input contains an integer T≤100 denoting the number of test-cases. Each test-case begins with an integer 1≤N≤100,000, on a separate line, denoting the size of the sequence. The following line contains N positive integers each between 1 and 100,000 (inclusive).

Output

For each test-case, output on a single line the maximum number of pairwise conflict-free interesting subsequences.

Sample Input

Sample Output

3

6

1 2 1 3 1 2

4

2 4 2 4

9

10 2 2 10 3 4 5 4 3

2

1

2


  解析

这个题的题意略坑,其实是问怎么划分这个数列使有趣数列的总数最大。

样例解释

1 2 1 3 1 2

2 4 2 4

10 2 2 10 3 4 5 4

动态规划 f[i]表示前i个数最大的总数,显而易见只有存在以i结尾的有趣数列才f[i]可能改变。
顺带提一点,要记录pre


#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int Pre[100010],Pot[100010],N,Arr[100010],f[100010];

void readdata()
{
	memset(Pre,0,sizeof(Pre));
	memset(Pot,0,sizeof(Pot));
	memset(f,0,sizeof(f));

	scanf("%d",&N);
	for(int i=1;i<=N;i++)
	{
		scanf("%d",&Arr[i]);
		Pre[i]=Pot[Arr[i]];
		Pot[Arr[i]]=i;
	}	
}
void work2()
{
	for(int i=1;i<=N;i++)
	{
		f[i]=f[i-1];
		if(Pre[i]!=0) f[i]=max(f[i],f[Pre[i]]+1);
	}

	printf("%d",f[N]);
}

int main()
{
	freopen("I.in","r",stdin);
	freopen("I.out","w",stdout);

	int T;
	scanf("%d",&T);
	for(int i=1;i<=T;i++)
	{
		readdata();
		work2();
		printf("\n");
	}

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值