Codeforces 633D Fibonacci-ish 【暴力递归】
2016-02-29 21:03
369 查看
D. Fibonacci-ish
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
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.
Examples
input
output
input
output
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
![](http://codeforces.com/predownloaded/48/e9/48e9b1a07010edd8b0fcc41851d308fc75dcdedf.png)
,
![](http://codeforces.com/predownloaded/7b/b0/7bb0fcb11f088129a8f3a522654217e1497e592c.png)
,
![](http://codeforces.com/predownloaded/94/5e/945ea01f0389d2607ef4b429c3b6aa96b3c1697f.png)
,
![](http://codeforces.com/predownloaded/15/21/1521976773597725597fa26f41c3a29fce3e798b.png)
, 28.
定义 K数列:f[0]、f[1]任意,对于n > 1,f
= f[n-1] + f[n-2]。
题意:给定n个元素,构造最长的K数列。
思路:以为暴力枚举会T。。。记录元素个数并去重,递归构造。
AC代码:
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
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.
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
![](http://codeforces.com/predownloaded/48/e9/48e9b1a07010edd8b0fcc41851d308fc75dcdedf.png)
,
![](http://codeforces.com/predownloaded/7b/b0/7bb0fcb11f088129a8f3a522654217e1497e592c.png)
,
![](http://codeforces.com/predownloaded/94/5e/945ea01f0389d2607ef4b429c3b6aa96b3c1697f.png)
,
![](http://codeforces.com/predownloaded/15/21/1521976773597725597fa26f41c3a29fce3e798b.png)
, 28.
定义 K数列:f[0]、f[1]任意,对于n > 1,f
= f[n-1] + f[n-2]。
题意:给定n个元素,构造最长的K数列。
思路:以为暴力枚举会T。。。记录元素个数并去重,递归构造。
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <stack> #include <algorithm> #include <map> #define CLR(a, b) memset(a, (b), sizeof(a)) using namespace std; typedef long long LL; const int MAXN = 1001; int a[MAXN]; map<int, int> fp, tp; int Count(int a, int b) { int ans = 0; if(fp[a+b]) { fp[a+b]--; //fp[b]--; ans = Count(b, a+b) + 1; fp[a+b]++; //fp[b]++; } return ans; } int main() { int n; cin >> n; fp.clear(); for(int i = 0; i < n; i++) cin >> a[i], fp[a[i]]++; if(fp[0] == n) { cout << n << endl; return 0; } sort(a, a+n); int top = unique(a, a+n) - a; int ans = 0; for(int i = 0; i < top; i++) { for(int j = 0; j < top; j++) { if(i == j && fp[a[i]] == 1) continue; fp[a[i]]--; fp[a[j]]--; //cout << Count(a[i], a[j]) << endl; ans = max(ans, Count(a[i], a[j]) + 2); fp[a[i]]++; fp[a[j]]++; } } cout << ans << endl; return 0; }
相关文章推荐
- java中volatile关键字的含义
- 第三方瀑布流layout:UICollectionViewWaterfallLayout
- Codeforces 633C Spy Syndrome 2 【字典树 + DFS】
- Spring mvc整合Mybatis例子
- Codeforces 633B A Trivial Problem 【数论】
- [LeetCode]28. Implement strStr()
- MEID与IMEI
- LVS + Keeplaved 配置
- UITableView的性能优化,让TableView更加的丝滑!
- poj-1321-棋盘问题(dfs)
- 2012年 蓝桥杯C++省赛 古堡算式
- 蓝桥杯 李白打酒
- Git初体验之远程仓库
- Java回调
- Codeforces 633A Ebony and Ivory 【水题】
- windows基础编程----第三篇(窗口的消息处理机制)
- leetcode刷题,总结,记录,备忘, 279
- AudioUnit的学习
- c++单例 通过cocos2dx重现c++单例—— c++语法复习
- 226. Invert Binary Tree