| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛

已结贴   问题点数：20  回复次数：6

1、    a[i]>0;
2、    当i>2时，a[i]=a[i-1]+a[i-2];

1 <= T <= 302
1 <= n <= 200
0 <= ai <= 10^9
∑n <= 30000

3
3
1 2 3
7
6 5 4 3 2 1 1
1
123456

3
5
1

得分:0

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

得分:20

（当然，只是类似而已，长度小于3时，leecode要求输出0）

```class Solution
{
public:
int lenLongestFibSubseq(vector<int>& A) {
return foo(A);
}
private:
size_t bar( const int* pa, const int* pb, int a, int b )
{
size_t len = 0;
for( ; pa=std::lower_bound(pa,pb,a+b), pa!=pb && *pa==a+b; ++pa )
{
++len;
b = b+a;
a = b-a;
}
return len==0 ? 0 : 2+len;
}
size_t foo( vector<int>& buf )
{
size_t maxlen = 0;

sort( buf.begin(), buf.end() );
for( size_t i=0; i!=buf.size(); ++i )
for( size_t j=i+1; j!=buf.size() && j<buf.size()-maxlen+1; ++j )
maxlen = std::max( maxlen, bar(&buf[0]+i,&buf[0]+buf.size(),buf[i],buf[j]) );

return maxlen;
}
};```

得分:0

得分:0

得分:0

得分:0

• 7
• 1/1页
• 1

Powered by Discuz, Processed in 0.030020 second(s), 8 queries.