CodeForces - 633D Fibonacci-ish 大数标记map+pair的使用
Fibonacci-ish
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.
Examples
3
1 2 -1
3
5
28 35 7 14 21
4
Note
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.
从已知元素中寻找最长斐波那契长度。
个人非常喜欢的一道题,比较综合。用到了一些套路,比如用map标记大数,以及加上pair来标记一组数。
开始有往dp方向考虑,想先给数列排个序,然后依次往右找。但是两数相加不一定会变大,和可能会在左边,比如本题负数情况。
再一个每次寻找下一个值时,都要重新使用标记,为了防止后效性,使用递归来处理。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<set>
#include<map>
#include<algorithm>
#define MAX 1005
#define INF 0x3f3f3f3f
using namespace std; int a[MAX],c;
map<int,int> mp;
map<pair<int,int>,int> mmp;
void dfs(int x,int y){ //递归寻找 if(mp[x+y]>) {
c++;
mp[x+y]--;
dfs(y,x+y);
mp[x+y]++;
}
}
int main()
{
int n,i,j;
scanf("%d",&n);
for(i=;i<=n;i++){
scanf("%d",&a[i]);
mp[a[i]]++; //大数标记
}
int max=;
for(i=;i<=n;i++){
for(j=;j<=n;j++){ //考虑负数情况
if(i!=j){
if(mmp[make_pair(a[i],a[j])]) continue;
mmp[make_pair(a[i],a[j])]=; //标记一对数,优化防T
c=;
mp[a[i]]--;
mp[a[j]]--;
dfs(a[i],a[j]);
mp[a[i]]++;
mp[a[j]]++;
if(c>max) max=c;
}
}
}
printf("%d\n",max);
return ;
}
CodeForces - 633D Fibonacci-ish 大数标记map+pair的使用的更多相关文章
- 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 Round #378 (Div. 2) D. Kostya the Sculptor map+pair
D. Kostya the Sculptor time limit per test 3 seconds memory limit per test 256 megabytes input stand ...
- 2018 ICPC 徐州网络预赛 Features Track (STL map pair)
[传送门]https://nanti.jisuanke.com/t/31458 [题目大意]有N个帧,每帧有K个动作特征,每个特征用一个向量表示(x,y).两个特征相同当且仅当他们在不同的帧中出现且向 ...
- BZOJ 4236 "JOIOJI"(前缀和+map+pair)
传送门: [1]:BZOJ [2]:洛谷 •题解 定义数组 a,b,c 分别表示 'J' , 'O' , 'I' 的前缀和: 要想使区间 (L,R] 满足条件当且仅当 a[R]-a[L] = b[R] ...
- Codeforces Round #486 (Div. 3) C "Equal Sums" (map+pair<>)
传送门 •题意 给k个数列,从中k个数列中找出任意2个数列 i ,j 使得数列i删除第x个数,和数列j删除第y个数的和相等 若存在,输出 i ,x 和 j,y •思路 每个数列之间的联系为数列的和之间 ...
- Codeforces Round #188 (Div. 2) C. Perfect Pair 数学
B. Strings of Power Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/318/p ...
- Military Problem CodeForces - 1006E(dfs搜一下 标记一下)
题意: 就是有一颗树 然后每次询问 父结点 的 第k个结点是不是他的子嗣...是的话就输出这个子嗣..不是 就输出-1 解析: 突然想到后缀数组的sa 和 x的用法..就是我们可以用一个id标记当前 ...
- Codeforces Round #462 (Div. 2) A Compatible Pair
A. A Compatible Pair time limit per test1 second memory limit per test256 megabytes Problem Descript ...
- SCAU1143 多少个Fibonacci数--大菲波数【杭电-HDOJ-1715】--高精度加法--Fibonacci数---大数比较
/*******对读者说(哈哈如果有人看的话23333)哈哈大杰是华农的19级软件工程新手,才疏学浅但是秉着校科联的那句“主动才会有故事”还是大胆的做了一下建一个卑微博客的尝试,想法自己之后学到东西都 ...
随机推荐
- C#中GroupBox控件的使用(转)
GroupBox(框架)控件是C#中用来组织其他控件形成一个控件组,它的使用方法为[工具箱]->[所有Windows窗体](或者是[容器]列表中)->[GroupBox],拖拽到窗体界面中 ...
- 九度OJ 1030:毕业bg (01背包、DP)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1814 解决:798 题目描述: 每年毕业的季节都会有大量毕业生发起狂欢,好朋友们相约吃散伙饭,网络上称为"bg" ...
- cocos2d-js添加艾盟插屏(通过jsb反射机制)
1.导入jar包 2.修改AndroidManifest.xml文件 添加: <activity android:name="com.xingka ...
- SecureCRT 会话设置项
登陆动作------自动登陆仿真------两个颜色复选框都勾上模式------光标键模式(2个复选框)映射键------使用windows复制和粘贴热键外观------字符编码:UTF-8外观--- ...
- 一起来学linux:日志文件
在管理系统当中,经常会遇到各种各样的错误和异常.要找到这些错误和异常,就需要各种日志来帮助定位问题了.linux的日志都是存放在/var/log这个文件夹下面,常见的日志文件有如下几种;/var/lo ...
- POJ - 1611 The Suspects 【并查集】
题目链接 http://poj.org/problem?id=1611 题意 给出 n, m 有n个人 编号为 0 - n - 1 有m组人 他们之间是有关系的 编号为 0 的人是 有嫌疑的 然后和 ...
- 算法(Algorithms)第4版 练习 1.3.25 1.3.24
代码实现: //1.3.24 /** * remove the node following the node x * (and does nothing if the argument or the ...
- Linux学习之路(五)压缩命令
常用压缩格式: .zip .gz .bz2 常用压缩格式: .tar.gz .tar.bz2 .zip格式压缩 .zip 压缩文件名 源文件 #压缩文件 .zip -r 压缩文件名 源目录 #压缩目录 ...
- php设计模式课程---9、桥接模式是什么
php设计模式课程---9.桥接模式是什么 一.总结 一句话总结: 一个类没干完,另外一个类接着给它干完 实质是类的拼接,也就是用类的组合代替了类的继承,因为类的组合可以有很多种方式,所以桥接就是类的 ...
- laravel基础课程---12、lavarel的ajax操作2(lavarel的ajax使用总结)
laravel基础课程---12.lavarel的ajax操作2(lavarel的ajax使用总结) 一.总结 一句话总结: 比较简单:就是js请求ajax,然后控制器获取请求参数,返回数据即可 1. ...