2021 CCPC(Harbin)-B. Magical Subsequence

Given a sequence A1,A2,⋯,An. As for a subsequence Ab1,Ab2,⋯,Abm(1≤b1<b2<⋯<bm≤n), we say it magical if and only if mm is even and Ab1+Ab2=Ab3+Ab4=⋯=Abm−1+Abm. Determine the maximum length among all magical subsequences of the given sequence.

Input

The first line contains one integer n(2≤n≤105), denoting the length of given sequence.

The second line contains nn integers A1,A2,⋯,An(1≤Ai≤100), denoting the given sequence.

Output

Output one line containing only one integer, denoting the answer.

Example

input

11
3 1 4 1 5 9 2 6 5 3 5

output

6

Note

One possible magical subsequence of length 6 is {A1=3,A5=5,A7=2,A8=6,A9=5,A10=3}. Here 3+5=2+6=5+3=83+5=2+6=5+3=8.

题意:尽可能选出n对数,使得每对数和相同,第 i 组最小下标要大于第 i-1 组的最大下标,求集合的长度,其实也就是组数x2。可以注意到Ai值是在1~100,这是关键点,因此对数和最小2,最大200。然后我们可以遍历【2~200】,得出答案 ,因为n>=2,最少一组,不存在0组。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
bool st[205];//记录该i这个数是否使用
int a[100005];
int main()
{
	int n,maxn=2;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int sum=2;sum<=200;sum++)
	{
		int res=0;//记录每个sum下的最大长度
		memset(st,false,sizeof st);//多次遍历,初始化
		for(int i=1;i<=n;i++)
		{
			if(a[i]>=sum) continue;
			//两数之和大于sum,那么其中一个数肯定小于sum
			if(st[sum-a[i]])//当前是a[i],如果sum-a[i]之前没有使用,那么两者就可以凑一对
			{
				res+=2;
				maxn=max(maxn,res);
				memset(st,false,sizeof st);
		//此处全部清空是因为i之前满足的都已经全部处理了		
			}else st[a[i]]=true;//标记访问过
		}
	}	
	printf("%d\n",maxn);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值