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. Spring框架系列(六)--事务Transaction

    本文绝大部分内容为转载,原文地址:https://blog.csdn.net/trigl/article/details/50968079 除此之外,后面还有延伸内容 事务在企业日常开发中几乎是一定会 ...

  2. 牛客多校Round 10

    咕咕咕.... 去烽火台和兵马俑了

  3. 【原】Python学习

    1.常用模块介绍 #python -m SimpleHTTPServer 执行上面的命令就会在服务器当前目录下启动一个文件下载服务器,默认打开8000端口.这个时候,你只需要将IP和端口告诉客户端,即 ...

  4. css--小白入门篇1

    一.引入 css用来描述html,学习css前我们先来学习html的基础标签的用法,再进入css的学习. 本教程面向小白对象,不会讲细枝末节深入的东西. 二.列表 列表有3种 2.1 无序列表 无序列 ...

  5. 「 HDU P3336 」 Count the string

    题目大意 给出一个长度为 $n$ 的字符串 $s$ 要求你求出 $s$ 的每一个前缀在 $s$ 中出现的次数之和.$n\le 200000$. 解题思路 暴力的对每一个前缀进行一次匹配,求出出现次数后 ...

  6. [Algorithm] 8. Rotate String

    Description Given a string and an offset, rotate string by offset. (rotate from left to right) Examp ...

  7. zabbix3.4调用钉钉报警通知(超详细)

     一.备注: zabbix调用钉钉接口报警通知有两种情况: 1.通知到个人钉 2.通知到钉钉群 本文主要介绍zabbix调用钉钉接口通知到钉钉个人的方式 二.zabbix3.4调用钉钉接口报警通知到个 ...

  8. 转来的——python webdriver自动化测试初步印象——转来的

    python webdriver自动化测试初步印象 以下示例演示启动firefox,浏览google.com,搜索Cheese,等待搜索结果,然后打印出搜索结果页的标题 from selenium i ...

  9. python之cookbook-day02

    第一章:数据结构和算法 1.2 解压可迭代对象赋值给多个变量 问题: 如果一个可迭代对象的元素个数超过变量个数时,会抛出一个 ValueError .那么 怎样才能从这个可迭代对象中解压出 N 个元素 ...

  10. Ubuntu notes

    ubuntu notes Table of Contents 1. backup data 2. Basics Ubuntu 3. Install, uninstall packages 4. Bas ...