CF 633 D 离散化+二分查找+暴力(还是要多学习学习呀,挺好的题目)

http://codeforces.com/contest/633/problem/D

D. Fibonacci-ish
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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  - 121, the whole sequence ai would be Fibonacci-ish.

In the second sample, the optimal way to rearrange elements is 28.


题目大意:

给一个数列。根据斐波那契数列的定义,得出最长的子序列。


思路:

因为是斐波那契数列,所以说如果前两项知道了,那么后面的数列就排好了。题解上面还有写到,因为斐波那契数列是接近于2的幂的,所以说最长的子序列是90.但是可能存在全都是0的子序列。所以要特别注意一下。

这道题想了好久还是不会做就看了一下别人的题解。

主要的方法是如下,首先,因为前两项是不知道的,所以要枚举所有的可能性,那就是n^2。然后因为要通过lower_bound来寻找函数(表示这点想到了,但是之前自己写的时候不知道为什么TLE了),所以说之前要排序。然后就是离散化了,将状态进行压缩,用b数组来记录一下a数组中重复的值。

还有一点就是用pos[]数组来保存值。


感觉这道题学到了一下几点

①可以用pos数组来储存下标。表示以前都是用pos来储存的,没怎么用过数组。

②离散化。减少重复的搜索和代码的长度,优化了。

③大牛代码中的思考条件都十分的全面,尤其是if中,一定要好好学习。

④最后别忘了使用过一次以后都加回来。

下面给出临摹的代码:(本人现在也只有这水平了。。。不过还是有点不一样的啦)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值