Codeforces--633D--Fibonacci-ish(暴力搜索+去重)(map)
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<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
map<int,int>fp;
int a[1010];
int f(int a,int b)
{
int ans=0;
if(fp[a+b])
{
fp[a+b]--;
ans=f(b,a+b)+1;
fp[a+b]++;
}
return ans;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
fp.clear();
for(int i=0;i<n;i++)
scanf("%d",&a[i]),fp[a[i]]++;
sort(a,a+n);
int N=unique(a,a+n)-a;
int ans=0;
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
if(i==j&&fp[a[i]]==1) continue;
fp[a[i]]--,fp[a[j]]--;
ans=max(ans,f(a[i],a[j])+2);
fp[a[i]]++,fp[a[j]]++;
}
}
printf("%d\n",ans);
}
return 0;
}
Codeforces--633D--Fibonacci-ish(暴力搜索+去重)(map)的更多相关文章
- ACM 暴力搜索题 题目整理
UVa 129 Krypton Factor 注意输出格式,比较坑爹. 每次要进行处理去掉容易的串,统计困难串的个数. #include<iostream> #include<vec ...
- Codeforces Gym 100431B Binary Search 搜索+组合数学+高精度
原题链接:http://codeforces.com/gym/100431/attachments/download/2421/20092010-winter-petrozavodsk-camp-an ...
- HDU 3131 One…Two…Five! (暴力搜索)
题目链接:pid=3131">HDU 3131 One-Two-Five! (暴力搜索) 题意:给出一串数字,要求用加,减,乘,除(5/2=2)连接(计算无优先级:5+3*6=8*6= ...
- POJ 1129:Channel Allocation 四色定理+暴力搜索
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13357 Accepted: 68 ...
- 「状压DP」「暴力搜索」排列perm
「状压DP」「暴力搜索」排列 题目描述: 题目描述 给一个数字串 s 和正整数 d, 统计 sss 有多少种不同的排列能被 d 整除(可以有前导 0).例如 123434 有 90 种排列能被 2 整 ...
- hdu 4740 The Donkey of Gui Zhou(暴力搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4740 [题意]: 森林里有一只驴和一只老虎,驴和老虎互相从来都没有见过,各自自己走过的地方不能走第二次 ...
- hdu 1427 速算24点 dfs暴力搜索
速算24点 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem De ...
- UVALive 5844 dfs暴力搜索
题目链接:UVAive 5844 Leet DES:大意是给出两个字符串.第一个字符串里的字符可以由1-k个字符代替.问这两个字符串是不是相等.因为1<=k<=3.而且第一个字符串长度小于 ...
- 随手练——洛谷-P1151(枚举与暴力搜索)
枚举 #include <iostream> using namespace std; int main() { ; cin >> k; ; i < ; i++) { ) ...
随机推荐
- C#入门经典 Chapter4 流程控制
4.1布尔逻辑 布尔比较运算符 == != < > <= >= 处理布尔值的布尔值运算符 ! & | ^(异或) 条件布尔运算符 &&am ...
- 移动web——bootstrap模板
基本概念 1.bootstrap就是在媒体查询技术出现以后才开始出现的 2.此技术使响应式开发变得十分轻松,最大特点就是栅格系统(什么设备上如何显示)以及响应式工具(是否可见) 基本模板 <!D ...
- ZfNet解卷积:可视化CNN模型( PythonCode可视化Cifar10)
原文链接:caffe Model的可视化 snapshot: 6000 一个在线可视化小工具:http://blog.csdn.net/10km/article/details/52713 ...
- [文章转载]-Java后端,应该日常翻看的中文技术网站 -江南白衣
Java后端,应该日常翻看的中文技术网站 1.内容生产者 InfoQ 中文技术第一站,佩服霍老板,真金白银地为中国程序员们生产内容. ImportNew 专门面向Java的内容生产者兼聚合者,偶然也有 ...
- (转) SolrCloud之分布式索引及与Zookeeper的集成
http://blog.csdn.net/ebay/article/details/46549481 作者:Wang, Josh 一.概述 Lucene是一个Java语言编写的利用倒排原理实现的文本检 ...
- css nth-child 的应用
最近改视频监控页面,由于窗口比较多,以前是通过计算窗口大小位置来处理页面布局的,其实还是比较麻烦,而且偶尔会有页面位置错乱的现象,虽然只是及其偶尔的现象,但总归是不好. 计算窗口位置的代码: /*监控 ...
- Split()函数
最近遇到一个有趣的问题关于使用Split函数 ,该函数能够根据传递的参数拆分,并返回一个string的数组. 贴出一个奇怪的例子 using System; using System.Collecti ...
- vue学习笔记(六)— 关于Vuex可以这样简单理解
关于Vuex可以这样简单理解 作者:狐狸家的鱼 本文链接:关于Vuex GitHub:sueRimn 概念理解 和大多数文章都一样,从概念解释到引出问题再举例解决问题. 官网中,Vuex是状态管理模式 ...
- chrome浏览器 快捷键设置
如何设置谷歌浏览器在新窗口中打开链接?如何设置谷歌浏览器在新标签页中打开链接?一.快捷键方式: 1.左键单击 ==> 在当前窗口中打开目标网页. 2.Shift + 左键单击 ==> 在新 ...
- vue.js---利用vue cli脚手架工具+webpack创建项目遇到的坑
1.Eslint js代码规范报错 WARNING Compiled with 2 warnings 10:43:26 ✘ http://eslint.org/docs/rules/quotes St ...