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}\) 意义下并没有二次剩余--我真是活回去了... ...
随机推荐
- ThinkPHP---TP功能类之邮件
[一]概论 (1)简介: 这里说的邮件不是平时说的email邮件(邮件地址带有@符号的),而是指的一般论坛网站的站内信息,也叫私信或者pm(private message私信) [二]站内信案例 (1 ...
- 洛谷——P1120 小木棍 [数据加强版]
P1120 小木棍 [数据加强版] 题目描述 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过5050. 现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍 ...
- mysql5.7报Access denied for xxx@localhost 的解决
使用root用户登录mysql数据库若如下报错 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using passwor ...
- Semi-colon expected (eclipse 引入 json文件报错)
最近做的项目用到的前端框架有一个json文件夹,里面全是json文件,所以导入Eclipse的时候会在整个项目上都是红叉,但是其实不影响项目运行的,之前忙着码代码也没时间管他,这个红叉存留了一个月!今 ...
- Flask项目中整合各组件
一.介绍 主要介绍flask_sqlalchemy.flask_script.flask_migrate这三个组件该如何整合到flask项目中,以及如何使用. # 安装组件 pip3 install ...
- MySQL Connector/Python 接口 (一)
这里仅介绍 MySQL 官方开发的 Python 接口,参见这里: https://dev.mysql.com/doc/connector-python/en/ Chapter 1 Introduct ...
- java List 数组删除元素
在 java 中,ArrayList 是一个很常用的类,在编程中经常要对 ArrayList 进行增.删.改.查操作.之前在学校时一直认为删除操作是最简单的,现在才越发觉得自己愚蠢.只需要设置好预期条 ...
- shrink&split
shrink将分片数按因子缩减.hard link segment文件.因缩减前后hash一致,不需要rehash.如:0 ,1 , 2, 3, 4, 5, 6, 7, 8.9个分片缩减成3个:0 [ ...
- 【10】AngularJS SQL
AngularJS SQL 使用 PHP 从 MySQL 中获取数据 <div ng-app="myApp" ng-controller="customersCtr ...
- BNUOJ 14381 Wavio Sequence
Wavio Sequence Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Origina ...