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. html5——伸缩布局

    基本概念 1.主轴:Flex容器的主轴主要用来配置Flex项目,默认是水平方向 2.侧轴:与主轴垂直的轴称作侧轴,默认是垂直方向的 3.方向:默认主轴从左向右,侧轴默认从上到下 4.主轴和侧轴并不是固 ...

  2. C# GridView使用方法

    public void Point_databind(PointList pl) { chart1.Series.Clear(); //清空图表 Series s1 = new Series(); / ...

  3. cookie和sessionStorage 、localStorage 对比

    相同点:都存储在客户端 不同点:1.存储大小 cookie数据大小不能超过4k. sessionStorage和localStorage 虽然也有存储大小的限制,但比cookie大得多,可以达到5M或 ...

  4. Requests库 更新中

    1.获取网页内容 --- requests库 <需理解HTTP协议> >requests库的7个主要方法   方法 说明 requests.requests() 构造一个请求,支撑一 ...

  5. 原始js表单文本框初始化获取焦点和选中

    发现: 1 使用dom对象.focus() 放在 window.onload 时可以 让这个DOM对象获取焦点. 2 使用DOM对象.select() 可以选中文本框中的文字. 3 不要加上on  , ...

  6. CentOS7.2下安装php加速软件Xcache

    说明: php安装目录:/usr/local/php php.ini配置文件路径:/usr/local/php/etc/php.ini Nginx安装目录:/usr/local/nginx Nginx ...

  7. 优化yum下载安装慢,不断换mirror

    不停地换mirror,为了解决这个问题,在网上搜了好多资料,总结出一个基于aliyun的mirror源 先检查:是否能正常上网,DNS是否正常,网关gw是否正常,若通过ping 不正常,则解决好网络, ...

  8. 第十节:numpy之数组文件操作

    1.安装ipython 2.创建txt文件 3.直接上代码:

  9. 9.2 Socket编程

    远程管理软件和黑客软件大多依赖于Socket来实现特定功能,前几年流行的端口反弹更是把这项技术发挥到了极致. 如前所述,UDP和TCP是网络体系结构的传输层运行的两大重要协议,其中,TCP适用于对效率 ...

  10. 公钥基本结构(PKI)的概念

    公钥证书 ,通常简称为证书 ,用于在 Internet.Extranet 和 Intranet 上进行身份验证并确保数据交换的安全.证书的颁发者和签署者就是众所周知的 证书颁发机构 (CA),将在下一 ...