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 ...
随机推荐
- 类Runtime
Runtime类的概述和使用 Runtime类概述 每个Java应用程序都有一个Runtime类实例,使应用程序能够与其运行的环境相连接.可以通过getRuntime方法获取当前运行时. 应用程序不能 ...
- 流程控制: if分支 while循环 for循环
流程控制 Python程序执行,一定按照某种规律在执行 1.宏观一定是自上而下(逻辑上方代码一定比逻辑下方代码先执行):顺序结构 2.遇到需要条件判断选择不同执行路线的执行方式:分支结构 3.有些事情 ...
- Spring Framework基础学习
Spring Framework基础学习 Core support for dependency injection,transaction management,web applications,d ...
- Python3数据分析与挖掘建模实战 学习 教程
Python3数据分析与挖掘建模实战 学习 教程 Python数据分析简介Python入门 运行:cmd下"python hello.py" 基本命令: 第三方库安装Windows ...
- IDEA+java通过SSH来进行分析日志,实现UI自动化动态验证码登录
在我写自动化脚本的时候是要真实发送验证码才能往下进行UI自动化 思路:验证码会显示在哪些地方,手机短信?数据库存储?日志? 完整代码如下: package guanyu.tools; import c ...
- mysql修改库、表、字段 字符集,中文排序
查看字段编码: show full columns from t2;show variables like '%character%';show variables like 'collation_% ...
- 基于Filter实现Gzip数据压缩
在web开发中,当服务器端向客户端返回的数据量比较大时,我们可以通过Gzip对数据进行压缩处理 注意:如果小数据量进行压缩,压缩后的数据可能比原始数据还大:所以response返回数据量比较小时不推荐 ...
- vue 运行时报 dependency was not found:错误
这种报错我知道的有两种情况引起: 第一种: 是在引入文件的时候路径不对, 解决办法是: 只要在加一个./就行了: import test from './test' 改成先对路径 如果是安装的模块的话 ...
- 补码一位乘法(Booth算法,C语言实现)
补码一位乘法 首先了解下什么是补码? 补码概念的理解,需要先从“模”的概念开始. 我们可以把模理解为一个容器的容量.当超出这个 容量时,会自动溢出.如:我们最常见到的时钟,其容量 是 12,过了 12 ...
- Arcmap10.7连接oracle,但不装oracle客户端的配置
环境:arcgis 10.7,oracle服务端12cR1.理论上其他版本方法一样 使用情况:一般开发人员不安装oracle服务端,甚至oracle客户端也不装,此时要用arcmap连oracle需要 ...