Arithmetic Sequence
Arithmetic Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
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<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = ;
const int oo = 0x3f3f3f3f;
long long al[N], ar[N], as[N], n; // al数组存从左边到这个点一直满足的+d1的数有几个,ar是往右边数满足+d2的有几个。
int main()
{
long long d1, d2, i, ans;
while(~scanf("%lld %lld %lld", &n, &d1, &d2))
{
for(i = ; i <= n; i++)
scanf("%lld", &as[i]);
al[] = ar[n] = ;
for(i = ; i <= n; i++)
if(as[i] == as[i-]+d1)
al[i] = al[i-]+;
else al[i] = ; // 只要间隔,不满足了那么连续的就是1个,它自身
for(i = n-; i >= ; i--)
{
if(as[i]+d2 == as[i+])
ar[i] = ar[i+]+;
else ar[i] = ;
}
ans = ;
for(i = ; i <= n; i++)
{
if(d1 == d2) ans += al[i]; // 如果d1,d2相等,al直接相加
else ans += al[i]*ar[i]; // if不等,就等于两者相乘,即间隔种类数
}
printf("%lld\n", ans);
}
return ;
}
Arithmetic Sequence的更多相关文章
- hdu 5400 Arithmetic Sequence
http://acm.hdu.edu.cn/showproblem.php?pid=5400 Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Ot ...
- hdu 5400 Arithmetic Sequence(模拟)
Problem Description A sequence b1,b2,⋯,bn are called (d1,d2)-arithmetic sequence ≤i≤n) such that ≤j& ...
- Arithmetic Sequence(dp)
Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 51 Solved: 19[Submit][Status][We ...
- [Swift]LeetCode1027. 最长等差数列 | Longest Arithmetic Sequence
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall t ...
- (模拟)Arithmetic Sequence -- HDU -- 5400
链接: http://acm.hdu.edu.cn/showproblem.php?pid=5400 Time Limit: 4000/2000 MS (Java/Others) Memory ...
- HZAU 21——Arithmetic Sequence——————【暴力 or dp】
Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1810 Solved: 311[Submit][Status] ...
- 华中农业大学第四届程序设计大赛网络同步赛-1020: Arithmetic Sequence,题挺好的,考思路;
1020: Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MB Submit: ->打开链接<- Descriptio ...
- LeetCode 1027. Longest Arithmetic Sequence
原题链接在这里:https://leetcode.com/problems/longest-arithmetic-sequence/ 题目: Given an array A of integers, ...
- 【leetcode】1027. Longest Arithmetic Sequence
题目如下: Given an array A of integers, return the length of the longest arithmetic subsequence in A. Re ...
随机推荐
- QTP技术支持之QTP对象无法识别(转自582357212的个人空间,链接:http://www.51testing.com/html/64/305564-847787.html)
QTP自动化测试从业者,或者很多练习使用QTP开发自动化测试代码的人员遇到最多的问题恐怕就是对象无法识别了,对象无法识别原因有很多种,根据经常对QTP自动化测试脚本开发人员的技术Support,我总结 ...
- Jmeter之JDBC--Mysql
一.添加JDBC Connection Configuration控件 界面显示如下: 配置说明: 1.名称:标识 2.注释:备注信息 3.Variable Name:定义的变量名,供后续调用: 4. ...
- Python笔记(二十二)_魔法方法_基本魔法方法
__init__(self[,...]) __init__和__new__组成python的构造器,但__init__更多的是负责初始化操作,相当于一个项目中的配置文件,__new__才是真正的构造函 ...
- 【ABAP系列】SAP ABAP同时显示多个ALV的方法
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP同时显示多个AL ...
- 【ABAP系列】SAP ABAP 利用class创建客户/供应商主数据
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP ABAP 利用class创建 ...
- python每日一练:0004题
第 0004 题: 任一个英文的纯文本文件,统计其中的单词出现的个数. import re count = 0 with open('./EnglishText.txt','r') as f: tem ...
- python3.5+django2.0快速入门(一)
因为这篇教程需要用到anaconda的一些操作,如果还不懂anaconda的操作的同学可以看下这篇文章python 入门学习之anaconda篇. 创建python3+的开发环境 直接在终端输入:co ...
- SpringBoot 使用maven创建springboot项目
有两种方式可以创建 1是使用spring-boot-starter-parent ,2是使用spring-boot-dependencies (即父项目dependencyManagement) ( ...
- windows 使用nginx
windows 安装nginx 进入此地址进行下载 http://nginx.org/en/download.html 解压到相关目录 启动 start nginx 关闭 nginx -s stop ...
- Java-JavaMail邮件开发(1)Java Mail
1.仅使用Java Mail 使用163邮箱作为代理服务器: 引入依赖: <dependency> <groupId>com.sun.mail</groupId> ...