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. ASP.NET MVC+EasyUI+Entity FrameWork 整合开发

    本文详细讲解怎么用ASP.NET MVC+EasyUI+Entity FrameWork 来开发一个项目 对于ASP.NET MVC的Jscript库,主要引用 <script type=.mi ...

  2. Java-利用spring发送邮件

    最近项目中需要发送邮件的功能,于是百度一大把例子.但是有很多都是一样的,一点特点都没有.所以决定整理一番.         在spring2.X以后的版本就提供了org.springframework ...

  3. Oracle安装错误ora-00922(zhuan)

    Oracle安装错误ora-00922(缺少或无效选项) (2012-03-19 10:49:27) 转载▼ 标签: 杂谈   安装Oracle 11g R2的过程中,在新建数据库实例时出现了该错误, ...

  4. 介绍 Android 的 Camera 框架

    总体介绍 Android Camera 框架从整体上看是一个 client/service 的架构,有两个进程:一个是 client 进 程,可以看成是 AP 端,主要包括 JAVA 代码与一些 na ...

  5. VIM Ctrl-V Conflict with Windows Paste

    /************************************************************************************** * VIM Ctrl-V ...

  6. shell 里把命令的输出赋给变量 以及变量的使用

    //获取本月1号 的命令 date +%Y-$m-1 shell脚本  把时间命令的值赋给变量 并使用 #! /bin/sh #赋值 time=$(date +%Y-%m-) #使用变量(转换成时间戳 ...

  7. cocos2d-x 2.0 序列帧动画 深入分析

    转自:http://blog.csdn.net/honghaier/article/details/8222401 序列帧动画主要有几个类: CCSpriteFrame:精灵帧信息,序列帧动画是依靠多 ...

  8. Task和BackTask

    一.总结性知识点:     1.Android应用运行时会创建任务Task,用于存放主窗口     2.每一个任务包含一个堆栈数据结构,用于保存当前应用已创建的窗口对象,这个堆栈即回退栈BackSta ...

  9. 你能识别这些科技公司的真假logo吗?

    快告诉我,不止我一个眼瞎~

  10. C# 使用NPlot绘图技巧

    原文 C# 使用NPlot绘图技巧 图表控件一直是很难找的,特别是免费又强大的.NPlot是一款非常难得的.Net平台下的图表控件,能做各种曲线图,柱状图,饼图,散点图,股票图等,而且它免费又开源,使 ...