程序代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int f[210];
int dp[210][210];
int BinarySearch(int a[], int L, int R, int tgt)
{
int Mid;
while (L<=R)
{
Mid = L+(R-L)/2;
if (a[Mid]==tgt)
{
return Mid;
}
else if (a[Mid]>tgt)
R = Mid-1;
else
L = Mid+1;
}
return -1;
}
int main(void)
{
//freopen("haha.txt","r", stdin);
int T;
scanf("%d", &T);
while (T--)
{
memset(f, 0, sizeof(f));
memset(dp, 0, sizeof(dp));
int n, tmax = 2;
scanf("%d", &n);
for (int i=0; i<n; ++i)
{
scanf("%d", &f[i]);
if (f[i]<=0)
{
i--, n--;
}
}
if (n<=2)
{
printf("%d\n", n);
continue;
}
sort(f, f+n);
for (int i=0; i<n; ++i)
{
for (int j=i+1; j<n; ++j)
{
dp[i][j] = 2;
}
}
for (int i=0; i<n; ++i)
{
for (int j=i+1; j<n; ++j)
{
int k = BinarySearch(f, 0, n-1, f[i]+f[j]);
if (k==-1) continue;
else
{
if (dp[j][k]<dp[i][j]+1)
dp[j][k] = dp[i][j]+1;
if (tmax<dp[j][k])
tmax = dp[j][k];
}
}
}
printf("%d\n", tmax);
}
return 0;
}
这是我的代码,但是过不了OJ。请求支援