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
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 <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n;
int a[N],A[N],Len[N];
int main()
{
int i,j,ans=;
R(n);
if(n<=) {Wl(n); return ;}
for(i=;i<=n;i++)
{
R(a[i]);
}
*A=;
for(i=;i<=n;i++)
{
if(a[i]>)
{
A[++*A]=a[i];
Len[*A]=;
}
else
{
A[++*A]=;
for(;i<=n&&a[i]==;i++) Len[*A]++;
i--;
}
}
for(i=;i<=n;i++) ans=max(ans,Len[i]);
if(*A==) ans=n;
if(*A==)
{
if(A[]==) ans=max(Len[],Len[]+);
else ans=Len[];
}
for(i=;i<=(*A)-;i++)
{
int tmp;
if(A[i]==)
{
tmp=Len[i+]+;
}
else if(A[i+]==)
{
if(Len[i+]==) tmp=Len[i+]+;
else
{
tmp=+Len[i+];
for(j=i+;j<=*A;j++)
{
if(A[j]==A[j-]+A[j-]) tmp+=Len[j];
else break;
}
ans=max(ans,tmp);
continue;
}
}
else tmp=Len[i]+Len[i+];
for(j=i+;j<=*A;j++)
{
if(A[j]==A[j-]+A[j-]) tmp+=Len[j];
else break;
}
ans=max(ans,tmp);
}
Wl(ans);
return ;
}
/*
input
10
1 2 3 5 8 13 21 34 55 89
output
10 input
5
1 1 1 1 1
output
2 input
10
1 1 0 0 0 0 0 0 0 1
output
7
*/
 

codeforces365B的更多相关文章

随机推荐

  1. Mac下安装Mongodb

    Mac下安装Mongodb 一: 安装MongoDB: Mac下安装MongoDB有两种方法,第一种是通过源码安装,另一种是使用 homebrew, 下面我使用homebrew来安装,首先我们需要安装 ...

  2. python基础学习第六天

    import os #文件读写 不用自动关闭with open('test.txt','r') as f: #一次性读取所有 data = f.read() #print(data)print('-- ...

  3. C# 语法二 值类型引用类型

    1.值类型 2.引用类型 一 值类型 值类型存放在栈中,引用类型存放在堆中. 值类型有:数值.布尔.字符,例如:int i;bool i2;char cr='a'; 二 引用类型 大多数类型是引用类型 ...

  4. 关于TCP和MQTT之间的转换

    现在物联网流行的就是MQTT 其实MQTT就是在TCP的基础上建立了一套协议 可以看这个,本来我自己想用Wireshark监听一下,不过百度一搜索一大把,我就不测试了 https://blog.csd ...

  5. C#深入理解AutoResetEvent和ManualResetEvent

    当在C#使用多线程时就免不了使用AutoResetEvent和ManualResetEvent类,可以理解这两个类可以通过设置信号来让线程停下来或让线程重新启动,其实与操作系统里的信号量很相似(汗,考 ...

  6. 使用Pyspark进行特征工程时的那些坑

    以脚本spark_clean_online_action.py.数据集new_sxf_time_count_1781115582.csv为例: 集群节点包括212.216.217.218.需要注意的是 ...

  7. item 10: 比起unscoped enum更偏爱scoped enum

    本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 一般情况下,在花括号中声明一个name(包括变量名,函数名),这个 ...

  8. [开源 .NET 跨平台 Crawler 数据采集 爬虫框架: DotnetSpider] [四] JSON数据解析

    [DotnetSpider 系列目录] 一.初衷与架构设计 二.基本使用 三.配置式爬虫 四.JSON数据解析与配置系统 五.如何做全站采集 场景模拟 接上一篇, JD SKU对应的店铺信息是异步加载 ...

  9. tomcat多实例方案启动脚本

    批量启动 #!/bin/sh BASE_PATH="/usr/local/tomcat8/tomcat-ins/"bash $BASE_PATH/web1/tomcat.sh st ...

  10. 记录:EM 算法估计混合高斯模型参数

    当概率模型依赖于无法观测的隐性变量时,使用普通的极大似然估计法无法估计出概率模型中参数.此时需要利用优化的极大似然估计:EM算法. 在这里我只是想要使用这个EM算法估计混合高斯模型中的参数.由于直观原 ...