codeforces 633D - 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.
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).
Print the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement.
3
1 2 -1
3
5
28 35 7 14 21
4
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.
思路:由于斐波那契数列数值成指数型增长(称为斐波那契数列的收敛性),所以实际可选的长度不会超过100(实际上增长速度比2的幂次方要慢,若是正整数的斐波那契数列,要从0 1增长到1e9只需40位左右,所以题解给出的长度最大也是接近90的);那么只需离散出全部的不同的数值及其个数,枚举前两位即可;特例是前两位为0,这时长度可能达到1000,但只会出现一次;
实现细节:用stk记录下路径上的序号,为了方便每次枚举时,初始化每个数的个数;同时也可递推出当前两位的和;
没使用map,使用map的话,插入和查询都是log(n),总时间复杂度为O(n^2*log(n)*len)len为每次搜索的长度,且常数较大;用数组模拟,时间复杂度中少了log(n)与map带来的额外的常数,只需46ms 0k
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
#define MSi(a) memset(a,0x3f,sizeof(a))
#define inf 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m+1, r, rt << 1|1
typedef __int64 ll;
template<typename T>
void read1(T &m)
{
T x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
m = x*f;
}
template<typename T>
void read2(T &a,T &b){read1(a);read1(b);}
template<typename T>
void read3(T &a,T &b,T &c){read1(a);read1(b);read1(c);}
template<typename T>
void out(T a)
{
if(a>) out(a/);
putchar(a%+'');
}
int v[],num[],stk[];//stk长度也要为1000,因为前面两个为0时,len可能为最大值
int main()
{
int n;
read1(n);
rep0(i,,n) read1(v[i]);
sort(v,v + n);
int ans = ,cnt = ;
rep0(i,,n){
int t = i;
while(i < n - && v[i] == v[i + ]) i++;
num[cnt] = i - t + ;
v[cnt++] = v[i];//压缩
}
v[cnt] = 2e9;
rep0(i,,cnt){
rep0(j,,cnt){
if(i != j || num[j] > ){
stk[] = i;stk[] = j;
int len = ,val = v[i] + v[j];
num[i]--,num[j]--;
int down = lower_bound(v,v+cnt,val) - v;
while(v[down] == val && num[down]){
num[down]--;
val -= v[stk[len-]];
val += v[down];
stk[++len] = down;
down = lower_bound(v,v+cnt,val) - v;
}
ans = max(ans,len);
rep1(i,,len) num[stk[i]]++;
}
}
}
out(ans);
return ;
}
codeforces 633D - Fibonacci-ish 离散化 + 二分查询的更多相关文章
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
- [Codeforces 1199C]MP3(离散化+二分答案)
[Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...
- C# 二分查询
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- POJ2391 Floyd+离散化+二分+DINIC
题意: 有n个猪圈,每个猪圈里面都有一定数量的猪(可能大于当前猪圈的数量),每个猪圈都有自己的容量,猪圈与猪圈之间给出了距离,然后突然下雨了,问多久之后所有的猪都能进圈. 思路: ...
- Codeforces Round #521 (Div. 3) E. Thematic Contests (离散化,二分)
题意:有\(n\)个话题,每次都必须选取不同的话题,且话题数必须是上次的两倍,第一次的话题数可以任意,问最多能选取多少话题数. 题解:我们首先用桶来记录不同话题的数量,因为只要求话题的数量,与话题是多 ...
- [Codeforces]817F. MEX Queries 离散化+线段树维护
[Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You sho ...
- codeforces 749D Leaving Auction(二分)
题目链接:http://codeforces.com/problemset/problem/749/D 题意:就是类似竞拍,然后报价肯定要比上一个高,然后查询输入k个数表示那些人的竞拍无效, 输出最后 ...
- 2019 Multi-University Training Contest 3 Find the answer (离散化+二分+树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6609 题目大意:给定一个含有n个数的序列,还有一个m,对于每个i(1<=i<=n)求出最少 ...
- Codeforces 55D (数位DP+离散化+数论)
题目链接: http://poj.org/problem?id=2117 题目大意:统计一个范围内数的个数,要求该数能被各位上的数整除.范围2^64. 解题思路: 一开始SB地开了10维数组记录情况. ...
随机推荐
- 文件I/O(不带缓冲)之lseek函数
每个打开的文件都有一个与其相关联的“当前文件偏移量”(current file offset).它通常是一个非负整数,用以度量从文件开始处计算的字节数.通常,读.写操作都从当前文件偏移量处开始,并使偏 ...
- JavaScript网站设计实践(一)网站结构以及页面效果设计
这是JavaScript DOM编程艺术里的构建JavaScript网站设计的例子,这本书给我学习JavaScript带来许多启发,在这个乐队宣传网站中,把前面学到的知识点整合在这个项目了.在这里记录 ...
- scrollLeft、offsetLeft、clientLeft、clientHeight详解
offsetLeft:Html元素相对于自己的offsetParent元素的位置 scrollLeft:返回和设置当前横向滚动务的坐标值 scrollLeft.offsetLeft.clientLef ...
- A XSS filter for Java EE web apps--转载
原文地址:http://java.dzone.com/articles/xss-filter-java-ee-web-apps Cross Site Scripting, or XSS, is a f ...
- ajax的一部分知识
jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(p ...
- iOS 导航控制器如何随意push和pop 想要在 A push B 后, B 在push 到 D ,然后从 D pop 到 C ,在从 C pop 的A
这里主要是对导航控制器的viewControllerss这个数组进行操作,因为push操作和pop操作都是根据这个数据去切换控制器或者在这个数组里增加控制器的,所以只要改变这个子控制器数据就能自定义切 ...
- UILabel 自动换行 和支持换行符
这个主要是 lable对\n换行符号的支持,有的时候我们从网页或者后台拿到的数据需要处理一下: 这里没什么要说的,注意两点,一个是label的numofline属性的值要为0 或者不能为1 这样la ...
- js运动
一.offsetWidth / offsetHeight 获取整个块的宽度/高度,包括border 二.clientWidth / clientHeight 获取块的宽度/高度,不包括border 三 ...
- Hive体系结构介绍
http://www.aboutyun.com/thread-6217-1-1.html 1.Hive架构与基本组成 下面是Hive的架构图. 图1.1 Hive体系结构 Hive ...
- Android IntentService使用
因为多数启动服务不必同时处理多个请求(在多线程情景下会很危险),所以使用IntentService类实现服务是很好的选择.本经验将通过继承IntentService输出当前时间教大家如何使用Inten ...