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. perl6中的替换

    use v6; =begin pod perl6 中的替换用S/// S有几个可选参数: :g —(长形式::global)全局匹配:替换掉所有的出现 :i —不区分大小写的匹配 :ii —(长形式: ...

  2. perl中的默认变量与Z/map介绍

    use v6; =begin pod @*ARGS 命令行参数, 不含脚本名 $*PROGRAM-NAME:当前运行脚本的相对路径 $*PROGRAM:当前运行脚本的文件名称 $*CWD:当前工作路径 ...

  3. Django【设计】settings方案

      配置文件: 目标:配置文件,默认配置和手动配置分开,参考django的配置文件方案,默认配置文件放在内部,只让用户做常用配置   /bin/settings.py(手动配置) PLUGIN_ITE ...

  4. 安装icephp 记

    下面开始安装: 一:yum 安装 首先需要添加一个yum源. # vi /etc/yum.repos.d/zeroc-ice-amzn.repo写入: [zeroc-ice]name=Ice 5 fo ...

  5. OC 07 类的扩展

    1.NSDate的使用 NSDate是Cocoa中⽤于处理⽇期和时间的基础类,封装了某⼀给定的时刻(含日期,时间,时区) 注意NSLog(@“%@”,nowDate);⽆论你是哪个时区的时间,打印时总 ...

  6. jdk1.8在linux环境下的安装

    转自博客:http://www.cnblogs.com/ShawnYuki/p/6816179.html

  7. 微信小程序使用canvas绘制图片的注意事项

    1.单位换算问题,canvas的尺寸单位是px,所以单位需要做个换算,可以通过wx.getSystemInfo获取屏幕宽高(单位是px),微信小程序无论什么机型,屏幕宽度都是750rpx,因此可以做个 ...

  8. MYSQL表中设置字段类型为TIMESTAMP时的注意事项

    在MYSQL中,TIMESTAMP类型是用来表示日期的,但是和DATETIME不同,不同点就不再这里说明了. 当我们在使用TIMESTAMP类型设置表中的字段时,我们应该要注意一点,首先我们在表中新增 ...

  9. Html Css  练习

    一.  取消a链接的下划线 <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  10. P2737 [USACO4.1]麦香牛块Beef McNuggets(完全背包+数论确定上界)

    题目链接:https://www.luogu.org/problem/show?pid=2737 题目大意:农夫布朗的奶牛们正在进行斗争,因为它们听说麦当劳正在考虑引进一种新产品:麦香牛块.奶牛们正在 ...