51nod1055 最长等差数列

$n \leq 10000$的不同数字,问能组成的最长等差数列。

$f(i,j)$--$i,j$结尾的最长等差数列。枚举中间$i$,往左右扩展,找$j,k$,用$f(j,i)$来更新$f(i,k)$。

$n \leq 100000$的暂时不会。

 1 //#include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 //#include<time.h>
 5 //#include<complex>
 6 //#include<set>
 7 //#include<queue>
 8 //#include<vector>
 9 #include<algorithm>
10 #include<stdlib.h>
11 using namespace std;
12 
13 #define LL long long
14 int qread()
15 {
16     char c; int s=0,f=1; while ((c=getchar())<'0' || c>'9') (c=='-') && (f=-1);
17     do s=s*10+c-'0'; while ((c=getchar())>='0' && c<='9'); return s*f;
18 }
19 
20 //Pay attention to '-' , LL and double of qread!!!!
21 
22 int n;
23 short f[10002][10002];
24 int a[10011];
25 int main()
26 {
27     n=qread();
28     for (int i=1;i<=n;i++) a[i]=qread();
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             f[i][j]=2;
33     for (int i=1;i<=n;i++)
34     {
35         int j=i-1,k=i+1,v=a[i]*2;
36         while (j && k<=n)
37         {
38             int u=a[j]+a[k];
39             if (u<v) k++;
40             else if (u>v) j--;
41             else if (u==v) {f[i][k]=max(f[i][k],short(f[j][i]+1)); j--; k++;}
42         }
43     }
44     int ans=0;
45     for (int i=1;i<=n;i++)
46         for (int j=i+1;j<=n;j++)
47             ans=max(ans,(int)f[i][j]);
48     printf("%d\n",ans);
49     return 0;
50 }
View Code

 

posted @ 2018-06-11 21:33  Blue233333  阅读(172)  评论(0编辑  收藏  举报