Arithmetic Sequence

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1445    Accepted Submission(s): 632

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
 
Author
xudyh
 
对每个数先预处理出左右能够达到的最远距离。然后当d1!=d2时,用乘法原理得到区间数(左边有l[i]个区间,右边有r[i]个区间,左右就是l[i]*r[i]个区间)
当d1==d2 时,左右会算重,只算左边就好了。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <queue>
  5. #include <algorithm>
  6. using namespace std;
  7. typedef long long LL;
  8. const int N = ;
  9. int a[N];
  10. LL l[N],r[N],ans; ///l[i]记录第i个数左边与其成方差为d1的数的个数,r[i]记录第i个数右边与其方差为
  11. ///d2的数的个数(包括自身)
  12. int main()
  13. {
  14. int n,d1,d2;
  15. while(scanf("%d%d%d",&n,&d1,&d2)!=EOF){
  16. for(int i=;i<=n;i++){
  17. scanf("%d",&a[i]);
  18. }
  19. l[] = ,r[n]=;
  20. ans = ;
  21. for(int i=;i<=n;i++){ ///预处理
  22. if(a[i]==a[i-]+d1){
  23. l[i] = l[i-]+;
  24. }else l[i] = ;
  25. }
  26. for(int i=n-;i>=;i--){
  27. if(a[i]+d2==a[i+]){
  28. r[i] = r[i+]+;
  29. }else r[i]=;
  30. }
  31. if(d1==d2){
  32. for(int i=;i<=n;i++){
  33. ans+=l[i];
  34. }
  35. }
  36. else
  37. for(int i=;i<=n;i++){ ///乘法原理
  38. // printf("%lld %lld\n",l[i],r[i]);
  39. ans+=l[i]*r[i];
  40. }
  41. printf("%lld\n",ans);
  42. }
  43. }

hdu 5400(思路题)的更多相关文章

  1. hdu 4908(思路题)

    BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. Proud Merchants HDU - 3466 (思路题--有排序的01背包)

    Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerfu ...

  3. hdu 5191(思路题)

    Building Blocks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  4. hdu 5101(思路题)

    Select Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  5. hdu 5063(思路题-反向操作数组)

    Operation the Sequence Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  6. hdu 4859(思路题)

    Goffi and Squary Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  7. hdu 4956(思路题)

    Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. HDU 1173 思路题

    题目大意 有n个地点(坐标为实数)需要挖矿,让选择一个地点,使得在这个地方建造基地,到n个地点的距离和最短,输出基地的坐标. 题解+代码: 1 /* 2 把这个二维分开看(即把所有点投影到x轴上,再把 ...

  9. 51nod P1305 Pairwise Sum and Divide ——思路题

    久しぶり! 发现的一道有意思的题,想了半天都没有找到规律,结果竟然是思路题..(在大佬题解的帮助下) 原题戳>>https://www.51nod.com/onlineJudge/ques ...

随机推荐

  1. NodeJS基础入门-fs文件系统

    文件I/O是由简单封装的标准POSIX函数提供.通过require('fs') 使用该模块.所有的方法都有异步和同步的形式. 异步方法的最后一个参数都是一个回调函数.传给回调函数的参数取决于具体方法, ...

  2. java实现验证码功能

    java实现验证码功能 通过java代码实现验证码功能的一般思路: 一.通过java代码生成一张验证码的图片,将验证码的图片保存到项目中的指定文件中去,代码如下: package com.util; ...

  3. nodejs开发过程中遇到的一些插件记录

    1.chalk Github:https://github.com/chalk/chalk   终端样式定制插件,可自定义输出日志的样式. 1.semver   管网:https://semver.o ...

  4. day3- python 注册

    # .先把文件内容的账号密码放到list/字典 f = open('users') result = f.read() f.close() user_list = result.split() # u ...

  5. vim小操作

    初时,先有ed,ed为ex之父,ex为vi之父,而vi为vim之父 c 修改 d 删除 y 复制到寄存器 g~ 反转大小写 gu 反转为小写 gU 反转为大写 > 增加缩进 < 减小缩进 ...

  6. 解决linux不能解压rar格式压缩包

    1download rarlinux-x64-5.3.0.tar.gz data package 2.tar xvf rarlinux-64-5.3.0.tar.gz 3. cd rar and th ...

  7. 第十四届华中科技大学程序设计竞赛决赛同步赛 Beautiful Land

    It’s universally acknowledged that there’re innumerable trees in the campus of HUST.Now HUST got a b ...

  8. poj 1742 多重背包问题 dp算法

    题意:硬币分别有 A1.....An种,每种各有C1......Cn个,问组成小于m的有多少种 思路:多重背包问题 dp[i][j]表示用前i种硬币组成j最多剩下多少个  dp=-1的表示凑不齐 dp ...

  9. Selenium WebDriver-通过断言页面是否存在某些关键字来确定页面按照预期加载

    #encoding=utf-8 import unittest import time import chardet from selenium import webdriver class Visi ...

  10. selenium - 常用等待操作

    # 4. 等待操作 # 强制等待from time import sleepsleep(10) # 隐性等待# 设置最长等待时间,在这个时间在只要有个时间点加载完成,则执行下一步代码,比sleep智能 ...