Fibonacci-ish

Time Limit: 3000MS   Memory Limit: 524288KB   64bit IO Format: %I64d & %I64u

Submit
Status

Description

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.

Sample Input

Input
3
1 2 -1
Output
3
Input
5
28 35 7 14 21
Output
4

Sample Output

Hint

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.

Source


我去,以前做过的,就记得是dfs,但是忘记要用map跟去重了,真是人老了,记性不行咯
#include<iostream>
#include<map>
#include<cstring>
#include<algorithm>
using namespace std;
int num[1010];
map<int, int>fp;
int DFS(int a, int b)
{
int ans = 0;
if (fp[a + b])
{
fp[a + b]--;
ans=DFS(b, a + b)+1;
fp[a + b]++;
}
return ans;
}
int main()
{
int n;
while (cin >> n)
{
memset(num, 0, sizeof(num));
fp.clear();
for (int i = 0; i < n; i++)
cin >> num[i], fp[num[i]]++;
sort(num, num + n);
int ans = 0;
int N = unique(num, num + n)-num;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (i == j&&fp[num[i]] == 1) continue;
fp[num[i]]--, fp[num[j]]--;
ans = max(ans, DFS(num[i], num[j]) + 2);
fp[num[i]]++, fp[num[j]]++;
}
}
cout << ans << endl;
}
return 0;
}

Codeforces--633D--Fibonacci-ish (map+去重)(twice)的更多相关文章

  1. codeforces 633D D. Fibonacci-ish(dfs+暴力+map)

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

  2. CodeForces - 633D Fibonacci-ish 大数标记map+pair的使用

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

  3. codeforces 633D - Fibonacci-ish 离散化 + 二分查询

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

  4. Codeforces 977F - Consecutive Subsequence - [map优化DP]

    题目链接:http://codeforces.com/problemset/problem/977/F 题意: 给定一个长度为 $n$ 的整数序列 $a[1 \sim n]$,要求你找到一个它最长的一 ...

  5. Codeforces 126D Fibonacci Sums 求n由随意的Sum(fib)的方法数 dp

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qq574857122/article/details/34120269 题目链接:点击打开链接 题意 ...

  6. CodeForces 567C. Geometric Progression(map 数学啊)

    题目链接:http://codeforces.com/problemset/problem/567/C C. Geometric Progression time limit per test 1 s ...

  7. Map去重,去重value相同的元素,保留key最小的那个值

    Map<Integer,String>,Integer代表时间撮,String代表文本信息去重函数:就是删除Map中value相同的元素,只保留key最小的那个元素 public stat ...

  8. Codeforces 567C - Geometric Progression - [map维护]

    题目链接:https://codeforces.com/problemset/problem/567/C 题意: 给出长度为 $n$ 的序列 $a[1:n]$,给出公比 $k$,要求你个给出该序列中, ...

  9. Codeforces 193E - Fibonacci Number(打表找规律+乱搞)

    Codeforces 题目传送门 & 洛谷题目传送门 蠢蠢的我竟然第一眼想套通项公式?然鹅显然 \(5\) 在 \(\bmod 10^{13}\) 意义下并没有二次剩余--我真是活回去了... ...

随机推荐

  1. js获取微信版本号的方法

    微信现在几乎已经普遍在使用,但是微信版本一直在更新,不同的版本有不同的功能,于是了解微信的版本号很重要,以便自己及时更新体验新的功能.那么如何用js获取微信版本号呢? 一.判断微信版本号 ? 1 2 ...

  2. js判断是安卓 还是 ios webview?

    通过判断浏览器的userAgent,用正则来判断是否是ios和Android客户端.代码如下: <script type="text/javascript"> var ...

  3. java虚拟(一)--java内存区域和常量池概念

    一.java运行时数据区 也可以称为java内存区域,和java内存模型不是一回事,不要弄混,这里基于jdk1.8之前 1.1.方法区 线程共享,类装载过程中产生的java.lang.Class对象保 ...

  4. 用Docker构建Nginx镜像

    1构建Nginx镜像 1建立工作目录 [root@localhost ]# mkdir 1nginx [root@localhost 1nginx]# cd 1nginx/ [root@localho ...

  5. Rest 参数(...)

    javascript 之Rest 参数(...) ES6 Rest参数 Rest就是为解决传入的参数数量不一定, rest parameter(Rest 参数) 本身就是数组,数组的相关的方法都可以用 ...

  6. 网络编程 - 简单的socket例子

    1.客户端 #客户端import socketclient=socket.socket() #生成socket连接对象client.connect(("localhost",696 ...

  7. Invalid ON UPDATE clause for 'create_date' column

    高版本的mysql导数据到低版本出现的问题 日期类型报错 解决方式:将datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT  中的  ON ...

  8. 在MONO Design中使用Flex3D

    在项目开发组的努力下,HTML5 3D 的开发包变得越来越优秀,越来越健壮:基于HTML5 3D技术的MONO Design建模平台功能也变得越来越强大和完善,这个方便了很多使用我们HTML5 3D的 ...

  9. hadoop 3.0.0新特性

    1.Minimum required Java version increased from Java 7 to Java 8 java最低支持版本变成java8 2.Support for eras ...

  10. 校长的收藏(洛谷 U4534)

    题目背景 XS中学的校长喜欢收集手办,家里面都是价值不菲的手办. 校长喜欢给手办们排队并且对于某些些区间内的手办喜爱有加. 现在,校长外出散步(找乐子),你潜入他的房间打算借(偷走)他的手办炫耀一下. ...