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 1665
#include<iostream>//chengdacaizi 08.11.12 #include<iomanip> #define p 3.1415927 using na ...
- 虚拟专用网络VPN
寒假回到家里需要下载论文,怎样才能访问学校图书馆的数据库呢?解决方法是学校图书馆在内网中架设一台VPN服务器,VPN服务器有两块网卡,一块连接内网,一块连接公网.然后就可以通过互联网找到VPN服务器, ...
- DevExpress TreeList 那些事儿
1:TreeList绑定数据源 当我们给予TreeList 的 parentFieldName 和 KeyFieldName 两个属性之后 会自动的生成树结构. 1 var sql = @" ...
- ExtJs之文本框及数字输入
结合HTML来理解, 比较容易. <!DOCTYPE html> <html> <head> <title>ExtJs</title> &l ...
- 窗口截图(可指定HWND窗口句柄)(三篇文章)
BOOL SaveHwndToBmpFile(HWND hWnd, LPCTSTR lpszPath) { HWND hDesktop = ::GetDesktopWindow(); ASSERT(h ...
- IOS 的loadView 及使用loadView中初始化View注意的问题。(死循环并不可怕)
在XCode 4.2后,我基本上的应用都不使用Xib文件了,虽然xib文件有很多好趣,可以快速免代码构建视窗,可以减少好多代码构建带来的麻烦,其实能用xib还是不错的,主要是我的机器打开xib来编辑时 ...
- USACO Section 2.1: Hamming Codes
挺简单的一道题 /* ID: yingzho1 LANG: C++ TASK: hamming */ #include <iostream> #include <fstream> ...
- android ImageView的属性android:scaleType,即ImageView.setScaleType(ImageView.ScaleType)
实例 <ImageView android:id="@+id/image" android:layout_width="fill_parent" andr ...
- Android判断网络是否已经连接
// check all network connect, WIFI or mobile public static boolean isNetworkAvailable(final Context ...
- [原]poj-2488-water-DFS
题目大意: 输入一个p*q的棋盘, 行用数字表示, 列用大写字母表示 , 1 <= p*q <= 26, 输出能够把棋盘全部空格走完且每个空格只走一次的字典序最小的路径.不存在则输出“im ...