First define the LIS problem: given a sequence of length n, the maximum length of the longest ascending subsequence is obtained.
Method One: (n^2).
F[i] is the length of the longest ascending subsequence ending with I, when and only if J satisfies A[j]<a[i] (1≤j≤i≤n), F[i] is transferred from F[j.
This state transfer equation is easy to construct: f[j] = MAX (F[i]) + 1 (A[j] < a[i]).
Method Two: (n log n)
Now, let's consider the case of calculating f[i] carefully.
Suppose there are two elements a[j1] and a[j2], satisfying (1) 0<j1<j2<i (2) A[j1]<a[j2]<a[i] (3) f[j1]=f[j2]. At this point, select F[j1] and select F[j2] All can get the same f[i] value, then it is clear that the choice of J1 is better than J2 because of the condition (2), in a[x+1] ... a[i-1] in this paragraph, if there is J3, there a[j1] < A[J3] < a[ J2], the longer ascending subsequence is obtained compared to the selection of a[j2].
We will get a revelation: classify according to the value of f[]. For each value K of f[], we only need to preserve the small values in all A[i] that meet f[i] = k. Set G[k] To record this value, i.e. g[k] = Min{a[i]} (f[i] = k).
Note the two features of g[]:
(1) The value of g[k] is monotonous and does not rise throughout the calculation process.
(2) The value of g[] is ordered, i.e. g[1] < g[2] < G[3] < ... < g[n].
Using g[], we can get another method to calculate the length of a long ascending subsequence.
Sets the length of the long ascending subsequence that is currently calculated as Len. First Judge A[i] and G[len], if a[i] > G[len], then A[i] will get a longer ascending subsequence, Len = len + G[len] = 1,d[len+1]? Otherwise, in A[i]. F[len], find the Big J, meet F[j] < a[i]. K = j + 1, then f[j] < A[i] <= F[k], and A[i] after F[j] will get a longer ascending subsequence, and update g[k] = A[i]. After Len is the The length of the long ascending sub-sequence to be obtained. Because of the characteristics of g[] (2), when we look in d[], we can use binary lookup to do it efficiently, the time complexity of the whole algorithm is reduced to O (Nlogn).
Reference: Http://hi.baidu.com/fandywang_jlu/item/da673a3d83e2a65980f1a7e1
Digression: Take a closer look at the classic Interceptor Missile (greedy plus dynamic programming)
Enclosure (Interceptor missile test instructions):
The first projectile of a missile interception system can reach any height, but each projectile cannot be higher than the previous one. One day, the radar caught the enemy's missiles to attack. Since the system is still in the trial phase, there is only one set of systems that may not intercept all missiles. Enter the height of the missile in turn, calculate how many missiles the system can intercept, and how many sets of these missile interceptors should be equipped to intercept all missiles at least?
On Lis of Lis, LCS and LCIs