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 ...
随机推荐
- Postman Tests脚本的使用
直接在Tests中写js代码断言结果,Test Result展示运行结果,简单方便. 示例脚本: var jsonData = JSON.parse(responseBody); var num = ...
- Monte Carlo Policy Evaluation
Model-Based and Model-Free In the previous several posts, we mainly talked about Model-Based Reinfor ...
- 初学Java总结
经过了一年C语言的学习,自己也渐渐懂了一些东西.由于二者有共通之处,所以在刚开始学习Java的过程中,并没有刚开始学习C语言的时候那么晦涩难懂. 第一周: 1)了解了Java的发展过程以及JDK的安装 ...
- MD5加密 和 自定义加密解密
public class EncryptString { /// <summary> /// MD5加密 /// </summary> /// <param name=& ...
- npm模块管理器
npm模块管理器 地址: http://javascript.ruanyifeng.com/nodejs/npm.html#
- 不用找了,基于 Redis 的分布式锁实战来了!
Java技术栈 www.javastack.cn 优秀的Java技术公众号 作者:菜蚜 my.oschina.net/wnjustdoit/blog/1606215 前言:在分布式环境中,我们经常使用 ...
- Qt之UI文件设计和运行机制
1.项目文件组成在QtCreator中新建一个WidgetApplocation项目,选中窗口基类中选中QWidget作为窗口基类,并选中"GnerateForm"复选框.创建后项 ...
- VS2015+QT环境配置后,Lauch Qt Designer打开失败,无法打开*.ui文件
最近在VS2015上配置QT时出现了这个问题,遂百度其解决方法,解决之后记录下来.第一步: 在[解决方案资源管理器]中,右击你的 xxx.ui文件,选择[打开方式],此时列表中默认值是[ Qt des ...
- [BNDSOJ] 小P的数列代码
感谢gjznb大佬的帮助Orz #include<bits/stdc++.h> using namespace std; ; vector<int> dp[N][N]; ]; ...
- Django中ORM的聚合索引
Django中ORM的聚合索引 在Django中,聚合函数是通过aggregate方法实现的,aggregate方法返回的结果是一个字典 在使用时需要先导入模块from django.db.mod ...