Time Limit: 3000MS   Memory Limit: 524288KB   64bit IO Format: %I64d & %I64u

Submit
Status

Description

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.

Sample Input

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

Sample Output

Hint

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.

Source

刚开始想直接暴力搜索的,可是数据的范围有点大,数组记录无法实现,需要用map,用map的时候要去重

#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
map<int,int>fp;
int a[1010];
int f(int a,int b)
{
int ans=0;
if(fp[a+b])
{
fp[a+b]--;
ans=f(b,a+b)+1;
fp[a+b]++;
}
return ans;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
fp.clear();
for(int i=0;i<n;i++)
scanf("%d",&a[i]),fp[a[i]]++;
sort(a,a+n);
int N=unique(a,a+n)-a;
int ans=0;
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
if(i==j&&fp[a[i]]==1) continue;
fp[a[i]]--,fp[a[j]]--;
ans=max(ans,f(a[i],a[j])+2);
fp[a[i]]++,fp[a[j]]++;
}
}
printf("%d\n",ans);
}
return 0;
}

Codeforces--633D--Fibonacci-ish(暴力搜索+去重)(map)的更多相关文章

  1. ACM 暴力搜索题 题目整理

    UVa 129 Krypton Factor 注意输出格式,比较坑爹. 每次要进行处理去掉容易的串,统计困难串的个数. #include<iostream> #include<vec ...

  2. Codeforces Gym 100431B Binary Search 搜索+组合数学+高精度

    原题链接:http://codeforces.com/gym/100431/attachments/download/2421/20092010-winter-petrozavodsk-camp-an ...

  3. HDU 3131 One…Two…Five! (暴力搜索)

    题目链接:pid=3131">HDU 3131 One-Two-Five! (暴力搜索) 题意:给出一串数字,要求用加,减,乘,除(5/2=2)连接(计算无优先级:5+3*6=8*6= ...

  4. POJ 1129:Channel Allocation 四色定理+暴力搜索

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13357   Accepted: 68 ...

  5. 「状压DP」「暴力搜索」排列perm

    「状压DP」「暴力搜索」排列 题目描述: 题目描述 给一个数字串 s 和正整数 d, 统计 sss 有多少种不同的排列能被 d 整除(可以有前导 0).例如 123434 有 90 种排列能被 2 整 ...

  6. hdu 4740 The Donkey of Gui Zhou(暴力搜索)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4740 [题意]: 森林里有一只驴和一只老虎,驴和老虎互相从来都没有见过,各自自己走过的地方不能走第二次 ...

  7. hdu 1427 速算24点 dfs暴力搜索

    速算24点 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem De ...

  8. UVALive 5844 dfs暴力搜索

    题目链接:UVAive 5844 Leet DES:大意是给出两个字符串.第一个字符串里的字符可以由1-k个字符代替.问这两个字符串是不是相等.因为1<=k<=3.而且第一个字符串长度小于 ...

  9. 随手练——洛谷-P1151(枚举与暴力搜索)

    枚举 #include <iostream> using namespace std; int main() { ; cin >> k; ; i < ; i++) { ) ...

随机推荐

  1. css 众妙之门 学习笔记

    伪类: 结构伪类: :empty :only-child :before :after :active :hover :focus :link :visited :first-child :last- ...

  2. python 字典 get方法

    在做项目的过程中,遇到了一个问题,数据保存到字典中,后来发现数据不对,排查了字典的构建过程,是OK的,后来怀疑是别的部分共用了这一个字典,排查代码,发现这里应该是有问题的. score = None ...

  3. JAVA趣味逻辑算法

    /**已知4位同学中的一位数学考了100分,当小李询问这4位是谁考了100分时,4个人的回答如下: A说:不是我. B说:是C C说:是D. D说:他胡说. 已知三个人说的是真话,一个人说的是假话.现 ...

  4. Ubuntu下获取内核源码

    查看当前系统使用的内核版本: apt-cache search linux-source 输出如下: linux-source - Linux kernel source with Ubuntu pa ...

  5. (转) Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)

    http://blog.csdn.net/u010648555/article/details/60767633 当任何时候觉你得难受了,其实你的大脑是在进化,当任何时候你觉得轻松,其实都在使用以前的 ...

  6. VS2015编译ffmpeg的问题解决

    刚装了vs2015,打开一些ffmpeg项目,发现不能编译通过,包括stdio.h都无法找到,可能是vs2015的bug吧. 现在记录一下解决方法: 我的目录是这样定义的: C:\Program Fi ...

  7. [JS]window.location获取url各项参数详解

    window.location方法后还还可以带href,search等参数,下面我们来看看获取url各项参数的办法. URL即:统一资源定位符 (Uniform Resource Locator, U ...

  8. Kail更新源、输入法、浏览器

    更新源 kali官方的更新源:图中的kali-rolling是kali目前最新的代号,kali有两个代号(codename):sana和kali-rolling: 查看自己的kali linux源版本 ...

  9. sql 语句实现可用户名、邮箱、手机号登录系统

    select top 1 nid from Users where (userName collate Chinese_PRC_CS_AS=@userName or mobile collate Ch ...

  10. SGC强制最低128位加密,公钥支持ECC加密算法的SSL证书

      Pro SSL证书,验证企业域名所有权和企业身份信息,采用SGC(服务器门控)技术强制128位以上至256位加密,属于企业OV验证级专业版(Pro) SSL证书:即使用户使用低版本浏览器(比如浏览 ...