[Title]:
If for all i = 3,4,.., n, there is AI = ai-1+ ai-2, then the integer sequence A1,a2,..., An is called the Fibonacci sequence.
Given an integer sequence c1, c2, ..., CM, you need to find the longest Fibonacci subsequence in this sequence (note that the subsequence cannot change the order of the integer sequence given).
- Input
- The first line of the input data contains an integer m (1 <= m <= 3,000). The following line has m integers, and the absolute value of these integers does not exceed 10^9.
- Output
- Outputs only an integer that represents the length of the longest Fibonacci subsequence in the sequence given by the input data.
- Sample input
101 1 3-1 2 0 5-1-1 8
- Sample output
5
[idea] Any Fibonacci sequence that determines the two consecutive entries in the sequence determines the length of all the items in the series. Set Array Dp[i][j] indicates that the number of subscript I and J is the last two of an aerial seeding sequence, then there is a state transfer equation dp[j][k] = max (dp[j][k], dp[i][j]+1) when A[i] + a[j] = A[k ] and K > J. A number can appear multiple times, we can work out the value of a[k], but we do not know where a[k] appears, and if we search all locations greater than J, the complexity increases instead. So discretization after the binary search, discretization, the number of the largest is 3000 can be stored in a vector array, storing the current number of all locations. Then each time the two points are searched. The point at which the position of the sieve sequence is greater than the current J is transferred.
[Code June]
1#include <iostream>2#include <algorithm>3#include <vector>4#include <string.h>5 using namespacestd;6 Const intMAXN =3010;7 intDP[MAXN][MAXN];8 intA[MAXN];9 intCHECK[MAXN];Ten intENCODE[MAXN]; One intN; A intkey; -vector<int>NUM[MAXN]; - intBinintaim) the { - intL =1, R = key-1; - intmid; - while(L <=R) + { -Mid = (L + r)/2; + if(AIM = = Encode[mid]) Break; A Else if(AIM = = Encode[l]) {mid = L; Break;} at Else if(AIM = = Encode[r]) {mid = R; Break;} - Else if(Encode[mid] > Aim) r = Mid-1; - Else if(Encode[mid] < aim) L = mid +1; - } - returnL <= r? Mid:-1; - } in - intSlove () to { +Memset (DP,0, MAXN * MAXN *sizeof(int)); - for(inti =1; I <= N; i++) {cin >> a[i]; Check[i] =a[i];} the if(n = =1)return 1; * if(n = =2)return 2; $Sort (check +1, check +1+n);Panax Notoginseng intPre = check[1]; -encode[1] = check[1]; the for(inti =1; I <= N; i++)if(A[i] = = check[1]) num[1].push_back (i); +Key =2; A for(inti =2; I <= N; i++) the { + if(Check[i] = = Pre)Continue; - for(intj =1; J <= N; J + +)if(A[j] = =Check[i]) Num[key].push_back (j); $encode[key++] =Check[i]; $Pre =Check[i]; - } - intAns =0; the for(inti =1; I < n; i++) - {Wuyi for(intj = i +1; J <= N; J + +) the { - intNext = A[i] +A[j]; Wu - intpos =bin (next); About $ if(Pos <0)Continue; - Else - { - A for(intK =0; K < Num[pos].size (); k++) + { the - inttemp =Num[pos][k]; $ if(Temp <= j)Continue; theDp[j][temp] = max (Dp[j][temp],dp[i][j] +1); theAns =Max (ans, dp[j][temp]); the } the } - } in } the the About returnAns +2; the } the the intMain () + { - while(Cin >>N) the {Bayi intAns =slove (); thecout << ans <<Endl; the } - return 0; -}
Maximum Fibonacci sequence selection (discretization + binary + DP)