BZOJ 3357--[Usaco2004]等差数列(STL&DP)

3357: [Usaco2004]等差数列

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 516  Solved: 241
[Submit][Status][Discuss]

Description

    约翰发现奶牛经常排成等差数列的号码.他看到五头牛排成这样的序号:“1,4,3,5,7”
很容易看出“1,3,5,7”是等差数列.
    给出N(1≤N≤2000)数字AI..AN(O≤Ai≤10^9),找出最长的等差数列,输出长度.

Input

    第1行:一个整数N.
    第2到N+1行:每行一个整数Ai,表示牛的号码.

Output

 
    最长等差数列的长度.

Sample Input

5
1
4
3
5
7

Sample Output

4

题目链接:

    http://www.lydsy.com/JudgeOnline/problem.php?id=3357 

Solution

  f [ i ] [ j ] 表示以a[i]为末尾,j为倒数第2个数的最长等差数列长度。。

  j的范围很大,用map维护。。。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#define pa pair<LL,LL>
#define LL long long
using namespace std;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void Out(int a){
    if(a>9) Out(a/10);
    putchar(a%10+'0');
}
const LL inf=1e9+10;
const LL mod=1e9+7;
const int N=2050;
int n,ans,a[N];
map<int,int> f[N]; 
int main(){
	n=read();
	if(n==1){
		printf("1\n");return 0;
	}
	for(int i=1;i<=n;++i)
		a[i]=read();
	for(int i=1;i<=n;++i){
		for(int j=1;j<i;++j){
			f[i][a[j]]=max(f[i][a[j]],2);
			f[i][a[j]]=max(f[i][a[j]],f[j][a[j]+a[j]-a[i]]+1);
			ans=max(ans,f[i][a[j]]);
		}
	}
	printf("%d\n",ans);
	return 0;
}

  

  

This passage is made by Iscream-2001.

 

posted @ 2018-09-18 12:42  Iscream-2001  阅读(159)  评论(0编辑  收藏  举报
/* */ /* */