Manthan, Codefest 16 D. Fibonacci-ish 暴力
D. Fibonacci-ish
题目连接:
http://www.codeforces.com/contest/633/problem/D
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
Sample Output
3
Hint
题意
给你n个数,然后让你随便取一些数出来,使得能够组成的广义fib序列最长
题解:
暴力枚举第一个数和第二个数就好了,然后再暴力往下走,开一个map,记录一下每个数还有多少个
注意回溯。
然后还得注意第一个数和第二个数是0的情况,这样的话,你在暴力枚举的时候,可能复杂度就变成n^3了
所以再开一个map<pair<int,int>,int> 记录一下你枚举了哪些数就好了,这样复杂度就不可能变成n^3了。
当然,你预先处理出第一个数是0,第二个数是0的也可以
其他的fib长度感觉不会超过100吧?
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3+5;
int n;
map<int,int> H;
map<pair<int,int>,int>H2;
int a[maxn];
int deal(int x,int y)
{
if(H[x+y]==0)return 0;
H[x+y]--;
int ans = deal(y,x+y);
H[x+y]++;
return ans+1;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]),H[a[i]]++;
sort(a,a+n);
int ans = 0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)continue;
if(H2[make_pair(a[i],a[j])])continue;
H2[make_pair(a[i],a[j])]=1;
H[a[i]]--;
H[a[j]]--;
ans=max(ans,deal(a[i],a[j]));
H[a[i]]++;
H[a[j]]++;
}
}
cout<<ans+2<<endl;
}
Manthan, Codefest 16 D. Fibonacci-ish 暴力的更多相关文章
- Manthan, Codefest 16 D. Fibonacci-ish(暴力)
题目链接:点击打开链接 题意:给你n个数, 问最长的题目中定义的斐波那契数列. 思路:枚举開始的两个数, 由于最多找90次, 所以能够直接暴力, 用map去重. 注意, 该题卡的时间有点厉害啊. ...
- Manthan, Codefest 16 H. Fibonacci-ish II 大力出奇迹 莫队 线段树 矩阵
H. Fibonacci-ish II 题目连接: http://codeforces.com/contest/633/problem/H Description Yash is finally ti ...
- Manthan, Codefest 16 D. Fibonacci-ish
D. Fibonacci-ish time limit per test 3 seconds memory limit per test 512 megabytes input standard in ...
- Manthan, Codefest 16 -A Ebony and Ivory
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- Manthan, Codefest 16
暴力 A - Ebony and Ivory import java.util.*; import java.io.*; public class Main { public static void ...
- Manthan, Codefest 16 A. Ebony and Ivory 水题
A. Ebony and Ivory 题目连接: http://www.codeforces.com/contest/633/problem/A Description Dante is engage ...
- Manthan, Codefest 16(B--A Trivial Problem)
B. A Trivial Problem time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Manthan, Codefest 16 -C. Spy Syndrome 2
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- CF Manthan, Codefest 16 G. Yash And Trees 线段树+bitset
题目链接:http://codeforces.com/problemset/problem/633/G 大意是一棵树两种操作,第一种是某一节点子树所有值+v,第二种问子树中节点模m出现了多少种m以内的 ...
随机推荐
- Django 1.10中文文档-第一个应用Part5-测试
本教程上接教程Part4. 前面已经建立一个网页投票应用,现在将为它创建一些自动化测试. 自动化测试简介 什么是自动化测试 测试是检查你的代码是否正常运行的行为.测试也分为不同的级别.有些测试可能是用 ...
- Vue组件-使用插槽分发内容
在使用组件时,我们常常要像这样组合它们: <app> <app-header></app-header> <app-footer></app-fo ...
- python基础===isinstance() 函数,判断一个对象是否是一个已知的类型
isinstance(object, classinfo) object -- 实例对象. classinfo -- 可以是直接或间接类名.基本类型或者有它们组成的元组. >>>a ...
- nodejs面试题
1.为什么用Nodejs,它有哪些缺点? 事件驱动,通过闭包很容易实现客户端的生命活期. 不用担心多线程,锁,并行计算的问题 V8引擎速度非常快 对于游戏来说,写一遍游戏逻辑代码,前端后端通用 当然N ...
- 文件上传实现ajax假异步
$.ajaxFileUpload({ url : getSitePath()+'/jlyElderly/doUpdateImage', secureuri : false,//是否需要安全协议 fil ...
- 【linux】su和sudo命令的区别
来源:http://www.jb51.net/LINUXjishu/12713.html 一. 使用 su 命令临时切换用户身份 1.su 的适用条件和威力 su命令就是切换用户的工具,怎么理解呢?比 ...
- .vue,跟小程序文件在sublime里面怎么实现代码格式化
.vue文件跟小程序的.wxml,.wxss用sublime的HTML/CSS/JS prettify插件也可以实现格式化代码的效果 首先你在sublime要已经安装好了HTML/CSS/JS pre ...
- 前后端分离,Vue+restfullframework
一.准备 修改源: npm config set registry https://registry.npm.taobao.org 创建脚手架: vue init webpack Vue项目名称 #I ...
- 微信小程序实战篇-下拉刷新与加载更多
下拉刷新 实现下拉刷新目前能想到的有两种方式 1. 调用系统的API,系统有提供下拉刷新的API接口 2. 监听scroll-view,自定义下拉刷新,还记得scroll-view里面有一个binds ...
- Maximum Subarray——经典
Find the contiguous subarray within an array (containing at least one number) which has the largest ...