1605: nc与数列
Time Limit: 2000 MS Memory Limit: 64 MBSubmit: 84 Solved: 13
[ Submit][ Status][ Web Board]
Description
nc最近很无聊~所以他总是想各种有趣的问题来打发时间。
nc在地上写了一些数字,他发现有一些有趣的数列:这些数列是非递减的,且从第三个数开始,数字的大小总是前两个数的和。如著名的Fibonacci数列:1 2 3 5 8 13 ...,或者其他满足条件的数列:2 2 4 6 10 16。他现在给你n个数字,想让你从中取出尽量多的数字,对其重新排列后使其满足上述条件,并输出其长度
Input
第一行为N,表示有N个数字,1<=N<=1000
以下有N个数字a1...an,其中0<=ai<=10^9。
Output
一个整数,表示最长有趣数列的长度。
Sample Input
6 5 4 3 2 1
Sample Output
HINT
我们可以找到许多满足条件的序列:
如:
1
5 6
2 4 5
1 2 3 5
由于最长的序列为1 2 3 5,故我们只需输出其长度4。
Source
题目链接:
http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1605
题目大意:
题目给出一些数a[i],求从中取出最多的数,且能够构成下述数列:
一个非递减数列,且从第三个数开始,数字的大小总是前两个数的和。
问最多能取出多少数。
题目思路:
【动态规划】
先把所有数从小到大排序,f[i][j]表示前i个数,构成数列最后一项为a[j]的最长数列长度。
转移的时候因为有a[i]和a[j]可以推出再之前的数。
找上一个数可以用二分但我用了for(因为懒)
/****************************************************
Author : Coolxxx
Copyright 2017 by Coolxxx. All rights reserved.
BLOG : http://blog.youkuaiyun.com/u010568270
****************************************************/
#include<bits/stdc++.h>
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define mem(a,b) memset(a,b,sizeof(a))
const double EPS=1e-8;
const int J=10;
const int MOD=100000007;
const int MAX=0x7f7f7f7f;
const double PI=3.14159265358979323;
const int N=1004;
const int M=14;
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
int n,m,lll,ans;
int a[N];
int f[N][N];
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
int x,y,z;
// for(scanf("%d",&cass);cass;cass--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s))
while(~scanf("%d",&n))
{
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+1+n);
if(n<3)
{
printf("%d\n",n);
continue;
}
ans=2;
f[1][0]=f[2][0]=1;
f[2][1]=2;
for(i=3;i<=n;i++)
{
for(j=i-1;j>=0;j--)
{
f[i][0]=1;
f[i][j]=2;
for(k=j-1;k>=0;k--)
{
if(a[j]+a[k]<a[i])break;
if(a[j]+a[k]==a[i])
f[i][j]=max(f[i][j],f[j][k]+1);
}
ans=max(f[i][j],ans);
}
}
printf("%d\n",ans);
}
return 0;
}
/*
//
//
*/