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, ...
随机推荐
- POJ 2000
#include<iostream> #include<cstdio> #define MAXN 10009 using namespace std; ]; int main( ...
- (转)白话经典算法系列之八 MoreWindows白话经典算法之七大排序总结篇
在我的博客对冒泡排序,直接插入排序,直接选择排序,希尔排序,归并排序,快速排序和堆排序这七种常用的排序方法进行了详细的讲解,并做成了电子书以供大家下载.下载地址为:http://download.cs ...
- VISO下载地址
http://pan.baidu.com/share/home?uk=4011207371#category/type=0
- 微软VSS的超级BUG
发现问题:今天一个新同事,无意中发现他直接就登录进VSS了,并且还是“admin”用户: 解决问题:于是开始在网上找度娘和谷歌帮忙,真是不查不要紧,一查吓一跳,VSS本身就存在这个bug,并且是一个超 ...
- web服务器【apache/nginx] 关闭目录的浏览权限
web服务器[apache/nginx] 关闭目录的浏览权限 我的配置(将Options 中的Indexes干掉): <VirtualHost *:80> ServerAdmin webm ...
- 自绘CProgressCtrl进度条控件,支持自定义显示文本和进程百分比信息
// CXProgressCtrl 头文件 #pragma once // CXProgressCtrl class CXProgressCtrl : public CProgressCtrl { D ...
- WordPress主题制作教程6:常用模版标签
在wordpress中,模板标签指一些字段,比如标题,内容,作者,发布日期,评论数等等,获取静态值和循环里面经常使用. 输出模板标签一般有两种方式:the_yourtag() 输出标签值和get_th ...
- iOS:核心动画之转场动画CATransition
转场动画——CATransition CATransition是CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果.iOS比Mac OS X的转场动画效果少一点 U ...
- RHEL7-使用Apache服务部署静态网站
1. 安装Apache服务程序 1.1 在虚拟机中选中光盘镜像,并设置连接 1.2 将光盘设备挂载到/media/cdrom目录 [root@localhost ~]# mkdir -p /media ...
- Data Flow ->> OLE Command
这里有篇博客文章是讲这个OLE Command的:http://www.cnblogs.com/tylerdonet/archive/2011/06/20/2085490.html OLE Comma ...