链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5400

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

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
 
 
 
 
代码:
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<math.h>
  5.  
  6. #define N 100005
  7. #define LL long long
  8.  
  9. int a[N], dp[N];
  10.  
  11. int main()
  12. {
  13. int n, d1, d2;
  14.  
  15. while(scanf("%d%d%d", &n, &d1, &d2)!=EOF)
  16. {
  17. int i;
  18.  
  19. for(i=; i<=n; i++)
  20. scanf("%d", &a[i]);
  21.  
  22. memset(dp, , sizeof(dp));
  23.  
  24. for(i=; i<n; i++)
  25. {
  26. if(a[i+]==a[i]+d1)
  27. dp[i+] = ;
  28. else if(a[i+]==a[i]+d2)
  29. dp[i+] = ;
  30. else dp[i+] = ;
  31. }
  32.  
  33. LL ans=, tmp=;
  34.  
  35. for(i=; i<=n; i++)
  36. {
  37. if(dp[i]==)
  38. {
  39. if(dp[i-]==) tmp = ;
  40. else tmp++;
  41.  
  42. ans = ans + tmp +;
  43. }
  44. else if(dp[i]==)
  45. {
  46. tmp++;
  47. ans = ans + tmp + ;
  48. }
  49. else
  50. {
  51. ans ++;
  52. tmp = ;
  53. }
  54. }
  55.  
  56. printf("%lld\n", ans);
  57.  
  58. }
  59. return ;
  60. }
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4.  
  5. #define N 110000
  6.  
  7. int a[N];
  8.  
  9. int main()
  10. {
  11. int n, i, x, y, d1, d2;
  12.  
  13. while(scanf("%d%d%d", &n, &d1, &d2)!=EOF)
  14. {
  15. __int64 s1, s2, sum;
  16.  
  17. s1 = s2 = sum = ;
  18. memset(a, , sizeof(a));
  19.  
  20. scanf("%d", &x);
  21.  
  22. for(i=; i<n; i++)
  23. {
  24. scanf("%d", &y);
  25. a[i] = y-x;
  26. x = y;
  27. }
  28.  
  29. for(i=; i<n; i++)
  30. {
  31. if(a[i]==d1)
  32. {
  33. if(a[i-]!=d1) s1 = ;
  34.  
  35. s1++;
  36. sum += s1; ///sum加上当前公差为的d1序列长度
  37. s2 = ; ///s2进行清零
  38. }
  39. else if(a[i]==d2)
  40. {
  41. s2++;
  42. sum += s1 + s2; ///加上公差为d2和前半段为d1后半段为d2的序列长度
  43. }
  44. else
  45. s1 = s2 = ;
  46. }
  47.  
  48. printf("%I64d\n", sum+n); ///n是只有一个元素的时候
  49. }
  50.  
  51. return ;
  52. }

(模拟)Arithmetic Sequence -- HDU -- 5400的更多相关文章

  1. hdu 5400 Arithmetic Sequence(模拟)

    Problem Description A sequence b1,b2,⋯,bn are called (d1,d2)-arithmetic sequence ≤i≤n) such that ≤j& ...

  2. hdu 5400 Arithmetic Sequence

    http://acm.hdu.edu.cn/showproblem.php?pid=5400 Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Ot ...

  3. Arithmetic Sequence(dp)

    Arithmetic Sequence Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 51  Solved: 19[Submit][Status][We ...

  4. [Swift]LeetCode1027. 最长等差数列 | Longest Arithmetic Sequence

    Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall t ...

  5. HZAU 21——Arithmetic Sequence——————【暴力 or dp】

    Arithmetic Sequence Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1810  Solved: 311[Submit][Status] ...

  6. hdu 5400(思路题)

    Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  7. 华中农业大学第四届程序设计大赛网络同步赛-1020: Arithmetic Sequence,题挺好的,考思路;

    1020: Arithmetic Sequence Time Limit: 1 Sec  Memory Limit: 128 MB Submit:  ->打开链接<- Descriptio ...

  8. LeetCode 1027. Longest Arithmetic Sequence

    原题链接在这里:https://leetcode.com/problems/longest-arithmetic-sequence/ 题目: Given an array A of integers, ...

  9. 【leetcode】1027. Longest Arithmetic Sequence

    题目如下: Given an array A of integers, return the length of the longest arithmetic subsequence in A. Re ...

随机推荐

  1. Windows应用程序的VC链接器设置

    Windows应用程序的VC链接器设置 /*转载请注明出自 听风独奏 www.GbcDbj.com */ Windows应用程序分为GUI(Graphical User Interface)和CUI( ...

  2. Train-Alypay-Cloud:蚂蚁大数据平台培训开课通知(第三次)- 培训笔记3(机器学习平台)

    ylbtech-Train-Alypay-Cloud:蚂蚁大数据平台培训开课通知(第三次)- 培训笔记3(机器学习平台) 机器学习平台 一站式可视化机器学习 https://pai.cloud.ali ...

  3. Java利用ScriptEngineManager对计算公式的支持

    1.ScriptEngineManager是JDK6提出的相关方法,这方式的主要目的就是用来对脚本语言的处理.这里只是简单介绍一下对我们常用的数学公式的应用. 2.ScriptEngineManage ...

  4. logger5步走

    https://www.cnblogs.com/GGGGGGZX/p/9114378.html'''打印日志11/26/2017 10:44:21 PM bug 24 并写入文件example.log ...

  5. css踩过的坑

    1. 英文内容会撑破容器而中文内容是可以自动换行的 原因: 因为数学规则因素,中文韩文朝鲜文等可以在任何字之后断行的,而英文却不能把一个单词拆开换行,只能撑破容器 解决: 用word-break: b ...

  6. 第七章 : Git 介绍 (下)[Learn Android Studio 汉化教程]

    Learn Android Studio 汉化教程 Let’s reset even further to remove all traces of your work on the deprecat ...

  7. js中的event

    event代表事件的状态,例如触发event对象的元素.鼠标的位置及状态.按下的键等等.event对象只在事件发生的过程中才有效.event的某些属性只对特定的事件有意义.比如,fromElement ...

  8. 496. Next Greater Element I + 503. Next Greater Element II + 556. Next Greater Element III

    ▶ 给定一个数组与它的一个子列,对于数组中的一个元素,定义它右边第一个比他大的元素称为他的后继,求所给子列的后继构成的数组 ▶ 第 496 题,规定数组最后一个元素即数组最大元素的后继均为 -1 ● ...

  9. JDK常用命令

    转自:https://www.cnblogs.com/saiQsai/p/10353044.html 1.jps 查看java进程,得到进程ID:7854 作用等同于:ps -ef | grep ja ...

  10. Maven配置本地仓库

    当我们在myeclipse中update maven时可能会报错User setting file does not exist C:\Users\lenevo\.m2\setting.xml,以致于 ...