1. 程式人生 > >51nod 1055 最長等差數列

51nod 1055 最長等差數列

http pro sta name r+ cnblogs ble targe type

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1055

題意:

技術分享

思路:
先固定一個位置,然後從該中心點出發向兩邊掃,確實很難想到啊。。。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<vector>
 6 #include<stack>
 7 #include<queue>
 8
#include<cmath> 9 #include<map> 10 #include<set> 11 using namespace std; 12 typedef long long ll; 13 typedef pair<int,int> pll; 14 const int INF = 0x3f3f3f3f; 15 const int maxn=10000+5; 16 17 int n; 18 19 short int dp[maxn][maxn]; 20 int a[maxn]; 21 22 int main()
23 { 24 //freopen("in.txt","r",stdin); 25 while(~scanf("%d",&n)) 26 { 27 memset(dp,0,sizeof(dp)); 28 for(int i=1;i<=n;i++) scanf("%d",&a[i]); 29 sort(a+1,a+1+n); 30 for(int i=1;i<=n;i++) 31 for(int j=i+1;j<=n;j++) 32 dp[i][j]=2
; 33 34 int ans=2; 35 for(int i=n-1;i>1;i--) 36 { 37 int l=i-1,r=i+1; 38 while(l>0 && r<=n) 39 { 40 if(a[l]+a[r]>2*a[i]) l--; 41 else if(a[l]+a[r]<2*a[i]) r++; 42 else 43 { 44 dp[l][i]=dp[i][r]+1; 45 if(dp[l][i]>ans) ans=dp[l][i]; 46 l--;r++; 47 } 48 } 49 } 50 printf("%d\n",ans); 51 } 52 return 0; 53 }

51nod 1055 最長等差數列