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以内的 ...
随机推荐
- Python3 文件基本操作
Python文件的打开模式有: r,只读模式(默认).w,只写模式.[不可读:不存在则创建:存在则删除内容:]a,追加模式.[可读: 不存在则创建:存在则只追加内容:]"+" 表示 ...
- Linux下基本栈溢出攻击【转】
转自:http://blog.csdn.net/wangxiaolong_china/article/details/6844415 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[ ...
- python基础===理解Class的一道题
解题如下: from random import randint class Die(): def __init__(self,sides=6): self.sides = sides def rol ...
- 独立服务器远程重装Linux系统
独立服务器远程重装Linux系统 http://rashost.com/blog/remote-reinstall-linux-dedicated-server 本文介绍怎样在没有console连接, ...
- RemoteBox 1.6 发布,VirtualBox 管理工具
RemoteBox 1.6 发布,VirtualBox 管理工具 http://www.lupaworld.com/article-230113-1.html 安装VirtualBox Extensi ...
- 【NOIP2016】补题
今天突然想到自己居然还没把NOIP2016补完 简直是傻逼... 所以开始写 D1T1:模拟 D1T2:NOIP最难的一道题,首先求LCA 离线下,把观察员单独提出来 然后可以维护一个类似桶排序的东西 ...
- 通过file中的字段查询MySQL内容
# -*- coding: utf-8 -*- import MySQLdb with open(r"./user.txt", "r") as f: f.rea ...
- 网站服务器压力Web性能测试(4):服务器压力Web性能测试小结
1.Apache Bench,Webbench,http_load对网站压力Web性能进行测试时,为了得到更加客观和准确的数值,应该从远程访问.局域网访问和本地等多个方面进行全方位的测试.一般用127 ...
- service XXX start启动报start: Rejected send message, 1 matche
转,原文地址:http://blog.sina.com.cn/s/blog_56d8ea9001018w1l.html [问题]start: Rejected send messag现象:crifan ...
- 如何简单解释 MapReduce算法
原文地址:如何简单解释 MapReduce 算法 在Hackbright做导师期间,我被要求向技术背景有限的学生解释MapReduce算法,于是我想出了一个有趣的例子,用以阐释它是如何工作的. 例子 ...