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. 转 救命的教程 anaconda下载安装包网络错误的解决办法

    折腾了一天,终于找到了这个解决办法 https://blog.csdn.net/sinat_29315697/article/details/80516498

  2. 【计数】hdu5921Binary Indexed Tree

    二进制拆位计算贡献 题目描述 树状数组是一种常用的数据结构,下面是树状数组用于给区间 [1,x] 内的数加 t 的代码: void add(int x,int t){ for (int i=x;i;i ...

  3. nginx的url重写

    1.1 简介 url重写由ngx_http_rewrite_module模块提供,默认会安装,但该模块功能的实现需要pcre.URL重写技术不仅要求掌握几个指令的语法.熟悉简单的正则表达式,还需要尽量 ...

  4. MySQL_8.0.15_Windows10_X64 安装教程

    最近学习的内容涉及到MySQL的知识,所以安装一个MySQL非常有必要,参考别人的教程安装过程还算顺利,其中遇到了一些问题查了一些也解决了,这里把整个安装过程梳理一遍,给大家一个参考. 我手里的电脑是 ...

  5. 初级练手项目——用Python一步一步实现“智能”贪吃蛇!

    贪吃蛇作为一款经典的小游戏,想必大家一定并不陌生,今天我们就来用Python来设计与实现贪吃蛇游戏,回味一下童年的快乐.快跟着小编来看看吧! ​ 基本环境配置 ●版本:Python3 ●系统:Wind ...

  6. UVALive - 3942 (DP + Trie树)

    给出一个长度不超过300000的字符串 S,然后给出 n 个长度不超过100的字符串. 如果字符串可以多次使用,用这 n 个字符串组成 S 的方法数是多少? 比如样例中,abcd = a + b + ...

  7. PAT Basic 1075

    1075 链表元素分类 给定一个单链表,请编写程序将链表元素进行分类排列,使得所有负值元素都排在非负值元素的前面,而 [0, K] 区间内的元素都排在大于 K 的元素前面.但每一类内部元素的顺序是不能 ...

  8. 启动Chrome浏览器弹出“You are using an unsupported command-line flag –ignore-certificate-errors. Stability and security will suffer”

    采用如下代码: public static void launchChrome() { System.setProperty("webdriver.chrome.driver", ...

  9. python面试题解析(前端、框架和其他)

    答: HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1. ...

  10. day03_11 if语句实现猜年龄01

    老男孩猜年龄游戏 age_of_princal = 56 guess_age = int( input(">>:") ) #以下为伪代码 ''' if guess_ag ...