Codeforces--633D--Fibonacci-ish (map+去重)(twice)
Time Limit: 3000MS | Memory Limit: 524288KB | 64bit IO Format: %I64d & %I64u |
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 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
3
1 2 -1
3
5
28 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 sequence
ai would be Fibonacci-ish.
In the second sample, the optimal way to rearrange elements is ,
,
,
,
28.
Source
#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)的更多相关文章
- 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 ...
- CodeForces - 633D Fibonacci-ish 大数标记map+pair的使用
Fibonacci-ish Yash has recently learnt about the Fibonacci sequence and is very excited about it. He ...
- codeforces 633D - Fibonacci-ish 离散化 + 二分查询
Fibonacci-ish Yash has recently learnt about the Fibonacci sequence and is very excited about it. He ...
- Codeforces 977F - Consecutive Subsequence - [map优化DP]
题目链接:http://codeforces.com/problemset/problem/977/F 题意: 给定一个长度为 $n$ 的整数序列 $a[1 \sim n]$,要求你找到一个它最长的一 ...
- Codeforces 126D Fibonacci Sums 求n由随意的Sum(fib)的方法数 dp
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qq574857122/article/details/34120269 题目链接:点击打开链接 题意 ...
- CodeForces 567C. Geometric Progression(map 数学啊)
题目链接:http://codeforces.com/problemset/problem/567/C C. Geometric Progression time limit per test 1 s ...
- Map去重,去重value相同的元素,保留key最小的那个值
Map<Integer,String>,Integer代表时间撮,String代表文本信息去重函数:就是删除Map中value相同的元素,只保留key最小的那个元素 public stat ...
- Codeforces 567C - Geometric Progression - [map维护]
题目链接:https://codeforces.com/problemset/problem/567/C 题意: 给出长度为 $n$ 的序列 $a[1:n]$,给出公比 $k$,要求你个给出该序列中, ...
- Codeforces 193E - Fibonacci Number(打表找规律+乱搞)
Codeforces 题目传送门 & 洛谷题目传送门 蠢蠢的我竟然第一眼想套通项公式?然鹅显然 \(5\) 在 \(\bmod 10^{13}\) 意义下并没有二次剩余--我真是活回去了... ...
随机推荐
- elk大纲
一.ELK功能概览 1.检索 2.数据可视化--实时监控(实时刷新) nginx 访问量 ip地区分布图(大数据) 3.zabbix 微信联动报警 4.大数据日志分析平台(基于hadoop) 二.ka ...
- 16监听器、Filter、Nginx、Spring、AOP
16监听器.Filter.Nginx.Spring.AOP-2018/07/30 1.监听器 监听web对象创建与销毁的监听器 ServletContextListener HttpSessionLi ...
- [Luogu] P3258 [JLOI2014]松鼠的新家
题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在”树“上. 松鼠想邀请小熊维尼前 ...
- Python字符串(Python学习笔记02)
字符串 Python 3 中的字符串可以使用双引号或单引号标示,如果字符串出现引号,则可以使用 \ 来去除引号标示字符串的作用. 几种字符串的表示方法: str1 = "hello" ...
- BZOJ 1370 团伙
两个认识的人不是朋友就是敌人,且满足: 1,朋友的朋友是朋友: 2,敌人的敌人是朋友. 一群朋友组成一个团伙,给出m个信息,求有多少个团伙. 用并查集,把一个点x拆成x和x’ 若a与b为朋友,则将a与 ...
- uva 1444 Knowledge for the masses
uva 1444 Description You are in a library equipped with bookracks that move on rails. There are ma ...
- Python基础(十一) 异常处理
在程序运行过程中,总会遇到各种各样的错误,有的错误是程序编写有问题造成的,比如本来应该输出整数结果输出了字符串,这样的错误我们通常称之为BUG,BUG是必须修复的.在Python中内置了一套异常处理机 ...
- VMWare学习总结(1)——Centos7安装完毕后无法联网的解决方法
在VmWare 上安装Centos7时,装好vmware后还是连不上网,通过查找资料原来是因为有线网卡没有激活,默认centos和redhat7都是不启用有线网卡的,要么手动开启,要么安装时直接启用! ...
- BNUOJ 2528 Mayor's posters
Mayor's posters Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Origin ...
- Leetcode 76.最小覆盖子串
最小覆盖子串 给定一个字符串 S 和一个字符串 T,请在 S 中找出包含 T 所有字母的最小子串. 示例: 输入: S = "ADOBECODEBANC", T = "A ...