Arithmetic Sequence
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).
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const long long maxn = 100005; 6 long long A[maxn]; 7 int main() 8 { 9 long long flag,n, d1, d2; 10 long long sum, cnt; 11 while(~scanf("%I64d %I64d %I64d", &n, &d1, &d2)) 12 { 13 for(int i = 0; i < n; i++) 14 { 15 scanf("%I64d", &A[i]); 16 } 17 18 sum = 0, flag = 0, cnt = 0; 19 20 for(int i = 1; i < n; i++) 21 { 22 if(A[i]-A[i-1] == d1 && flag == 0) 23 { 24 cnt ++; 25 continue; 26 } 27 if(A[i]-A[i-1] == d2) 28 { 29 flag = 1; 30 cnt ++; 31 continue; 32 } 33 //printf("cnt = %d\n", cnt); 34 sum += (1+cnt)*cnt/2; 35 if(cnt != 0) i--; 36 cnt = 0; 37 flag = 0; 38 } 39 sum += (1+cnt)*cnt/2; 40 //if (d1!=d2) 41 printf("%I64d\n", sum + n); 42 //else printf("%I64d\n",n); 43 } 44 return 0; 45 } 46 47 [ Copy to Clipboard ] [ Save to File]
转载于:https://www.cnblogs.com/pblr/p/4739770.html
Arithmetic Sequence相关推荐
- 构造 HDOJ 5400 Arithmetic Sequence
题目传送门 题意:问有多少个区间,其中存在j使得ai + d1 == ai+1(i<j) && ai + d2 == ai+1 (i>j) 构造:用c1[i], c2[i] ...
- HDU 5400 Arithmetic Sequence
HDU 5400 Arithmetic Sequence /** HDU 5400 Arithmetic Sequence 直接预处理求解就好了 预处理找出以a[i]结尾最长的subArr长度(满足条 ...
- Arithmetic Sequence 三分,货仓选址,nth_element,__int128(济南)
题意 : 给一序列,每次操作选择一个数加一或者减一,需要将这个序列变成等差数列,问最小操作数 思路 : 数据范围很大又没有头绪,想到二分或者三分.对于不同的公差d,肯定是只有一个最优,两边的都比这个公 ...
- XTU 1278 Arithmetic Sequence
等差数列 题目描述 给一个整数数列,请判断是否为等差数列. 输入 第一行是一个整数T(1≤T≤100),表示样例的个数. 每个样例第一行是一个整数N(3≤N≤10000),表示数列元素的个数. 第二行 ...
- LeetCode 1027. Longest Arithmetic Sequence--笔试题--C++解法
LeetCode 1027. Longest Arithmetic Sequence–笔试题–C++解法 LeetCode题解专栏:LeetCode题解 我做的所有的LeetCode的题目都放在这个专 ...
- c语言序列sequence,Sequence
Sequence Time Limit:1000MS Memory Limit:65536K Total Submit:0 Accepted:0 Description The sequence 1, ...
- LeetCode - 413. Arithmetic Slices - 含中文题意解释 - O(n) - ( C++ ) - 解题报告
1.题目大意 A sequence of number is called arithmetic if it consists of at least three elements and if th ...
- Arithmetic Slices
这两天一直复习动态规划,就想到leetcode上刷刷题,easy难度的很少,大部分都是medium和hard.本题是第一道DP类型medium难度的题目,但是用其他的方法比如暴力法也可以求解.首先来看 ...
- LeetCode 413. Arithmetic Slices
题目: A sequence of number is called arithmetic if it consists of at least three elements and if the d ...
最新文章
- phpMyAdmin的配置
- JavaScript面向对象的支持(7)
- 关闭主窗体而不退出主程序 以及如何获取操作系统的关闭、注销信息
- 2018山东省省赛 问题 H: Dominoes
- PyTorch机器学习从入门到实战-CH2
- 非线性光纤光学_1.56 m波段高能量百飞秒光纤激光器
- 评估一个垃圾收集(GC)
- Spring Cloud实战小贴士:随机端口
- SQL Server如何链接到 Oracle并查询其中的数据?并实现做接口
- “相当一部分”HPE公司OpenStack工作人员被转移至SUSE
- oracle 修改字段长度_Oracle修改字符集前如何找出可能出现问题的数据?
- 12家存在低俗内容的网络文学企业被约谈:严肃查处 深入整改
- 洛谷 P2241统计方形(数据加强版) 题解
- 如何修改SAO用户密码
- 黑客之门的魅力:感染与加载
- DirectX11--使用DirectX Tool Kit帮助开发
- 用 PHP 来刷leetCode 之 电话号码的字母组合
- Spring AOP @Aspect没反应的处理
- Visual Studio 卸载 Visual Assist番茄助手
- 在Windows下架设FTP服务器
热门文章
- 零基础入门语义分割——Task2 数据扩增
- GBDT的回归、二分类以及多分类教程
- linux下 卸载qt_Ubuntu14.04安装、配置与卸载QT5的步骤详解
- android ndk 头文件,笔记:Android源码和NDK目录及头文件
- delphi java jni 互通_编程应用实例:Java通过JNI与Delphi交互
- 战斗服务器响应超时是否尝试重连,刺激战场:教你,从开伞到落地瞬间技巧
- 基于Android中实现定时器的3种解决方法
- 《MFC游戏开发》笔记五 定时器和简单动画
- java 列表对话框
- linux 脚本做成服务,Shell脚本注册到Linux系统服务实例