hdu 5400 Arithmetic Sequence(重构数组)

来源:互联网 发布:dns的协议端口号 编辑:程序博客网 时间:2024/05/29 10:55

Arithmetic Sequence

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1430    Accepted Submission(s): 625


Problem Description
A sequence b1,b2,,bn are called (d1,d2)-arithmetic sequence if and only if there exist i(1in) such that for every j(1j<i),bj+1=bj+d1 and for every j(ij<n),bj+1=bj+d2.

Teacher Mai has a sequence a1,a2,,an. He wants to know how many intervals [l,r](1lrn) there are that al,al+1,,ar are (d1,d2)-arithmetic sequence.
 

Input
There are multiple test cases.

For each test case, the first line contains three numbers n,d1,d2(1n105,|d1|,|d2|1000), the next line contains n integers a1,a2,,an(|ai|109).
 

Output
For each test case, print the answer.
 

Sample Input
5 2 -20 2 0 -2 05 2 32 3 3 3 3
 

Sample Output
125

题意:求一个区间,使得区间内存在一点i,序列[l,i)是公差为d1的等差数列,序列[i,r]是公差为d2的等差数列,问你一共有几个区间满足

思路:枚举i点即可,但是直接枚举会有O(n²)的复杂度会炸掉,所以要用l和r数组分别记录当前点最大能延伸到前面和后面的距离,最后相乘即可,注意两点

1:当d1=d2的时候,区间会算重,此时只需要特判即可,值等于所有l或者所有r

2:l和r数组必须为long long 10^6*10^6会超出int型....因为这个WA了好多次。

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;#define N 100010#define LL long longint a[N];LL l[N],r[N];int main(){    int n,d1,d2;    while(~scanf("%d %d %d",&n,&d1,&d2))    {        long long ans=0;        for(int i=1; i<=n; i++)        {            scanf("%d",&a[i]);            l[i]=r[i]=1;        }        for(int i=2; i<=n; i++)            if(a[i]==a[i-1]+d1)                l[i]=l[i-1]+1;        for(int i=n-1; i>=1; i--)            if(a[i+1]==a[i]+d2)                r[i]=r[i+1]+1;        if(d1==d2)        {            for(int i=1; i<=n; i++)                ans+=l[i];        }        else        {            for(int i=1; i<=n; i++)                ans+=l[i]*r[i];        }        printf("%lld\n",ans);    }    return 0;}


0 0
原创粉丝点击