Arithmetic Sequence

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

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

Teacher Mai has a sequence a1,a2,⋯,an. He wants to know how many intervals [l,r](1≤l≤r≤n) 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(1≤n≤105,|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 -2
0 2 0 -2 0
5 2 3
2 3 3 3 3
 
Sample Output
12
5
 
Author
xudyh
 
对每个数先预处理出左右能够达到的最远距离。然后当d1!=d2时,用乘法原理得到区间数(左边有l[i]个区间,右边有r[i]个区间,左右就是l[i]*r[i]个区间)
当d1==d2 时,左右会算重,只算左边就好了。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = ;
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=;i<=n;i++){
scanf("%d",&a[i]);
}
l[] = ,r[n]=;
ans = ;
for(int i=;i<=n;i++){ ///预处理
if(a[i]==a[i-]+d1){
l[i] = l[i-]+;
}else l[i] = ;
}
for(int i=n-;i>=;i--){
if(a[i]+d2==a[i+]){
r[i] = r[i+]+;
}else r[i]=;
}
if(d1==d2){
for(int i=;i<=n;i++){
ans+=l[i];
}
}
else
for(int i=;i<=n;i++){ ///乘法原理
// printf("%lld %lld\n",l[i],r[i]);
ans+=l[i]*r[i];
}
printf("%lld\n",ans);
}
}

hdu 5400(思路题)的更多相关文章

  1. hdu 4908(思路题)

    BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. Proud Merchants HDU - 3466 (思路题--有排序的01背包)

    Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerfu ...

  3. hdu 5191(思路题)

    Building Blocks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  4. hdu 5101(思路题)

    Select Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  5. hdu 5063(思路题-反向操作数组)

    Operation the Sequence Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  6. hdu 4859(思路题)

    Goffi and Squary Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  7. hdu 4956(思路题)

    Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. HDU 1173 思路题

    题目大意 有n个地点(坐标为实数)需要挖矿,让选择一个地点,使得在这个地方建造基地,到n个地点的距离和最短,输出基地的坐标. 题解+代码: 1 /* 2 把这个二维分开看(即把所有点投影到x轴上,再把 ...

  9. 51nod P1305 Pairwise Sum and Divide ——思路题

    久しぶり! 发现的一道有意思的题,想了半天都没有找到规律,结果竟然是思路题..(在大佬题解的帮助下) 原题戳>>https://www.51nod.com/onlineJudge/ques ...

随机推荐

  1. 201621123080《java程序设计》第六周作业总结

    201621123080<java程序设计>第六周作业总结 1. 本周学习总结 2. 书面作业 clone方法 1.1 在test1包中编写Employee类,在test2包中新建一个Te ...

  2. 序列内置方法详解(string/list/tuple)

    一.常用方法集合 1.1.string,字符串常用方法 以下举例是python2.7测试: 函数名称 作用 举例 str.capitalize() 字符串第一个字符如果是字母,则把字母替换为大写字母. ...

  3. Applied Nonparametric Statistics-lec4

    Ref: https://onlinecourses.science.psu.edu/stat464/print/book/export/html/5 Two sample test 直接使用R的t- ...

  4. 二叉排序树:HDU3791-二叉搜索树(用指针建立二叉排序树)

    二叉搜索树 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Descr ...

  5. WPF触控程序开发(三)——类似IPhone相册的反弹效果

    用过IPhone的都知道,IPhone相册里,当图片放大到一定程度后,手指一放,会自动缩回,移动图片超出边框后手指一放,图片也会自动缩回,整个过程非常和谐.自然.精确,那么WPF能否做到呢,答案是肯定 ...

  6. 使用像AdminLTE的前端框架,树形导航菜单实现方式都有哪些?

    之前用easyui等富前端框架开发的时候都是使用封装好的县城的插件,现在使用最新的类似AdminLTE似的前段框架实现树形菜单都用什么方式? 后台拼接html然后前端用JS append方法添加还是直 ...

  7. multiprocessing join与lock区别

    加锁 join方法 join方法会造成阻塞,在上一个进程完成之前不会运行join()后面的代码 lock  仍会执行之后的代码,遇到创建进程,会发向操作系统发出指令,但不会执行,等到上锁的进程结束之后 ...

  8. 光学字符识别OCR-4

    经过第一部分,我们已经较好地提取了图像的文本特征,下面进行文字定位. 主要过程分两步:         1.邻近搜索,目的是圈出单行文字:         2.文本切割,目的是将单行文本切割为单字. ...

  9. iOS 9下支持的键盘类型:

    http://blog.csdn.net/cloudox_/article/details/50532124

  10. Tensorflow 自适应学习速率

    Tensorflow 自适应学习速率 在模型的初期的时候,往往设置为较大的学习速率比较好,因为距离极值点比较远,较大的学习速率可以快速靠近极值点:而,后期,由于已经靠近极值点,模型快收敛了,此时,采用 ...