CF#213DIV2:B 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.
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).
Print the length of the longest good segment in array a.
10
1 2 3 5 8 13 21 34 55 89
10
5
1 1 1 1 1
2
找出符合a[i] = a[i-1]+a[i-2]的最长长度
没有考虑0的状况。。。。。。蛋疼,长久没做CF了,坑成球了。。。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int a[100005],b[100005],len; int main()
{
int maxn,sum;
int i,n;
while(~scanf("%d",&n))
{
sum = 0;
for(i = 0; i<n; i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
if(sum == 0)
{
printf("%d\n",n);
continue;
}
if(n == 1)
{
printf("1\n");
continue;
}
if(n == 2)
{
printf("2\n");
continue;
}
len = 0;
int l,r,flag = 1;
for(i = 2; i<n; i++)
{
if(a[i-1]+a[i-2] == a[i])
b[len++] = 1;
else
b[len++] = 0;
}
maxn = 0;
l = 0;
r = 0;
for(i = 0; i<len; i++)
{
if(b[i] == 1)
{
if(flag)
{
l = i;
flag = 0;
}
else
r = i;
}
else
{
if(r-l+1>maxn && r!=l)
maxn = r-l+1;
flag = 1;
l = 0;
r = 0;
}
}
if(r-l+1>maxn && r!=l)
maxn = r-l+1;
printf("%d\n",maxn+2);
} return 0;
}
CF#213DIV2:B The Fibonacci Segment的更多相关文章
- cf B. The Fibonacci Segment
http://codeforces.com/contest/365/problem/B #include <cstdio> #include <cstring> #includ ...
- CF 914 G Sum the Fibonacci —— 子集卷积,FWT
题目:http://codeforces.com/contest/914/problem/G 其实就是把各种都用子集卷积和FWT卷起来算即可: 注意乘 Fibonacci 数组的位置: 子集卷积时不能 ...
- 笔试算法题(11):Josephus环 & Fibonacci序列
出题:Josephus Cycle,约瑟夫环问题.k个数字连成一个环,第一个数字为1.首先从1开始计数删除第m个数字:然后从上次被删除的数字的下一个数字开始计数,删除第m个数字:重复进行第二步直到只剩 ...
- 题解报告:poj 3070 Fibonacci
题目链接:http://poj.org/problem?id=3070 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, a ...
- 题解报告:hdu 1848 Fibonacci again and again(尼姆博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1848 Problem Description 任何一个大学生对菲波那契数列(Fibonacci num ...
- codeforces B. The Fibonacci Segment 解题报告
题目链接:http://codeforces.com/problemset/problem/365/B 题目意思:简单来说,就是要找出最长的斐波纳契长度. 解决的方法不难,但是要注意更新左区间和右区间 ...
- HDU1250:Hat's Fibonacci
Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequen ...
- 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)
题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9: 对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可. 后者mod=1e9,5才 ...
- Codeforces Round #213 (Div. 2) B. The Fibonacci Segment
#include <iostream> #include <algorithm> #include <vector> using namespace std; in ...
随机推荐
- [置顶] js操作iframe兼容各种浏览器
在做项目时,遇到了操作iframe的相关问题.业务很简单,其实就是在操作iframe内部某个窗体时,调用父窗体的一个函数.于是就写了两个很简单的htm页面用来测试,使用网上流行的方法在谷歌浏览器中始终 ...
- Linux下启用Chrome/Firefox的Java插件
JDK 已经安装好,可是浏览器执行 Java Applet 时提示需安装 Java 插件. 这时,在浏览器安装文件夹中 plugins 文件夹下创建2个重要的符号链接就可以. libnpjp2.so ...
- 详细解说Android权限在安卓开发中
android.permission.ACCESS_CHECKIN_PROPERTIES //允许读写访问”properties”表在checkin数据库中,改值可以修改上传 android.perm ...
- html5 中的SVG 和canvas
想到昨天看资料的时候,发现html5 中的SVG 和canvas 都可以表示图形,那它们到底有哪些区别呢?该如何正确的使用它们呢? 1.SVG:可缩放矢量图形,(Scalable Vector Gra ...
- 高性能网站优化-创建快速响应的Web
<高性能网站建设进阶指南> 优化原则 优化的目的是希望降低程序的整体开销. 减少开销 通常认为开销就是程序的执行时间.而在进行优化工作时,应该把重点放在对程序开销影响最大的那部分. 假设我 ...
- linkbutton.js
jquery.linkbutton.js /** * linkbutton - jQuery EasyUI * * Licensed under the GPL: * http://www.gnu.o ...
- HTML5 总结-应用程序缓存-8
HTML 5 应用程序缓存 使用 HTML5,通过创建 cache manifest 文件,可以轻松地创建 web 应用的离线版本. 什么是应用程序缓存(Application Cache)? HTM ...
- subplot的几个详细说明
http://blog.sciencenet.cn/blog-251664-800766.html
- [LeetCode]题解(python):019-Remove Nth Node From End of List
题目来源: https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 题意分析: 这道题是给定一个链表,删除倒数第n个节点.提醒, ...
- 高质量程序设计指南C/C++语言——C++/C常量