https://vjudge.net/problem/2198220/origin

https://vjudge.net/problem/2198220/origin
枚举等差数列第一个和第二个,然后二分确定数列后面是否存在,复杂度比较玄学,卡过了。

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<map>
#include<stack>
#include<cstring>
#define inf 2147483647
#define ls rt<<1
#define rs rt<<1|1
#define lson ls,nl,mid,l,r
#define rson rs,mid+1,nr,l,r
#define N 5010
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()

using namespace std;
int n;
int a[N],ans,d,now,num;
void in(int &x){
    int y=1;
    char c=g();x=0;
    while(c<'0'||c>'9'){
        if(c=='-')y=-1;
        c=g();
    }
    while(c<='9'&&c>='0'){
        x=(x<<1)+(x<<3)+c-'0';c=g();
    }
    x*=y;
}
void o(int x){
    if(x<0){
        p('-');
        x=-x;
    }
    if(x>9)o(x/10);
    p(x%10+'0');
}
int main(){
    in(n);
    For(i,1,n)
        in(a[i]);
    sort(a+1,a+n+1);
    ans=2;
    For(i,1,n-ans)
        For(j,i+1,n-ans+1){
            d=a[j]-a[i];
            now=2;
            num=a[j];
            while(1){
                num+=d;
                if(binary_search(a+1,a+n+1,num))
                    now++;
                else{
                    ans=max(ans,now);
                    break;
                }
            }
        }
    o(ans);
    return 0;
}

 

 

也可以dp做,f[j][i]=max(f[j][i],f[i][pre]+1);
f[j][i]表示j是等差数列最后一个下标,i是倒数第二个下标

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<map>
#include<stack>
#include<cstring>
#define inf 2147483647
#define ls rt<<1
#define rs rt<<1|1
#define lson ls,nl,mid,l,r
#define rson rs,mid+1,nr,l,r
#define N 5010
#define For(i,a,b) for(int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()

using namespace std;
int n;
int a[N],ans,d,now,num,pre,f[N][N];
void in(int &x){
    int y=1;
    char c=g();x=0;
    while(c<'0'||c>'9'){
        if(c=='-')y=-1;
        c=g();
    }
    while(c<='9'&&c>='0'){
        x=(x<<1)+(x<<3)+c-'0';c=g();
    }
    x*=y;
}
void o(int x){
    if(x<0){
        p('-');
        x=-x;
    }
    if(x>9)o(x/10);
    p(x%10+'0');
}
int main(){
    in(n);
    For(i,1,n)
        in(a[i]);
    sort(a+1,a+n+1);
    ans=2;
    For(i,1,n)
        For(j,1,n)
            f[i][j]=2;
    For(i,1,n){
        pre=i-1;
        For(j,i+1,n){
            while(pre>0&&a[j]-a[i]>a[i]-a[pre]) pre--;
            if(!pre) break;
            if(pre>0&&a[j]-a[i]==a[i]-a[pre])
                f[j][i]=max(f[j][i],f[i][pre]+1);
            ans=max(ans,f[j][i]);
        }
    }
    o(ans);
    return 0;
}

 

posted @ 2019-07-16 20:16  WeiAR  阅读(897)  评论(0编辑  收藏  举报