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;
}