Codeforces--633D--Fibonacci-ish(暴力搜索+去重)(map)
来源:互联网 发布:mac os sierra 下载 编辑:程序博客网 时间:2024/06/04 18:06
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 alln ≥ 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 sequenceai.
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
31 2 -1
3
528 35 7 14 21
4
Sample Output
Hint
In the first sample, if we rearrange elements of the sequence as - 1, 2, 1, the whole sequenceai would be Fibonacci-ish.
In the second sample, the optimal way to rearrange elements is ,,,,28.
Source
#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)
- Codeforces--633D--Fibonacci-ish(map+去重)(twice)
- CodeForces - 633D Fibonacci-ish (map&暴力)
- Codeforces 633D Fibonacci-ish(搜索)
- CodeForces 633D Fibonacci-ish(容器暴力)
- Codeforces 633D Fibonacci-ish 【暴力递归】
- Codeforces 633D Fibonacci-ish(暴力)
- Codeforces 633D Fibonacci-ish 数学+暴力
- CodeForces 633 D.Fibonacci-ish(枚举)
- Manthan, Codefest 16 D. Fibonacci-ish(暴力、规律)
- Manthan, Codefest 16 D. Fibonacci-ish(暴力)
- 【Manthan, Codefest 16D】【暴力+去重or特判】Fibonacci-ish n个数组成最长斐波那契数列的长度
- [莫队算法 线段树 斐波那契 暴力] Codeforces 633H Fibonacci-ish II
- codeforces 651C(map、去重)
- HDU 4277 USACO ORZ (暴力搜索+set去重)
- codeforces633D Fibonacci-ish map容器
- Manthan, Codefest 16 633D Fibonacci-ish(脑洞+stl)
- HDU 5167 Fibonacci(DFS暴力搜索)
- meta标签中的http-equiv属性使用介绍
- 近5年126个Java面试问题汇总 收藏
- 深度学习阅读列表 Deep Learning Reading List
- MergeKSortedList
- 深入理解Java异常处理机制
- Codeforces--633D--Fibonacci-ish(暴力搜索+去重)(map)
- k近邻算法实例
- STL-list实现
- WPF中抛出NullReferenceException:System.Windows.Markup.IStyleConnector.Connect(Int32, System.Object)
- 查找BADI的方法
- 51nod 1019 逆序数
- BZOJ 1878 HH的项链(莫队算法)
- 安卓系统启动脚本init.rc说明文件readme.txt翻译
- Error instantiating class book.pojo.User with invalid types () or values (). 报错解决方法