codeforces365B
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
- 10
1 2 3 5 8 13 21 34 55 89
- 10
- 5
1 1 1 1 1
- 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的更多相关文章
随机推荐
- 使用HttpClient和Jsoup实现一个简单爬虫
一直很想了解一下爬虫这个东西的,完全是出于兴趣,其实刚开始是准备用python的,但是由于种种原因选择了java,此处省略很多字... 总之,如果你想做一件事情的话就尽快去做吧,千万不要把战线拉得太长 ...
- linux中断源码分析 - 初始化(二)
本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 本篇文章主要讲述源码中是如何对中断进行一系列的初始化的. 回顾 在上一篇概述中,介绍了几个对于中断来说非常重要的 ...
- u-boot全面分析
uboot主Makefile分析1 uboot住Makefile分析参考:https://www.2cto.com/kf/201607/522424.html uboot version确定(Make ...
- ubuntu12.04安装mininet
网上安装mininet教程有很多,都是通过git命令安装,但有一个坑,安装到./install.sh时会报错,记录下来 1.通过git 下载mininet git clone git://github ...
- Qt实现软件自动更新的一种简单方法
前言 最近在学习Qt开发上位机,想实现一个检查更新的功能,网上搜索了一大圈,发现实现过程都很复杂,关键是代码看不懂,所以就自己开发一种简单的方式来实现.实现效果如下: 点击"检查更新&quo ...
- maven docker 插件集成的几个小坑
昨晚看springboot视频的时候,发现可以使用docker-maven-plugin这个插件直接build出 docker 镜像到远程服务器上,感觉很方便,于是自己也试了一下,但是碰到了几个问题, ...
- .net core实践系列之短信服务-为什么选择.net core(开篇)
前言 从今天我将会写.net core实战系列,以我最近完成的短信服务作为例子.该系列将会尽量以最短的时间全部发布出来.源码也将优先开源出来给大家. 源码地址:https://github.com/S ...
- 为小米(红米)6A解锁_ROOT_安装天下游虚拟定位教程_已亲身验证通过!附图
第一步骤: 把TeamViewer发给客户 第二步骤: 在自己电脑上购买小米账号:http://shop1.91kami.com/UXWHTEY9KN?cid=1200 要求:(购买邮箱注册账号,需要 ...
- C. Good Array
链接 [https://codeforces.com/contest/1077/problem/C] 题意 给你一个数组,问你能去掉某个数使得剩下的数中某个数是其他数的和 输出个数以及他们的下标 分析 ...
- 第三次Sprint-最后冲刺
由于一些原因,导致我和汝婷被退队了.因此我们是从上星期重新开始做系统. 陈汝婷单独负责: 1.用户输入题目数: 2.限制题数: 3.自动生成用户需要题目数的题目: 4.计时 练丽云单独: 1.异常处理 ...