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}\) 意义下并没有二次剩余--我真是活回去了... ...
随机推荐
- CAD利用Select2得到所有实体(网页版)
主要用到函数说明: IMxDrawSelectionSet::Select2 构造选择集.详细说明如下: 参数 说明 [in] MCAD_McSelect Mode 构造选择集方式 [in] VARI ...
- 梦想CAD控件打印相关
一.打印设置 在顶部快速访问工具栏单击打印按钮或者直接输入PLOT命令或者点击打印控制的打印设置按钮打开打印对话框.c#代码实现如下: //打印设置 private void Print1() { ...
- 用Docker构建Tomcat镜像
构建tomcat镜像 创建工作目录 [root@elk-node2 tomcat]# mkdir tomcat [root@elk-node2 tomcat]# cd tomcat [root@elk ...
- docker 转载
写的非常好的一篇文章,不知道为什么被删除了. 利用Google快照,做个存档. 快照地址:地址 作者地址:青牛 什么是docker Docker 是一个开源项目,诞生于 2013 年初,最初是 do ...
- printf函数压栈(i++/i--,++i/--i) 终极解密
#include <stdio.h> void main() { ; printf("%d %d %d %d\n", i, --i, i, i--); } 输出是“3 ...
- try catch影响Spring事务吗?
对于这个问题有两种情况: 1.catch只打印异常,不抛出异常 try { 数据库做添加订单表; /; 数据库减少库存; }catch (Exception e){ e.printStackTrace ...
- Django DTL模板语法中的循环
from django.shortcuts import render def index(request): context={ 'books':[ '5年高考3年模拟', '家猪养殖与配种', ' ...
- lucas定理和组合数学
自湖南长沙培训以来的坑...一直未填,今天把这个问题解决掉. 参考: 1.http://www.cnblogs.com/Var123/p/5523068.html 2.http://blog.csdn ...
- JavaScript 面向对象的编程(二) 类的封装
类的定义 方式一 var Book = function(id, name, price){ //私有属性,外部不能直接访问 var num = 1; //私有方法, function checkId ...
- 百练4152:最佳加法表达式(dp+高精度)
描述 给定n个1到9的数字,要求在数字之间摆放m个加号(加号两边必须有数字),使得所得到的加法表达式的值最小,并输出该值.例如,在1234中摆放1个加号,最好的摆法就是12+34,和为36 输入有不超 ...