B. The Fibonacci Segment
来源:互联网 发布:2017怎么注册淘宝小号 编辑:程序博客网 时间:2024/06/08 09:00
time limit per test
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
standard outputYou 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.
Sample test(s)
input
101 2 3 5 8 13 21 34 55 89
output
10
input
51 1 1 1 1
output
2
解题说明:此题就是判断一个数列中满足连续两个数之和等于后面一个数的最长串,用暴力遍历即可。
#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>using namespace std;int main(){int i,n,ans,k;int a[100000];scanf("%d",&n);for( i=0;i<n;i++) {scanf("%d",&a[i]);}ans=min(2,n);k=2;for( i=2;i<n;i++){if(a[i]==a[i-1]+a[i-2]) {k++;}else{k=2;}ans=max(ans,k);}printf("%d\n",ans);return 0;}
- B. The Fibonacci Segment
- CF#213DIV2:B The Fibonacci Segment
- codeforces 213B div2 The Fibonacci Segment
- B. Big Segment
- Codeforces-242B-Big Segment
- The Fibonacci Primes
- CodeForces 217B Blackboard Fibonacci
- Code Forces 242 B. Big Segment 贪心
- UVA 10236 The Fibonacci Primes
- UVA 10236 The Fibonacci Primes
- UVA 10236 The Fibonacci Primes
- uva 10236 - The Fibonacci Primes
- Migrate the index segment to another tablespace
- segment
- Segment
- Segment
- Segment
- Segment
- 浏览器加载和渲染html的顺序
- 微信公众帐号开发教程第3篇-开发模式启用及接口配置 .
- 最近做了个博客导出工具
- 关于XSS(跨站脚本攻击)和CSRF(跨站请求伪造)
- Android支持不同屏幕尺寸的手机
- B. The Fibonacci Segment
- 十大滤波算法程序大全(Arduino精编无错版)
- Apache下设置虚拟主机之基于域名的虚拟主机
- 数据库顶级会议——SIGMOD介绍
- opencv学习-粒子滤波 演示与opencv代码
- OpenSSL命令---verify
- Mongo入门
- 学习div容器
- Spring jar说明