1. 程式人生 > >HDU 5400 Arithmetic Sequence

HDU 5400 Arithmetic Sequence

names [1] target scanf targe sub mes lan code

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