Coderforces 633D:Fibonacci-ish(map+暴力枚举)
http://codeforces.com/problemset/problem/633/D
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.
题意:给出 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 }