hdu 5400 Arithmetic Sequence
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
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.
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).
#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的更多相关文章
- hdu 5400 Arithmetic Sequence(模拟)
Problem Description A sequence b1,b2,⋯,bn are called (d1,d2)-arithmetic sequence ≤i≤n) such that ≤j& ...
- 水题 等差数列HDU 5400 Arithmetic Sequence
主要是要知道它对于等差数列的定义,单个数也可以作为等差数列且一定满足题意,另外就是要算清楚区间与区间的关系,考虑两大类情况,一种是d1区间和d2区间连在一起,另外一种情况就是其余情况. #includ ...
- hdoj 5400 Arithmetic Sequence
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5400 水题 #include<stdio.h> typedef long long LL; ...
- 构造 HDOJ 5400 Arithmetic Sequence
题目传送门 题意:问有多少个区间,其中存在j使得ai + d1 == ai+1(i<j) && ai + d2 == ai+1 (i>j) 构造:用c1[i], c2[i] ...
- (模拟)Arithmetic Sequence -- HDU -- 5400
链接: http://acm.hdu.edu.cn/showproblem.php?pid=5400 Time Limit: 4000/2000 MS (Java/Others) Memory ...
- HDU 5860 Death Sequence(死亡序列)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 1711 Number Sequence(数列)
HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU 1005 Number Sequence(数列)
HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- Arithmetic Sequence(dp)
Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 51 Solved: 19[Submit][Status][We ...
随机推荐
- jsp之jsp基础
1. Jsp生命周期 客户端第一次请求->web容器把jsp文件转译为servlet源文件(java)->编译为class文件->载入class文件生成servlet对象 2. Js ...
- Oracle® Database Patch 19121551 - Database Patch Set Update 11.2.0.4.4 (Includes CPUOct2014) - 傲游云浏览
Skip Headers Oracle® Database Patch 19121551 - Database Patch Set Update 11.2.0.4.4 (Includes CPUOct ...
- WEBUS2.0 In Action - 搜索操作指南 - (3)
上一篇:WEBUS2.0 In Action - 搜索操作指南(2) | 下一篇:WEBUS2.0 In Action - 搜索操作指南(4) 3. 评分机制 (Webus.Search.IHitSc ...
- spring事务传播属性与隔离级别
一.Propagation (事务的传播属性) Propagation : key属性确定代理应该给哪个方法增加事务行为.这样的属性最重要的部份是传播行为. 有以下选项可供使用: PROPAGATIO ...
- I.MX6 shutdown by software
/************************************************************************ * I.MX6 shutdown by softwa ...
- vc判断文件是否存在
#include <io.h> #include <stdio.h> #include <stdlib.h> void main( void ) { /* Chec ...
- C++ 编写Windows service
最近实现一个windows server端守护进程启动服务功能(c++实现),遇到了一些问题,记录一下 1. 启动Service实现代码: int _tmain(int argc, TCHAR* ar ...
- Android下EditText的hint的一种显示效果------FloatLabelLayout
效果: 此为EditText的一种细节,平时可能用的不多,但是用户体验蛮好的,特别是当注册页面的项目很多的时候,加上这种效果,体验更好 仅以此记录,仅供学习参考. 参考地址:https://gist. ...
- Java自动装箱拆箱
一.装箱.拆箱定义 如果一个int型量被传递到需要一个Integer对象的地方,那么,编译器将在幕后插入一个对Integer构造方法的调用,这就叫做自动装箱.而如果一个Integer对象被放到需要in ...
- android4.0以上部分手机绘图时会出现重影
canvas外层的div需要设定属性style="overflow:visible;-webkit-transform: translateZ(0);