标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1445 Accepted Submission(s): 632
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <queue> #include <algorithm> using namespace std; typedef long long LL; const int N = 100005; int a[N]; LL l[N],r[N],ans; ///l[i]记录第i个数左边与其成方差为d1的数的个数,r[i]记录第i个数右边与其方差为 ///d2的数的个数(包括自身) int main() { int n,d1,d2; while(scanf("%d%d%d",&n,&d1,&d2)!=EOF){ for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } l[1] = 1,r[n]=1; ans = 0; for(int i=2;i<=n;i++){ ///预处理 if(a[i]==a[i-1]+d1){ l[i] = l[i-1]+1; }else l[i] = 1; } for(int i=n-1;i>=0;i--){ if(a[i]+d2==a[i+1]){ r[i] = r[i+1]+1; }else r[i]=1; } if(d1==d2){ for(int i=1;i<=n;i++){ ans+=l[i]; } } else for(int i=1;i<=n;i++){ ///乘法原理 // printf("%lld %lld\n",l[i],r[i]); ans+=l[i]*r[i]; } printf("%lld\n",ans); } }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5596866.html