D. Fibonacci-ish

题目连接:

http://www.codeforces.com/contest/633/problem/D

Description

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

the sequence consists of at least two elements

f0 and f1 are arbitrary

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

3

1 2 -1

Sample Output

3

Hint

题意

给你n个数,然后让你随便取一些数出来,使得能够组成的广义fib序列最长

题解:

暴力枚举第一个数和第二个数就好了,然后再暴力往下走,开一个map,记录一下每个数还有多少个

注意回溯。

然后还得注意第一个数和第二个数是0的情况,这样的话,你在暴力枚举的时候,可能复杂度就变成n^3了

所以再开一个map<pair<int,int>,int> 记录一下你枚举了哪些数就好了,这样复杂度就不可能变成n^3了。

当然,你预先处理出第一个数是0,第二个数是0的也可以

其他的fib长度感觉不会超过100吧?

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3+5;
int n;
map<int,int> H;
map<pair<int,int>,int>H2;
int a[maxn];
int deal(int x,int y)
{
if(H[x+y]==0)return 0;
H[x+y]--;
int ans = deal(y,x+y);
H[x+y]++;
return ans+1;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]),H[a[i]]++;
sort(a,a+n);
int ans = 0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)continue;
if(H2[make_pair(a[i],a[j])])continue;
H2[make_pair(a[i],a[j])]=1;
H[a[i]]--;
H[a[j]]--;
ans=max(ans,deal(a[i],a[j]));
H[a[i]]++;
H[a[j]]++;
}
}
cout<<ans+2<<endl;
}

Manthan, Codefest 16 D. Fibonacci-ish 暴力的更多相关文章

  1. Manthan, Codefest 16 D. Fibonacci-ish(暴力)

    题目链接:点击打开链接 题意:给你n个数, 问最长的题目中定义的斐波那契数列.  思路:枚举開始的两个数, 由于最多找90次, 所以能够直接暴力, 用map去重.  注意, 该题卡的时间有点厉害啊. ...

  2. Manthan, Codefest 16 H. Fibonacci-ish II 大力出奇迹 莫队 线段树 矩阵

    H. Fibonacci-ish II 题目连接: http://codeforces.com/contest/633/problem/H Description Yash is finally ti ...

  3. Manthan, Codefest 16 D. Fibonacci-ish

    D. Fibonacci-ish time limit per test 3 seconds memory limit per test 512 megabytes input standard in ...

  4. Manthan, Codefest 16 -A Ebony and Ivory

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  5. Manthan, Codefest 16

    暴力 A - Ebony and Ivory import java.util.*; import java.io.*; public class Main { public static void ...

  6. Manthan, Codefest 16 A. Ebony and Ivory 水题

    A. Ebony and Ivory 题目连接: http://www.codeforces.com/contest/633/problem/A Description Dante is engage ...

  7. Manthan, Codefest 16(B--A Trivial Problem)

    B. A Trivial Problem time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. Manthan, Codefest 16 -C. Spy Syndrome 2

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  9. CF Manthan, Codefest 16 G. Yash And Trees 线段树+bitset

    题目链接:http://codeforces.com/problemset/problem/633/G 大意是一棵树两种操作,第一种是某一节点子树所有值+v,第二种问子树中节点模m出现了多少种m以内的 ...

随机推荐

  1. 获取并编译最新的Notepad++源码

    获取并编译最新的Notepad++源码 http://blog.csdn.net/u012814856/article/details/68947310 Notepad++源码编译及其分析 http: ...

  2. 4.FireDAC组件快照 二

    TFDUpdateSQL 生成添加,删除,修改SQL语句 TFDMetaInfoQuery 查询数据源信息 TFDEventAlerter 负责处理数据库事件通知 使用TFDEventAlerter类 ...

  3. 【LabVIEW技巧】策略模式

    前言 在之前的文章提到了如何学习OOP以及对应的简单工厂模式,由于时间比较长,我们先回顾一下原有内容,然后继续了解新的模式. 为什么学习OOP 在测控系统的软件开发过程中,LabVIEW工程师一直认为 ...

  4. 根据节点解析xml

    config.xml文件如下: <?xml version="1.0" encoding="gb2312" ?> <root> < ...

  5. RabbitMQ 实践及使用

    目录 - 1. RabbitMQ的安装    - 1.1 配置好 epel    - 1.2 安装 RPM包    - 1.3 创建用户设置权限- 2. RabbitMQ组件- 3. RabbitMQ ...

  6. 其实linux下远程windows并不麻烦

    1:如果你是安装的ubuntu.那么安装完成之后就自带一个Remmina的远程桌面工具 2:这里我们需要安装rdesktop和tsclient,其中rdesktop是基于命令行的工具,tsclient ...

  7. P2511 [HAOI2008]木棍分割

    目录 Description Solution Code Description 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, ...

  8. HDU-5317

    RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  9. 常用python shell

    路径及文件操作 创建目录 os.mkdir(path_str) 列出当前文件夹中文件,存入string list中 os.listdir(path_str) 判断路径是否存在 os.path.exis ...

  10. memcached安装+绑定访问ip

    安装: 1.由于memcached是基于libevent的,需要安装libevent,libevent-devel $yum -y install libevent libevent-devel 2. ...