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维数组记录情况. ...
随机推荐
- .net core 1.1.0 MVC 控制器接收Json字串 (JObject对象) (二)
.net core 1.1.0 MVC 控制器接收Json字串 (JObject对象) (二) .net core 1.1.0 MVC 控制器接收Json字串 (JObject对象) (一) 上一篇主 ...
- 如何打开“USB调试”模式?
请首先确认您的系统版本, 点击「设置」-「关于手机」查看您当前的手机版本号. 如果您使用的是 Android 3.2及以下系统,请按以下步骤操作: STEP1:在应用列表选择「设置」进入系统设置菜单: ...
- Java 如何连接 SQL 2008 R2
前提: 1. 复制sqljdbc_auth.dll到C:\Windows\System32这个路径下. 2. 添加sqljdbc4.jar到Libraries里面. 代码示例: import java ...
- Android_Handler
xml文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...
- 写实例学习html5 WebSocket
WebSocket简介 WebSocket是html5的重要特性.它是HTML5一种新的协议,实现了浏览器与服务器全双工通信(full-duplex).使服务器可以主动传送数据给客户端,对构建实时w ...
- 基于Selenium2+Java的UI自动化(3) - 页面元素定位
一.几种网页定位方式 webdriver的页面定位很灵活,提供了8种定位方式: 其中,常见的有三种:id .cssSelector .xpath: 一个元素如果存在 id 属性,则这个 id 的值,在 ...
- 268条PCB Layout设计规范
1 PCB布线与布局 PCB布线与布局隔离准则:强弱电流隔离.大小电压隔离,高低频率隔离.输入输出隔离.数字模拟隔离.输en入输出隔离,分界标准为相差一个数量级.隔离方法包括:空间远离.地线隔开. 2 ...
- dedecms 首页分页功能
1.需要引入 <script language="javascript" type="text/javascript" src="{dede:g ...
- Java 简单算法--打印乘法口诀(只使用一次循环)
package cn.magicdu.algorithm; /** * 九九乘法口诀表 * * @author xiaoduc * */ public class NineNineMulitTable ...
- VMWare中安装CentOS6.6不能上网的解决办法
1.首先在虚拟机中将网络配置设置成NAT 2.在windows系统,我的电脑-管理-服务 中开启VMware NAT service和VMware DHCP service. 3.在CentOS里面打 ...