Fibonacci-ish

Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if

  1. the sequence consists of at least two elements
  2. f0 and f1 are arbitrary
  3. fn + 2 = fn + 1 + fn for all n ≥ 0.

You are given some sequence of integers a1, a2, ..., an. Your task is rearrange elements of this sequence in such a way that its longest possible prefix is Fibonacci-ish sequence.

Input

The first line of the input contains a single integer n (2 ≤ n ≤ 1000) — the length of the sequence ai.

The second line contains n integers a1, a2, ..., an (|ai| ≤ 109).

Output

Print the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement.

Examples
Input
3
1 2 -1
Output
3
Input
5
28 35 7 14 21
Output
4
Note

In the first sample, if we rearrange elements of the sequence as  - 1, 2, 1, the whole sequence ai would be Fibonacci-ish.

In the second sample, the optimal way to rearrange elements is , , , , 28.

思路:由于斐波那契数列数值成指数型增长(称为斐波那契数列的收敛性),所以实际可选的长度不会超过100(实际上增长速度比2的幂次方要慢,若是正整数的斐波那契数列,要从0 1增长到1e9只需40位左右,所以题解给出的长度最大也是接近90的);那么只需离散出全部的不同的数值及其个数,枚举前两位即可;特例是前两位为0,这时长度可能达到1000,但只会出现一次;

实现细节:用stk记录下路径上的序号,为了方便每次枚举时,初始化每个数的个数;同时也可递推出当前两位的和;

没使用map,使用map的话,插入和查询都是log(n),总时间复杂度为O(n^2*log(n)*len)len为每次搜索的长度,且常数较大;用数组模拟,时间复杂度中少了log(n)与map带来的额外的常数,只需46ms 0k

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
#define MSi(a) memset(a,0x3f,sizeof(a))
#define inf 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m+1, r, rt << 1|1
typedef __int64 ll;
template<typename T>
void read1(T &m)
{
T x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
m = x*f;
}
template<typename T>
void read2(T &a,T &b){read1(a);read1(b);}
template<typename T>
void read3(T &a,T &b,T &c){read1(a);read1(b);read1(c);}
template<typename T>
void out(T a)
{
if(a>) out(a/);
putchar(a%+'');
}
int v[],num[],stk[];//stk长度也要为1000,因为前面两个为0时,len可能为最大值
int main()
{
int n;
read1(n);
rep0(i,,n) read1(v[i]);
sort(v,v + n);
int ans = ,cnt = ;
rep0(i,,n){
int t = i;
while(i < n - && v[i] == v[i + ]) i++;
num[cnt] = i - t + ;
v[cnt++] = v[i];//压缩
}
v[cnt] = 2e9;
rep0(i,,cnt){
rep0(j,,cnt){
if(i != j || num[j] > ){
stk[] = i;stk[] = j;
int len = ,val = v[i] + v[j];
num[i]--,num[j]--;
int down = lower_bound(v,v+cnt,val) - v;
while(v[down] == val && num[down]){
num[down]--;
val -= v[stk[len-]];
val += v[down];
stk[++len] = down;
down = lower_bound(v,v+cnt,val) - v;
}
ans = max(ans,len);
rep1(i,,len) num[stk[i]]++;
}
}
}
out(ans);
return ;
}

codeforces 633D - Fibonacci-ish 离散化 + 二分查询的更多相关文章

  1. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

  2. [Codeforces 1199C]MP3(离散化+二分答案)

    [Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...

  3. C# 二分查询

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. POJ2391 Floyd+离散化+二分+DINIC

    题意:       有n个猪圈,每个猪圈里面都有一定数量的猪(可能大于当前猪圈的数量),每个猪圈都有自己的容量,猪圈与猪圈之间给出了距离,然后突然下雨了,问多久之后所有的猪都能进圈. 思路:     ...

  5. Codeforces Round #521 (Div. 3) E. Thematic Contests (离散化,二分)

    题意:有\(n\)个话题,每次都必须选取不同的话题,且话题数必须是上次的两倍,第一次的话题数可以任意,问最多能选取多少话题数. 题解:我们首先用桶来记录不同话题的数量,因为只要求话题的数量,与话题是多 ...

  6. [Codeforces]817F. MEX Queries 离散化+线段树维护

    [Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You sho ...

  7. codeforces 749D Leaving Auction(二分)

    题目链接:http://codeforces.com/problemset/problem/749/D 题意:就是类似竞拍,然后报价肯定要比上一个高,然后查询输入k个数表示那些人的竞拍无效, 输出最后 ...

  8. 2019 Multi-University Training Contest 3 Find the answer (离散化+二分+树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6609 题目大意:给定一个含有n个数的序列,还有一个m,对于每个i(1<=i<=n)求出最少 ...

  9. Codeforces 55D (数位DP+离散化+数论)

    题目链接: http://poj.org/problem?id=2117 题目大意:统计一个范围内数的个数,要求该数能被各位上的数整除.范围2^64. 解题思路: 一开始SB地开了10维数组记录情况. ...

随机推荐

  1. svn常用操作命令(不断更新中......)

      1.svn info显示本地或远程条目的信息.打印你的工作拷贝和URL的信息包括:路径.名称.URL.版本库的根.版本库的UUID.Revision.节点类型.最后修改作者.最后修改版本最后修改日 ...

  2. Android 开发中的屏幕适配技术详解

    本文主要介绍Android开发中比较头疼繁琐的一个问题-屏幕适配问题.主要从适配原因.基本核心概念.适配方法等方面介详细 介绍从而是的深入或者进一步对Android屏幕适配技术的掌握和理解. 真题园网 ...

  3. Linux批量替换文件内容

    问题描述:现在需要将rack1目录下*.send文件中的"-ip="替换成“-localIp=10.0.0.1/n-ip=” 刚才那个批量文本内容替换,只能替换内存中的内容,并不会 ...

  4. 深入理解计算机系统第二版习题解答CSAPP 2.13

    从20世纪70年代末到80年代末,Digital Equipment的VAX计算机是一种非常流行的机型.它没有布尔运算AND和OR指令,只有bis(位设置)和bic(位清除)这两种指令.两种指令的输入 ...

  5. CentOS 6 下安装Python 3

    可以下载各个版本的python:https://www.python.org/ftp/python/ 配置安装 下载最新的安装包(截止2013/11/05),还是3.3.2版本. #wget http ...

  6. 关于Spring的IOC和DI

    原始调用模型 Spring的演化过程 Spring的调用过程 ======================================= IoC[理解][应用][重点] 1.IoC(Inversi ...

  7. 实现百度地图导航Demo的语音播报功能

    上文中实现了在本地导入百度地图导航Demo,那么在此基础上如何实现导航的语音播报呢? 一.为该应用申请语音播报(也叫注册) http://developer.baidu.com/map/index.p ...

  8. php的标记形式

    共三种: 推荐第一种,第三种需要在php.ini中配置 效果: 第三种配置 将short_open_tag=Off改为On重启Apache就可以了

  9. mysqlimport 导入文件到数据库命令

    mysqlimport -h 172.16.145.125 -u ocetl -pocetl test  --fields-terminated-by='|' '/home/ocetl/tmp_use ...

  10. Windows下 Scala开发环境搭建

    1.配置jdk:可看这里 2.下载scala,并安装 3.配置scala环境变量,把scala的安装路径加入path内 ps:验证是否安装正确:cmd->输入scala,如果出现scala环境, ...