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, ...
随机推荐
- java集合之ArrayList的实现原理
1. ArrayList概述: ArrayList是List接口的可变数组的实现.实现了所有可选列表操作,并允许包括 null 在内的所有元素.除了实现 List 接口外,此类还提供一些方法来操作内部 ...
- 从Config文件中读取节点的配置信息
下面是web.config中与本内容有关的细节 <appSettings> <add key="servername" value="www" ...
- Map中如何把没有定义操作符<的类作为key
Map中如何把没有定义操作符<的类作为key 其实,为了实现快速查找,map内部本身就是按序存储的(比如红黑树).在我们插入<key, value>键值对时,就会按照key的大小顺序 ...
- Minifilter微过滤框架:框架介绍以及驱动层和应用层的通讯
minifilter是sfilter后微软推出的过滤驱动框架.相比于sfilter,他更容易使用,需要程序员做的编码更简洁. 系统为minifilter专门制作了一个过滤管理器,这个管理器本身其实是一 ...
- QT+VS编译器处理字符串时的坑真是多
以下因素都有影响:1. QT4与QT5对字符串编码的处理不同(最好不要在源代码里直接写中文,坑更多)2. QTextCodec自动对编码转换的影响3. 源代码文件对编码存储格式的不同会影响编译器对字符 ...
- 屏幕实战效果解析:IPS/TFT/AMOLED/SLCD
现在手机市场上,智能手机种类繁多,手机屏幕材质也是五花八门.对于一般消费者来说,一款手机是否值得购买,除了关心它的硬件参数以外,更重要的一点就是看它的屏幕.除了屏幕尺寸以外,影响着大家对该手机的第一感 ...
- 使用 Dalvik 调试监控服务 (DDMS) 工具
Android 附带一个叫Dalvik 调试监控服务 (DDMS) 的调试工具,它提供端口转发服务.在设备上的屏幕捕获,设备上的线程和堆栈信息, logcat,进程, 和无线状态信息,接收呼叫和SMS ...
- Java:装饰设计模式
装饰设计模式: 当想要对已有的对象进行功能增强时,可以定义类,将已有对象传入,基于已有的功能, 并提供加强功能,那么自定义的该类就称为装饰类. 装饰类通常通过构造方法接收被装饰的对象,并基于被装饰的对 ...
- Android java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.minor ver
java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.minor ver 解决 ...
- z-index无效问题的解决方法
在使用z-index这个属性之前,我们必须先了解使用z-index的必要条件: 1.要想给元素设置z-index样式,必须先让它变成定位元素,说的明白一点,就是要给元素设置一个postion:rela ...