首页 > 代码库 > HDU 5400 Arithmetic Sequence

HDU 5400 Arithmetic Sequence

HDU 5400 Arithmetic Sequence

/**
HDU 5400 Arithmetic Sequence
直接预处理求解就好了
预处理找出以a[i]结尾最长的subArr长度(满足条件的)
*/

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define maxn 100005
#define ll __int64
using namespace std;
int a[maxn];
ll d[maxn];//d[i] 以a[i]结尾最长的subArr长度
int main(){
    int n,d1,d2;
    while(~scanf("%d%d%d",&n,&d1,&d2)){
        for(int i=1;i<=n; i++)
            scanf("%d",&a[i]);
        d[1]=1;
        for(int i=2;i<=n;i++){
            if(a[i]-a[i-1]==d1&&(i-2==0||a[i-1]-a[i-2]==d1))
                d[i]=d[i-1]+1;
            else if(a[i]-a[i-1]==d2&&(i-2==0||a[i-1]-a[i-2]==d1))
                d[i]=d[i-1]+1;
            else if(a[i]-a[i-1]==d2&&(i-2==0||a[i-1]-a[i-2]==d2))
                d[i]=d[i-1]+1;
            else if(a[i]-a[i-1]==d1||a[i]-a[i-1]==d2) d[i]=2;
            else d[i]=1;
        }
        ll ans=0;
        for(int i=1;i<=n;i++)
            ans+=d[i];
        printf("%I64d\n",ans);
    }
    return 0;
}


HDU 5400 Arithmetic Sequence