hdu 5400 Arithmetic Sequence(重构数组)
2016-06-18 16:23
387 查看
Arithmetic Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1430 Accepted Submission(s): 625
Problem Description
A sequence b1,b2,⋯,bn are
called (d1,d2)-arithmetic
sequence if and only if there exist i(1≤i≤n) such
that for every j(1≤j<i),bj+1=bj+d1 and
for every j(i≤j<n),bj+1=bj+d2.
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.
Input
There are multiple test cases.
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).
Output
For each test case, print the answer.
Sample Input
5 2 -2
0 2 0 -2 0
5 2 3
2 3 3 3 3
Sample Output
12
5
题意:求一个区间,使得区间内存在一点i,序列[l,i)是公差为d1的等差数列,序列[i,r]是公差为d2的等差数列,问你一共有几个区间满足
思路:枚举i点即可,但是直接枚举会有O(n²)的复杂度会炸掉,所以要用l和r数组分别记录当前点最大能延伸到前面和后面的距离,最后相乘即可,注意两点
1:当d1=d2的时候,区间会算重,此时只需要特判即可,值等于所有l或者所有r
2:l和r数组必须为long long 10^6*10^6会超出int型....因为这个WA了好多次。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 100010
#define LL long long
int a
;
LL l
,r
;
int main()
{
int n,d1,d2;
while(~scanf("%d %d %d",&n,&d1,&d2))
{
long long ans=0;
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
l[i]=r[i]=1;
}
for(int i=2; i<=n; i++)
if(a[i]==a[i-1]+d1)
l[i]=l[i-1]+1;
for(int i=n-1; i>=1; i--)
if(a[i+1]==a[i]+d2)
r[i]=r[i+1]+1;
if(d1==d2)
{
for(int i=1; i<=n; i++)
ans+=l[i];
}
else
{
for(int i=1; i<=n; i++)
ans+=l[i]*r[i];
}
printf("%lld\n",ans);
}
return 0;
}
相关文章推荐
- UI调试工具 Reveal
- SystemUI下拉通知栏的源码分析,屏蔽通知栏,监听通知栏下拉动作
- IOS中div contenteditable=true无法输入
- JAVA之旅(十七)——StringBuffer的概述,存储,删除,获取,修改,反转,将缓存区的数据存储到数组中,StringBuilder
- JAVA之旅(十七)——StringBuffer的概述,存储,删除,获取,修改,反转,将缓存区的数据存储到数组中,StringBuilder
- duilib + cef简单浏览器的demo
- K most frequent words from a file
- Altium Designer 16 (AD16) 破解版下载 百度网盘分享 16.1.9 (Build 221) 更新
- iOS开发UI篇—Modal简单介绍
- if,continue,break,while,do-while, switch,return,foreach,for等条件语句的使用
- 解决缺少apue.h的问题
- Android UiAutomator: 从apk中启动实现方法
- Android UI设计和形成原理(实现三级菜单)
- UITableview 滚动到底部
- String、StringBuffer与StringBuilder之间区别
- suid sgid
- webpack知识,在html里通过webpack使用commonJS规范,可以require各种包
- require(),include(),require_once()和include_once()之间的区别
- 项目总结3 类似网易云音乐导航栏指示器(个性推荐、歌单等)的简单实现(一)
- dz query