hdu 5400 Arithmetic Sequence(重构数组)
来源:互联网 发布:dns的协议端口号 编辑:程序博客网 时间:2024/05/29 10:55
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 sequencea1,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.
Teacher Mai has a sequence
Input
There are multiple test cases.
For each test case, the first line contains three numbersn,d1,d2(1≤n≤105,|d1|,|d2|≤1000) , the next line contains n integers a1,a2,⋯,an(|ai|≤109) .
For each test case, the first line contains three numbers
Output
For each test case, print the answer.
Sample Input
5 2 -20 2 0 -2 05 2 32 3 3 3 3
Sample Output
125
题意:求一个区间,使得区间内存在一点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 longint a[N];LL l[N],r[N];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;}
0 0
- hdu 5400 Arithmetic Sequence(重构数组)
- HDU 5400 Arithmetic Sequence
- hdu 5400 Arithmetic Sequence
- HDU 5400 Arithmetic Sequence
- HDU 5400 Arithmetic Sequence
- HDU 5400 Arithmetic Sequence
- hdu 5400 Arithmetic Sequence
- hdu 5400 Arithmetic Sequence
- hdu 5400 Arithmetic Sequence
- HDU-5400 Arithmetic Sequence(数学 || DP)
- hdu 5400 Arithmetic Sequence(计数)
- hdu 4908 BestCoder Sequence(重构数组)
- hdu 5400 Arithmetic Sequence(水题一道)
- hdu(5400)——Arithmetic Sequence(想法题)
- HDU 5400(Arithmetic Sequence-暴力找区间)
- HDU 5400 Arithmetic Sequence(数学,容斥)
- Hdu 5400 Arithmetic Sequence 数学推理
- 【HDOJ 5400】Arithmetic Sequence
- 【HDOJ】【百度之星】5700 区间交
- 4002
- 了解使用Android ConstraintLayout
- 扑克牌游戏
- JavaMail
- hdu 5400 Arithmetic Sequence(重构数组)
- c++中的const
- SQL 存储过程回滚
- Problem G: C语言习题 n个数逆序
- 树莓派的关机命令
- [Nutch]Nutch+Eclipse+Tomcat+Solr+Cygwin搭建Windows开发环境
- 二叉树常用操作
- 正则表达式 - 元字符
- HBuilder webApp开发(七)微信/QQ/新浪/腾讯微博分享