【CodeForces - 633D】Fibonacci-ish (离散化,暴力枚举+STPmap,fib数列收敛性质)

题干:

Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if

  1. the sequence consists of at least two elements
  2. f0 and f1 are arbitrary
  3. 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.

Examples

Input

3
1 2 -1

Output

3

Input

5
28 35 7 14 21

Output

4

Note

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.

题目大意:

     定义,fib数列:f[0]、f[1]任意,对于n > 1,f[n] = f[n-1] + f[n-2]。给定n个元素,构造最长的fib数列,输出长度。

解题报告:

     由于斐波那契数列数值成指数型增长(称为斐波那契数列的收敛性),所以实际可选的长度不会超过100(实际上增长速度仅仅比2的幂次方要慢一点点。若是正整数的斐波那契数列,要从0 1增长到1e9只需40位左右,所以题中样例给出的长度最大也是接近90的)。那么只需离散出全部的不同的数值及其个数,枚举前两位即可。有个特例,如果前两位为0,这时可以造一个样例长度可能达到1000,但只会出现一次。。。所以无伤大雅。(好像cf里没有这个样例)

    时间复杂度o(100 * n^2logn)左右。

TLE3代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[2005];
int n;
ll max(ll x,ll y) {if(x > y) return x;return y;
}
ll solve(int p1,int p2) {ll res = 2,tmp = 0;int pos = 1;ll r[3] = {a[p1],a[p2]};while(1) {if(binary_search(a+1,a+n+1,r[0] + r[1]) == 0) break;res++;r[2] = r[0]+r[1];r[0]=r[1];	r[1]=r[2];} return res;
}
int main()
{ll maxx = 0;cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",a+i);sort(a+1,a+n+1);for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if(j == i) continue;maxx = max(maxx,solve(i,j));}}printf("%lld\n",maxx);return 0 ;} 

究其原因,,貌似是因为没有离散化?但是貌似离散化也离散不了哪里去吧,也就是可以下标存下这个元素了,但是查找的时候还是log的复杂度啊。。

 

AC代码:(700ms左右)

离散化后用map存出现次数,但是其实因为都离散化了,就不需要map了啊(因为下标已经是<1000的了),所以直接二分找位置然后开num数组存离散化后值出现的次数就好了呀。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[2005];
int n,len;
map<ll,ll> mp;
ll max(ll x,ll y) {if(x > y) return x;return y;
}
ll solve(int p1,int p2) {ll res = 2,tmp = 0;int pos = 1;ll r[3] = {a[p1],a[p2]};mp[r[0]]--;mp[r[1]]--;while(1) {if(mp[r[0]+r[1]] == 0) break;res++;r[2] = r[0]+r[1];r[0]=r[1];	r[1]=r[2];mp[r[1]]--;//mp[r[0]]--;} mp[r[1]]++;mp[r[0]]++;while(1) {if(r[0] == a[p1] && r[1] == a[p2]) break;r[2] = r[1]-r[0];r[1] = r[0];r[0] = r[2];mp[r[0]]++;}return res;
}int main()
{ll maxx = 0;cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",a+i),mp[a[i]]++;sort(a+1,a+n+1);len = unique(a+1,a+n+1) - a - 1;for(int i = 1; i<=len; i++) {for(int j = 1; j<=len; j++) {if(j == i && mp[a[i]] <= 1) continue;maxx = max(maxx,solve(i,j));}}printf("%lld\n",maxx);return 0 ;} 

另:这题solve函数中完全没有必要两边while,其实一遍就可以,顺便记录下用到了哪些值,最后直接遍历这些值然后num[]++不就好了。

AC代码2:(网上大部分都是给的递归的形式,但是有什么卵用呢、、)

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[2005];
int n,len;
map<ll,ll> mp;
ll max(ll x,ll y) {if(x > y) return x;return y;
}
int f(ll a,ll b) {ll ans=0;if(mp[a+b]) {mp[a+b]--;ans=f(b,a+b)+1;mp[a+b]++;}return ans;
}int main()
{ll maxx = 0;cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",a+i),mp[a[i]]++;sort(a+1,a+n+1);len = unique(a+1,a+n+1) - a - 1;for(int i = 1; i<=len; i++) {for(int j = 1; j<=len; j++) {if(j == i && mp[a[i]] <= 1) continue;mp[a[i]]--;mp[a[j]]--;maxx = max(maxx,f(a[i],a[j]));mp[a[i]]++;mp[a[j]]++;}}printf("%lld\n",maxx+2);return 0 ;} 

 

附一个网络AC代码没用map的:

#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;
void out(T a)
{if(a>9) out(a/10);putchar(a%10+'0');
}
int v[1010],num[1010],stk[1010];//stk长度也要为1000,因为前面两个为0时,len可能为最大值
int main()
{int n;read1(n);rep0(i,0,n) read1(v[i]);sort(v,v + n);int ans = 0,cnt = 0;rep0(i,0,n){int t = i;while(i < n - 1 && v[i] == v[i + 1]) i++;num[cnt] = i - t + 1;v[cnt++] = v[i];//压缩}v[cnt] = 2e9;rep0(i,0,cnt){rep0(j,0,cnt){if(i != j || num[j] > 1){stk[1] = i;stk[2] = j;int len = 2,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-1]];val += v[down];stk[++len] = down;down = lower_bound(v,v+cnt,val) - v;}ans = max(ans,len);rep1(i,1,len) num[stk[i]]++;}}}out(ans);return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441460.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

sql server 迁移 mysql_【转】sql server迁移到mysql

【1】MSSQL2SQLSQL Server转换为MySQL工具&#xff0c;用了一下 感觉蛮不错的。分享上来&#xff0c;同时也以便记录下来以后自用。工具名称&#xff1a;Mss2sql来个操作流程:下载后打开压缩包运行mss2sql默认就是Move to MysQL server directly,选择下一步继续下一步,稍等片刻…

【51Nod - 1268】和为K的组合 (背包 或 dfs)

题干&#xff1a; 给出N个正整数组成的数组A&#xff0c;求能否从中选出若干个&#xff0c;使他们的和为K。如果可以&#xff0c;输出&#xff1a;"Yes"&#xff0c;否则输出"No"。 Input 第1行&#xff1a;2个数N, K, N为数组的长度, K为需要判断的和(…

centos7 mysql tar_CentOS7中mysql-5.7.21-el7-x86_64.tar.gz版MySQL的安装与配置

一、准备阶段通常情况下&#xff0c;MySQL在CentOS下主要使用glibc、rpm、yum等方式进行安装&#xff0c;使用mysql-5.7.21-el7-x86_64.tar.gz包进行安装的很少见&#xff0c;网上资料也较少。通过一上午的摸索&#xff0c;总结出如下安装方法。下载安装包&#xff1a;[rootGee…

【51Nod - 1416】两点 (dfs 或 并查集+dfs)

题干&#xff1a; 福克斯在玩一款手机解迷游戏&#xff0c;这个游戏叫做”两点”。基础级别的时候是在一个nm单元上玩的。像这样&#xff1a; 每一个单元有包含一个有色点。我们将用不同的大写字母来表示不同的颜色。 这个游戏的关键是要找出一个包含同一颜色的环。看上图中4…

linux 源码安装mysql5.7_linux安装mysql5.7.27

一、卸载mysql安装有三种方式&#xff0c;包括二进制包安装(Using Generic Binaries)、RPM包安装、源码安装。一般是前两种比较多二、安装建议路径设置按照写的来将下载的压缩包复制到linux服务器/usr/local/路径下(下载地址https://dev.mysql.com/downloads/mysql/,进去下载默…

【51Nod - 1010 】只包含因子2 3 5的数 (打表,有坑越界)

题干&#xff1a; K的因子中只包含2 3 5。满足条件的前10个数是&#xff1a;2,3,4,5,6,8,9,10,12,15。 所有这样的K组成了一个序列S&#xff0c;现在给出一个数n&#xff0c;求S中 > 给定数的最小的数。 例如&#xff1a;n 13&#xff0c;S中 > 13的最小的数是15&…

c语言可以将负数强制转换成正数吗_C语言笔记(一、概述)

1&#xff0e; C语言的特点 ①语言简洁、紧凑&#xff0c;使用方便、灵活。共有&#xff13;&#xff12;个关键字(也称保留字)&#xff0c;&#xff19;种控制语句。 ②运算符丰富&#xff0c;共有&#xff13;&#xff14;种运算符。 ③数据结构丰富&#xff0c;数据类型有&a…

mysql or全表_mysql or条件可以使用索引而避免全表

在某些情况下&#xff0c;or条件可以避免全表扫描的。1 .where 语句里面如果带有or条件, myisam表能用到索引&#xff0c; innodb不行。1)myisam表&#xff1a;CREATE TABLE IF NOT EXISTS a (id int(1) NOT NULL AUTO_INCREMENT,uid int(11) NOT NULL,aNum char(20) DEFAULT N…

【51Nod - 1163】最高的奖励 (贪心+优先队列 或 妙用并查集)

题干&#xff1a; 有N个任务&#xff0c;每个任务有一个最晚结束时间以及一个对应的奖励。在结束时间之前完成该任务&#xff0c;就可以获得对应的奖励。完成每一个任务所需的时间都是1个单位时间。有时候完成所有任务是不可能的&#xff0c;因为时间上可能会有冲突&#xff0…

mysql varchar java_关于MySQL varchar类型最大值,原来一直都理解错了

写在前面关于MySQL varchar字段类型的最大值计算&#xff0c;也许我们一直都理解错误了&#xff0c;本文从问题出发&#xff0c;经实践验证得出一些实用经验&#xff0c;希望对大家的开发工作有些帮助~背景描述最近同事在做技术方案设计时候&#xff0c;考虑到一个表设计时希望…

【CodeForces - 1027C】Minimum Value Rectangle (数学,公式化简,思维,卡常卡memset)

题干&#xff1a; You have nn sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can …

mysql数据库业务逻辑_Mysql业务设计(逻辑设计)

逻辑设计数据库设计三大范式数据库设计第一大范式数据库表中所有的字段都只具有单一属性单一属性的列是由基本数据类型所构成设计出来的表都是简单的二维表数据库设计的第二大范式要求表中只有一个业务主键&#xff0c;也就是说符合第二范式的表不能存在非主键列&#xff0c;只…

【CodeForces - 632B】Alice, Bob, Two Teams (预处理,思维,前缀和后缀和)

题干&#xff1a; Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi. The way to split up game pieces is split into several steps: First, Alice will split …

mysql一张表1亿天数据_1亿条数据在PHP中实现Mysql数据库分表100张

转&#xff1a;1亿条数据在PHP中实现Mysql数据库分表100张http://php-z.com/thread-2115-1-1.html(出处: PHP-Z)当数据量猛增的时候&#xff0c;大家都会选择库表散列等等方式去优化数据读写速度。笔者做了一个简单的尝试&#xff0c;1亿条数据&#xff0c;分100张表。具体实现…

【POJ - 3273 】Monthly Expense (二分,最小最大值)

题干&#xff1a; Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ moneyi ≤ 10,000) that he will need to spend each day over t…

Mysql8秒级加字段_Mysql8.0秒级加字段

Mysql 8.0版本合并了腾讯互娱数据库团队的Patch&#xff0c;可以实现秒级添加字段&#xff0c;这个功能可谓是mysql数据库攻城狮的福音&#xff0c;解决了之前5.6&#xff0c;5.7版本添加字段很高的运维成本。下面是验证mysql8.0版本秒级添加字段的过程首先用sysbench模拟一张1…

【HDU - 1009 】FatMouse' Trade (贪心)

题干&#xff1a; FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of ca…

lua进入压缩包_使用lua语言制作贪吃蛇游戏(love2d)(一)开发环境的搭建

本教程教大家使用lua制作一个贪吃蛇&#xff0c;游戏引擎使用love2d&#xff0c;因为它开源轻巧而且跨平台。1.开发环境搭建&#xff1a;windows系统&#xff1a;在windows系统下&#xff0c;首先我们进入官网www.love2d.org。love2d官网进入官网可以看到Download选项&#xff…

【CodeForces - 660C】Hard Process (尺取 或 二分+滑窗,前缀和预处理)

题干&#xff1a; You are given an array a with n elements. Each element of a is either 0 or 1. Lets denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to…

mysql rand() 子查询_MySQL ------ 子查询(十三)

查询&#xff08;query&#xff09;:任何SQL 都是查询&#xff0c;但此术语一般指select 语句子查询&#xff08;subquery&#xff09;:嵌套在查询中的查询&#xff0c;MySQL4.1 引入对子查询的支持。接下来得就比较有意思了&#xff0c;需要你对于表与表之间的关系有所了解&am…