博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeforces365B
阅读量:4561 次
发布时间:2019-06-08

本文共 2848 字,大约阅读时间需要 9 分钟。

The Fibonacci Segment

 

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
10 1 2 3 5 8 13 21 34 55 89
Output
10
Input
5 1 1 1 1 1
Output
2 sol:找最长的满足斐波那契数列性质的数列,容易发现只要55个数字就会数字大小就会爆int,但是如果你直接暴力的话100000个0你就T飞了 所以把一串0缩成一个点,在暴力 但是有一堆地方要特判,我跪的很惨(我太菜菜菜菜菜菜菜菜菜菜了)
#include 
using namespace std;typedef int ll;inline ll read(){ ll s=0; bool f=0; char ch=' '; while(!isdigit(ch)) { f|=(ch=='-'); ch=getchar(); } while(isdigit(ch)) { s=(s<<3)+(s<<1)+(ch^48); ch=getchar(); } return (f)?(-s):(s);}#define R(x) x=read()inline void write(ll x){ if(x<0) { putchar('-'); x=-x; } if(x<10) { putchar(x+'0'); return; } write(x/10); putchar((x%10)+'0'); return;}#define W(x) write(x),putchar(' ')#define Wl(x) write(x),putchar('\n')const int N=100005;int n;int a[N],A[N],Len[N];int main(){ int i,j,ans=1; R(n); if(n<=2) {Wl(n); return 0;} for(i=1;i<=n;i++) { R(a[i]); } *A=0; for(i=1;i<=n;i++) { if(a[i]>0) { A[++*A]=a[i]; Len[*A]=1; } else { A[++*A]=0; for(;i<=n&&a[i]==0;i++) Len[*A]++; i--; } } for(i=1;i<=n;i++) ans=max(ans,Len[i]); if(*A==1) ans=n; if(*A==2) { if(A[1]==0) ans=max(Len[1],Len[2]+1); else ans=Len[2]; } for(i=1;i<=(*A)-2;i++) { int tmp; if(A[i]==0) { tmp=Len[i+1]+1; } else if(A[i+1]==0) { if(Len[i+1]==1) tmp=Len[i+1]+1; else { tmp=1+Len[i+2]; for(j=i+3;j<=*A;j++) { if(A[j]==A[j-1]+A[j-2]) tmp+=Len[j]; else break; } ans=max(ans,tmp); continue; } } else tmp=Len[i]+Len[i+1]; for(j=i+2;j<=*A;j++) { if(A[j]==A[j-1]+A[j-2]) tmp+=Len[j]; else break; } ans=max(ans,tmp); } Wl(ans); return 0;}/*input101 2 3 5 8 13 21 34 55 89output10input51 1 1 1 1output2input101 1 0 0 0 0 0 0 0 1output7*/
View Code

 

 

转载于:https://www.cnblogs.com/gaojunonly1/p/10617439.html

你可能感兴趣的文章
函数依赖的公理化系统
查看>>
rabbitmq学习(四):利用rabbitmq实现远程rpc调用
查看>>
侯捷C++学习(二)
查看>>
EasyPlayer RTSP Android安卓播放器修复播放画面卡在第一帧bug
查看>>
web项目中全局常量的添加
查看>>
搬运工程 启动!
查看>>
局部加权回归(LWR) Matlab模板
查看>>
Connect to the DSP on C6A8168/DM8168/DM8148 using CCS
查看>>
hibernate在使用getCurrentSession时提示no session found for current thread
查看>>
【Luogu1471】方差(线段树)
查看>>
DEV中svg图标的使用
查看>>
markdown测试
查看>>
Java-Maven-Runoob:Maven 依赖管理
查看>>
杂项-Log:log4net
查看>>
杂项-Java:EL表达式
查看>>
tarroni music
查看>>
unity 使用RotateAround的使用注意
查看>>
[SDOI2009]HH的项链
查看>>
CodeFirst模式,容易引发数据迁移问题(不建议使用)
查看>>
jquery的colorbox关闭并传递数据到父窗
查看>>