http://acm.hdu.edu.cn/showproblem.php?pid=5400

Arithmetic Sequence

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

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
 
题目大意:有一种区间[l,r],存在i使得当l<=j<=i时,a[j+1]=a[j]+d1,当i<=j<=r时a[j+1]=a[j]+d2;
其实这种区间就是[l,i]区间a中元素是以d1位公差的等差数列,[i,r]区间a中的元素是以d2为公差的等差数列
求这样的区间有多少种
 
枚举i(1<=i<=n),预处理出以i为中心,左侧是满足以d1为公差的等差数列的最大长度al[ i ],
右侧是满足以d2为公差的等差数列的最大长度ar[ i ];
 
1、如果d1=d2,  那么以i为一个解的区间个数为 ans +=al[i](或ar[i]);
2、如果d1!=d2, 那么以i为一个解的区间个数为al[ i ]*ar[ i ]
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#define N 100010 using namespace std; long long a[N], al[N], ar[N];//al[i]表示从左边开始(以d1为公差的等差数列)记录元素的个数,ar[i]表示从右边开始(以d2为公差的等差数列)记录元素的个数 int main()
{
long long n, d1, d2, i;
while(~scanf("%lld%lld%lld", &n, &d1, &d2))
{
for(i = ; i <= n ; i++)
scanf("%lld", &a[i]);
al[] = ar[n] = ;//初始化,最开始从左边开始i=1和从右边开始i=n时等差数列中的元素个数都为1
for(i = ; i <= n ; i++)
{
if(a[i] == a[i - ] + d1)
al[i] = al[i - ] + ;//如果满足条件即a[i]是以d1为公差的等差数列里的元素,所以元素个数在本来的基础上+1
else
al[i] = ;//不满足条件则a[i]不是以d1为公差的等差数列里的元素,此时a[i]是另一个数列。其元素个数为1
}
for(i = n - ; i >= ; i--)
{
if(a[i] == a[i + ] - d2)//由a[i+1]=a[i]+d2转化而来
ar[i] = ar[i + ] + ;
else
ar[i] = ;
}
long long ans = ;
for(i = ; i <= n ; i++)
{
if(d1 == d2)
ans += al[i];
else
ans += al[i] * ar[i];
}
printf("%lld\n", ans);
}
return ;
}

hdu 5400 Arithmetic Sequence的更多相关文章

  1. hdu 5400 Arithmetic Sequence(模拟)

    Problem Description A sequence b1,b2,⋯,bn are called (d1,d2)-arithmetic sequence ≤i≤n) such that ≤j& ...

  2. 水题 等差数列HDU 5400 Arithmetic Sequence

    主要是要知道它对于等差数列的定义,单个数也可以作为等差数列且一定满足题意,另外就是要算清楚区间与区间的关系,考虑两大类情况,一种是d1区间和d2区间连在一起,另外一种情况就是其余情况. #includ ...

  3. hdoj 5400 Arithmetic Sequence

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5400 水题 #include<stdio.h> typedef long long LL; ...

  4. 构造 HDOJ 5400 Arithmetic Sequence

    题目传送门 题意:问有多少个区间,其中存在j使得ai + d1 == ai+1(i<j) && ai + d2 == ai+1 (i>j) 构造:用c1[i], c2[i] ...

  5. (模拟)Arithmetic Sequence -- HDU -- 5400

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=5400 Time Limit: 4000/2000 MS (Java/Others)    Memory ...

  6. HDU 5860 Death Sequence(死亡序列)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  7. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  8. HDU 1005 Number Sequence(数列)

    HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...

  9. Arithmetic Sequence(dp)

    Arithmetic Sequence Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 51  Solved: 19[Submit][Status][We ...

随机推荐

  1. 戏(细)说Executor框架线程池任务执行全过程(下)

    上一篇文章中通过引入的一个例子介绍了在Executor框架下,提交一个任务的过程,这个过程就像我们老大的老大要找个老大来执行一个任务那样简单.并通过剖析ExecutorService的一种经典实现Th ...

  2. (六)6.9 Neurons Networks softmax regression

    SoftMax回归模型,是logistic回归在多分类问题的推广,即现在logistic回归数据中的标签y不止有0-1两个值,而是可以取k个值,softmax回归对诸如MNIST手写识别库等分类很有用 ...

  3. 【转】Eclipse+PyDev 安装和配置

    原文网址:http://www.51testing.com/html/67/589567-866611.html Python开发有很多工具,其中Eclipse+Pydev 是最常见的一种.本文简单介 ...

  4. android adb应用

    一 adb 简介 ADB是一个 客户端-服务器端 程序, 其中客户端是你用来操作的电脑, 服务器端是android设备. 二 安装 方法 先说安装方法, 电脑上需要安装客户端. 客户端包含在sdk里. ...

  5. js match regex

    需要返回成数组元素的要放在括号里头 var arr = /input-([0-9]*)-([0-9]*)/.exec(id); var all = arr[0]; var row = arr[1]; ...

  6. EL表达式(胖先生版)

    EL表达式没有指定范围,从最小范围开始 <% pageContext.setAttribute("shxt", "java web"); request. ...

  7. ios 页面传值4种方式(一) 之全局变量

    通用的是用代理的方式实现页面传值,但是有时候利用其它方式也可以很巧妙的解决问题,页面传值一共有4种方式: 1.使用全局变量, SharedApplication,定义一个变量来传递. 2.使用文件,或 ...

  8. Eclipse for PHP Developers + xamp +xdebug

    php 开发环境搭建 1.安装xamp 我的版本是v3.2.1 2.安装下载“Eclipse for PHP Developers”解压即可使用 3.配置“Eclipse for PHP Develo ...

  9. poj 2923(状态压缩dp)

    题意:就是给了你一些货物的重量,然后给了两辆车一次的载重,让你求出最少的运输次数. 分析:首先要从一辆车入手,搜出所有的一次能够运的所有状态,然后把两辆车的状态进行合并,最后就是解决了,有两种方法: ...

  10. JAVA多线程二

    Thread.Join() join()函数表示等待当前线程结束,然后返回. public final synchronized void join(long millis) throws Inter ...