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 ...
随机推荐
- [055] SSL 3.0曝出Poodle漏洞的解决方式-----开发人员篇
SSL 3.0曝出高危漏洞 2014年10月15日,Google研究人员发布SSL 3.0协议存在一个非常严重的漏洞,该漏洞可被黑客用于截取浏览器与server之间进行传输的加密数据,如网银账号.邮箱 ...
- .NET使用NPOI组件将数据导出Excel
.NPOI官方网站:http://npoi.codeplex.com/ 可以到此网站上去下载最新的NPOI组件版本 2.NPOI在线学习教程(中文版): http://www.cnblogs.com/ ...
- MVC3.0部署问题小结
环境:MVC3.0,IIS7 Mvc3.0的部署除像正常部署aspx程序一样外,另外还需要注意的几点:1. 安装MVC3.0 确保服务器上安装了MVC3.0,默认版本是“3.0.20105.0” 2. ...
- Oracle行转列实例
行转列之前的数据: select * from tbname; 行转列之后的数据: select pud, listagg(ud, ',') within group(order by null) a ...
- Java学习之位运算和逻辑运算符
今天看了一下HashMap类的源码,在HashMap的源码中定义了初始空间的大小 static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; 当 ...
- java结构与算法之选择排序
一 .java结构与算法之选择排序(冒择路兮快归堆) 什么事选择排序:从一组无序数据中选择出中小的的值,将该值与无序区的最左边的的值进行交换. 简单的解释:假设有这样一组数据 12,4,23,5,找到 ...
- python中的字符串和数字连接
1. 将数字强制转换成字符串 i = 1000 str1 = "hello" print str1 + str(i) 2. 格式化成字符串 i = 1000 str1 = &quo ...
- Python网络编程——设定并获取默认的套接字超时时间
Sometimes,you need to manipulate the default values of certain properties of a socket library, for e ...
- 使用python操作RabbitMQ,Redis,Memcache,SQLAlchemy 其一
一.概念 1.Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态 ...
- c 占位符
%d, %i,代表整数,%f-浮点,%s,字符串,%c,char. %p 指针,%fL 长log,%e科学计数,%g 小数或科学计数. C语言中的格式占位符: %a,%A 读入一个浮点值(仅C9 ...