The Fibonacci Segment

CodeForces - 365B

You have array a1, a2, ..., an. Segment [l, r] (1 ≤ l ≤ r ≤ n) is good if ai = ai - 1 + ai - 2, for all i (l + 2 ≤ i ≤ r).

Let's define len([l, r]) = r - l + 1, len([l, r]) is the length of the segment [l, r]. Segment [l1, r1], is longer than segment [l2, r2], if len([l1, r1]) > len([l2, r2]).

Your task is to find a good segment of the maximum length in array a. Note that a segment of length 1 or 2 is always good.

Input

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains integers: a1, a2, ..., an (0 ≤ ai ≤ 109).

Output

Print the length of the longest good segment in array a.

Examples

Input
  1. 10
    1 2 3 5 8 13 21 34 55 89
Output
  1. 10
Input
  1. 5
    1 1 1 1 1
Output
  1. 2
  2.  
  3. sol:找最长的满足斐波那契数列性质的数列,容易发现只要55个数字就会数字大小就会爆int,但是如果你直接暴力的话1000000你就T飞了
    所以把一串0缩成一个点,在暴力
    但是有一堆地方要特判,我跪的很惨(我太菜菜菜菜菜菜菜菜菜菜了)
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef int ll;
  4. inline ll read()
  5. {
  6. ll s=;
  7. bool f=;
  8. char ch=' ';
  9. while(!isdigit(ch))
  10. {
  11. f|=(ch=='-'); ch=getchar();
  12. }
  13. while(isdigit(ch))
  14. {
  15. s=(s<<)+(s<<)+(ch^); ch=getchar();
  16. }
  17. return (f)?(-s):(s);
  18. }
  19. #define R(x) x=read()
  20. inline void write(ll x)
  21. {
  22. if(x<)
  23. {
  24. putchar('-'); x=-x;
  25. }
  26. if(x<)
  27. {
  28. putchar(x+''); return;
  29. }
  30. write(x/);
  31. putchar((x%)+'');
  32. return;
  33. }
  34. #define W(x) write(x),putchar(' ')
  35. #define Wl(x) write(x),putchar('\n')
  36. const int N=;
  37. int n;
  38. int a[N],A[N],Len[N];
  39. int main()
  40. {
  41. int i,j,ans=;
  42. R(n);
  43. if(n<=) {Wl(n); return ;}
  44. for(i=;i<=n;i++)
  45. {
  46. R(a[i]);
  47. }
  48. *A=;
  49. for(i=;i<=n;i++)
  50. {
  51. if(a[i]>)
  52. {
  53. A[++*A]=a[i];
  54. Len[*A]=;
  55. }
  56. else
  57. {
  58. A[++*A]=;
  59. for(;i<=n&&a[i]==;i++) Len[*A]++;
  60. i--;
  61. }
  62. }
  63. for(i=;i<=n;i++) ans=max(ans,Len[i]);
  64. if(*A==) ans=n;
  65. if(*A==)
  66. {
  67. if(A[]==) ans=max(Len[],Len[]+);
  68. else ans=Len[];
  69. }
  70. for(i=;i<=(*A)-;i++)
  71. {
  72. int tmp;
  73. if(A[i]==)
  74. {
  75. tmp=Len[i+]+;
  76. }
  77. else if(A[i+]==)
  78. {
  79. if(Len[i+]==) tmp=Len[i+]+;
  80. else
  81. {
  82. tmp=+Len[i+];
  83. for(j=i+;j<=*A;j++)
  84. {
  85. if(A[j]==A[j-]+A[j-]) tmp+=Len[j];
  86. else break;
  87. }
  88. ans=max(ans,tmp);
  89. continue;
  90. }
  91. }
  92. else tmp=Len[i]+Len[i+];
  93. for(j=i+;j<=*A;j++)
  94. {
  95. if(A[j]==A[j-]+A[j-]) tmp+=Len[j];
  96. else break;
  97. }
  98. ans=max(ans,tmp);
  99. }
  100. Wl(ans);
  101. return ;
  102. }
  103. /*
  104. input
  105. 10
  106. 1 2 3 5 8 13 21 34 55 89
  107. output
  108. 10
  109.  
  110. input
  111. 5
  112. 1 1 1 1 1
  113. output
  114. 2
  115.  
  116. input
  117. 10
  118. 1 1 0 0 0 0 0 0 0 1
  119. output
  120. 7
  121. */
  1.  

codeforces365B的更多相关文章

随机推荐

  1. 使用HttpClient和Jsoup实现一个简单爬虫

    一直很想了解一下爬虫这个东西的,完全是出于兴趣,其实刚开始是准备用python的,但是由于种种原因选择了java,此处省略很多字... 总之,如果你想做一件事情的话就尽快去做吧,千万不要把战线拉得太长 ...

  2. linux中断源码分析 - 初始化(二)

    本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 本篇文章主要讲述源码中是如何对中断进行一系列的初始化的. 回顾 在上一篇概述中,介绍了几个对于中断来说非常重要的 ...

  3. u-boot全面分析

    uboot主Makefile分析1 uboot住Makefile分析参考:https://www.2cto.com/kf/201607/522424.html uboot version确定(Make ...

  4. ubuntu12.04安装mininet

    网上安装mininet教程有很多,都是通过git命令安装,但有一个坑,安装到./install.sh时会报错,记录下来 1.通过git 下载mininet git clone git://github ...

  5. Qt实现软件自动更新的一种简单方法

    前言 最近在学习Qt开发上位机,想实现一个检查更新的功能,网上搜索了一大圈,发现实现过程都很复杂,关键是代码看不懂,所以就自己开发一种简单的方式来实现.实现效果如下: 点击"检查更新&quo ...

  6. maven docker 插件集成的几个小坑

    昨晚看springboot视频的时候,发现可以使用docker-maven-plugin这个插件直接build出 docker 镜像到远程服务器上,感觉很方便,于是自己也试了一下,但是碰到了几个问题, ...

  7. .net core实践系列之短信服务-为什么选择.net core(开篇)

    前言 从今天我将会写.net core实战系列,以我最近完成的短信服务作为例子.该系列将会尽量以最短的时间全部发布出来.源码也将优先开源出来给大家. 源码地址:https://github.com/S ...

  8. 为小米(红米)6A解锁_ROOT_安装天下游虚拟定位教程_已亲身验证通过!附图

    第一步骤: 把TeamViewer发给客户 第二步骤: 在自己电脑上购买小米账号:http://shop1.91kami.com/UXWHTEY9KN?cid=1200 要求:(购买邮箱注册账号,需要 ...

  9. C. Good Array

    链接 [https://codeforces.com/contest/1077/problem/C] 题意 给你一个数组,问你能去掉某个数使得剩下的数中某个数是其他数的和 输出个数以及他们的下标 分析 ...

  10. 第三次Sprint-最后冲刺

    由于一些原因,导致我和汝婷被退队了.因此我们是从上星期重新开始做系统. 陈汝婷单独负责: 1.用户输入题目数: 2.限制题数: 3.自动生成用户需要题目数的题目: 4.计时 练丽云单独: 1.异常处理 ...