HDU5400 Arithmetic Sequence
解题思路:这题看懂题目是很关键的,这个区间是等差数列,且公差为d1或d2,
特别注意单个数字也为等差数列。每次求出等差数列序列长度,然后
求出对应这种长度对应有多少种组合方式,累加起来就是结果。
注意要用long long,还有注意特判数据,如 5 -1 -1 ,5 4 3 2 1;
5 1 1, 1 2 3 4 5 ; 5 1 1, 1 1 1 1 1等。
#include<cstdio>
int main()
{
int A[], n, d1, d2;
long long sum, cnt; //注意这里要用long long 否则会WA
//int sum, cnt;
while(~scanf("%d %d %d", &n, &d1, &d2))
{
sum = cnt = ;
for(int i = ; i < n; i++) scanf("%d", &A[i]);
for(int i = ; i < n; i++)
{
if(A[i]-A[i-] == d1)
{
while(A[i]-A[i-] == d1)
{
i ++;
cnt ++;
if(i == n) break;
}
if(i == n) break; //i为n时要及时跳出,其它地方同理。
while(A[i]-A[i-] == d2)
{
i ++;
cnt ++;
if(i == n) break;
}
sum += (cnt+)*cnt/; //满足条件的序列长度为cnt+1时,共有(cnt+1)*cnt/2种组合方式
cnt = ; //cnt重新初始化
i --; //一定要回退一步,画画就知道了。
continue;
}
if(i == n) break;
if(A[i]-A[i-] == d2)
{
while(A[i]-A[i-] == d2)
{
i ++;
cnt ++;
if(i == n) break;
}
sum += (cnt+)*cnt/;
cnt = ;
i --;
}
}
sum += (cnt+)*cnt/; //这步不能少
printf("%I64d\n", sum+n); //一定要加上这个n,刚开始我加的是5,WA了一发
// printf("%d\n", sum+n);
}
return ;
}
HDU5400 Arithmetic Sequence的更多相关文章
- [hdu5400 Arithmetic Sequence]预处理,容斥
题意:http://acm.hdu.edu.cn/showproblem.php?pid=5400 思路:预处理出每个点向左和向右的最远边界,从左向右枚举中间点,把区间答案加到总答案里面.由与可能与前 ...
- 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, ...
随机推荐
- VMware 使用
1.客户操作系统被禁用: BIOS中开启VT(Virtual Technology)
- BZOJ 1877: [SDOI2009]晨跑 费用流
1877: [SDOI2009]晨跑 Description Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现在给出一 ...
- js的正则表达式
正则表达式(regular expression)是一中描述字符模式的对象,js的RegExp类表示正则表达式,String与RegExp都定义了相应的方法来操作正则表达式,比如模式匹配,文本检索和替 ...
- js截取指定字节长度的字符串
默认的截取字符串都是根据字符长度或位置截取的,典型的两个方法是substr和substring. 这样导致的问题是截取同样长度的字符串时,多字节字符(汉字等)和单字节字符(半角英文字母.半角数字)占的 ...
- http://my.oschina.net/u/719192/blog/506062?p={{page}}
http://my.oschina.net/u/719192/blog/506062?p={{page}}
- Jlink更新新固件USB连接不上的问题
采购新买了一个jlink,在调试过程中发现不能使用.拿到手后对jink上电以后,发现灯不亮,到网上查了一下估计是固件损坏的原因,经过一番摸索从新更新了固件,然后可以正常使用了,下面说一下步骤: 新版的 ...
- 271. Encode and Decode Strings
题目: Design an algorithm to encode a list of strings to a string. The encoded string is then sent ove ...
- 大规模视觉识别挑战赛ILSVRC2015各团队结果和方法 Large Scale Visual Recognition Challenge 2015
Large Scale Visual Recognition Challenge 2015 (ILSVRC2015) Legend: Yellow background = winner in thi ...
- SQL 随笔
自动生成10位ID DECLARE @num INT ) )) ) Date的运算 DECLARE @StartDate DATETIME , GetDate()) --add day PRINT @ ...
- Java实现图片压缩代码,图片大小转换
在很多项目中我们会把上传的图片做处理,比较图片上传过多对服务器的容量和带宽有很多的浪费,如果不是必须的高清图片,我们可以通过代码来做压缩.在我的项目中我们压缩图片的目的是让web页面打开的速度很快,并 ...