码迷,mamicode.com
首页 > 其他好文 > 详细

Coderforces 633D:Fibonacci-ish(map+暴力枚举)

时间:2016-07-18 12:36:29      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

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

D. Fibonacci-ish
 

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.

 

题意:给出 n个数,求出他们任意排列之后最长的斐波那契数列是多长。

思路:这题也是听了师兄的讲解之后才会做的。因为斐波那契数的递增是巨大的,1e9里面的斐波那契数是比较少的,

因此暴力枚举,比较巧妙的是用map来存一个数有没有出现过,并且如果是很多个零的话要加一个特判,不然的话可能会超时。

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <map>
 6 using namespace std;
 7 #define N 1010
 8 
 9 map <int, int> mp;
10 int a[N];
11 int temp[N];
12 
13 int main()
14 {
15     int n ;
16     cin >> n;
17     mp.clear();
18     for(int i = 0; i < n; i++){
19         scanf("%d", a + i);
20         mp[a[i]]++;
21         //该数出现的次数
22     }
23     int ans = 2, now = 0, cnt, x, y, z;
24     for(int i = 0; i < n; i++){
25         for(int j = 0; j < n; j++){
26             if(i == j) continue;
27             x = a[i], y = a[j];
28             if( x == 0 && y == 0 ){
29                 //两个都是0的话,直接判断0的个数,因为0+0=0
30                 if(mp[0] > ans) ans = mp[0];
31                 continue;
32             }
33             cnt = 0;
34             mp[x]--; mp[y]--;
35             temp[cnt++] = x;
36             temp[cnt++] = y;
37             //先把要加的数从map里面删除,避免重复,用temp存放,之后再放回去
38             while(mp[x+y] > 0){
39                 z = x + y;
40                 mp[z]--;
41                 temp[cnt++] = z;
42                 x = y;
43                 y = z;
44             }
45             for(int i = 0; i < cnt; i++)
46                 mp[temp[i]]++;
47             if(cnt > ans) ans = cnt;
48         }
49     }
50     printf("%d\n",ans);
51     return 0;
52 }

 

 

 

Coderforces 633D:Fibonacci-ish(map+暴力枚举)

标签:

原文地址:http://www.cnblogs.com/fightfordream/p/5680105.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!