码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces365B

时间:2019-03-28 20:35:11      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:example   you   isp   info   bool   ons   size   ash   input   

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=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;
}
/*
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
*/
View Code

 

 

codeforces365B

标签:example   you   isp   info   bool   ons   size   ash   input   

原文地址:https://www.cnblogs.com/gaojunonly1/p/10617439.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!