http://codeforces.com/problemset/problem/633/D

D. 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.

题意:给出 n个数,求出他们任意排列之后最长的斐波那契数列是多长。

思路:这题也是听了师兄的讲解之后才会做的。因为斐波那契数的递增是巨大的,1e9里面的斐波那契数是比较少的,

因此暴力枚举,比较巧妙的是用map来存一个数有没有出现过,并且如果是很多个零的话要加一个特判,不然的话可能会超时。

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
#define N 1010 map <int, int> mp;
int a[N];
int temp[N]; int main()
{
int n ;
cin >> n;
mp.clear();
for(int i = ; i < n; i++){
scanf("%d", a + i);
mp[a[i]]++;
//该数出现的次数
}
int ans = , now = , cnt, x, y, z;
for(int i = ; i < n; i++){
for(int j = ; j < n; j++){
if(i == j) continue;
x = a[i], y = a[j];
if( x == && y == ){
//两个都是0的话,直接判断0的个数,因为0+0=0
if(mp[] > ans) ans = mp[];
continue;
}
cnt = ;
mp[x]--; mp[y]--;
temp[cnt++] = x;
temp[cnt++] = y;
//先把要加的数从map里面删除,避免重复,用temp存放,之后再放回去
while(mp[x+y] > ){
z = x + y;
mp[z]--;
temp[cnt++] = z;
x = y;
y = z;
}
for(int i = ; i < cnt; i++)
mp[temp[i]]++;
if(cnt > ans) ans = cnt;
}
}
printf("%d\n",ans);
return ;
}

Coderforces 633D:Fibonacci-ish(map+暴力枚举)的更多相关文章

  1. CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)

    题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ...

  2. HNU 12886 Cracking the Safe(暴力枚举)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...

  3. POJ-3187 Backward Digit Sums (暴力枚举)

    http://poj.org/problem?id=3187 给定一个个数n和sum,让你求原始序列,如果有多个输出字典序最小的. 暴力枚举题,枚举生成的每一个全排列,符合即退出. dfs版: #in ...

  4. CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)

    问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...

  5. HDU - 1248 寒冰王座 数学or暴力枚举

    思路: 1.暴力枚举每种面值的张数,将可以花光的钱记录下来.每次判断n是否能够用光,能则输出0,不能则向更少金额寻找是否有能够花光的.时间复杂度O(n) 2.350 = 200 + 150,买350的 ...

  6. POJ 3080 Blue Jeans (字符串处理暴力枚举)

    Blue Jeans  Time Limit: 1000MS        Memory Limit: 65536K Total Submissions: 21078        Accepted: ...

  7. こだわり者いろはちゃん / Iroha's Obsession (暴力枚举)

    题目链接:http://abc042.contest.atcoder.jp/tasks/arc058_a Time limit : 2sec / Memory limit : 256MB Score ...

  8. Codeforces Round #298 (Div. 2) B. Covered Path 物理题/暴力枚举

    B. Covered Path Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/probl ...

  9. Codeforces Round #266 (Div. 2)B(暴力枚举)

    很简单的暴力枚举,却卡了我那么长时间,可见我的基本功不够扎实. 两个数相乘等于一个数6*n,那么我枚举其中一个乘数就行了,而且枚举到sqrt(6*n)就行了,这个是暴力法解题中很常用的性质. 这道题找 ...

随机推荐

  1. Marker

    # 样例 <?xml version="1.0" encoding="UTF-8"?> <Configuration status=" ...

  2. FileHelper

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Win ...

  3. windows管理员利器之用Log Parser Studio分析IIS日志(附逐浪CMS官方命令集)

    原文:windows管理员利器之用Log Parser Studio分析IIS日志(附逐浪CMS官方命令集) Log Parser Studio是一个强大的IIS图形分析工具,值得推荐. 1. 安装L ...

  4. 【Linux】简单明了查看内存使用和ubuntu的版本号及位数

    1.查看ubuntu的版本号:cat /etc/issue 2.查看系统是32位的还是64位:getconf LONG_BIT 3.查看内存使用 free free命令可以用来查看系统内存使用情况,- ...

  5. 零元学Expression Blend 4 - Chapter 26 教你如何使用RaidoButton以及布局容器的活用

    原文:零元学Expression Blend 4 - Chapter 26 教你如何使用RaidoButton以及布局容器的活用 本章将教大家如何运用Blend的内建元件RaidoButton做出选单 ...

  6. 基于事件驱动的DDD领域驱动设计框架分享(附源代码)

    原文:基于事件驱动的DDD领域驱动设计框架分享(附源代码) 补充:现在再回过头来看这篇文章,感觉当初自己偏激了,呵呵.不过没有以前的我,怎么会有现在的我和现在的enode框架呢?发现自己进步了真好! ...

  7. Android View 滚动边界的测量

    最近一直在用Android TV的RecyclerView,实现视频搜索列表卡片的滚动显示,由于采用了双排滚动,打破了系统默认的单排滚动,且每一屏幕显示10个完整卡片5个半漏边卡片,每个完整卡片的左下 ...

  8. elasticsearch local debug环境搭建

    最近计划看看elasticsearch的源码,首先得把local debug环境搞定. 下载源码.因为公司产线是5.6.5,所以就下载了5.6.5的代码. 源码编译.先进入到/elasticsearc ...

  9. 写一个可拖动的 TShape(简单有效:依靠VCL体系,TShape自己就能被探测到被点击了,然后只要改变Left坐标就行了)

    问题来源: http://www.cnblogs.com/del/archive/2009/03/09/1234066.html#1471535 本例效果图: 自定义类(TMyShape)单元 : u ...

  10. C# Task 的用法

    C# Task 的用法(转自:http://www.wxzzz.com/683.html#) 其实Task跟线程池ThreadPool的功能类似,不过写起来更为简单,直观.代码更简洁了,使用Task来 ...