The longest increment subsequence is one of the most classical problems in dynamic programming, and we start with the discussion of this problem and gradually understand the relevant knowledge points of dynamic programming.
In a known sequence {a1, a 2,... a N} , a number of numbers are taken to form a new sequence {ai1, ai 2,... a im}, where the subscript i1, i2...im keep incrementing, that is, the number of the new series is still in the order of the original sequence, then we call the new sequence {ai1, ai 2,... a im} is a subsequence of the original sequence. If, in the subsequence, the current label IX > iy,aix > aiy, then we call this sub-sequence as an ascending subsequence of the original sequence. The longest increment subsequence problem is to obtain the longest increment subsequence length in a given original sequence.
There are sequences {a1, a 2,... a N}, we ask for the longest increment of the subsequence length. According to the idea of recursive solution, we use f[i] to represent the longest length of an ascending subsequence when the AI ends. When I is small, we can easily derive its value directly, such as f[1] = 1. So how do you push back the value from the F[i] value you've already obtained? Assuming that the values of f[1] to f[x-1] have been determined, note that an incrementing subsequence ending in ax, in addition to the length of 1, in other cases, Ax is immediately after an ascending subsequence consisting of an AI (i<x). Requiring the longest increment subsequence length to end in ax, we compare AX with all of its previous AI (i<x), and if AI is smaller than ax, then Microsoft Dynamics AX can follow the increment subsequence ending with Ai to form a new incrementing sub-sequence. And because the maximum length of the increment subsequence that ends with AI has been obtained, in this case, the length of the longest increment subsequence ending with AI plus the new sequence of Ax is determined, and with the maximum value of all these lengths, we can get the value of f[x]. In particular, when there is no AI (i<x) smaller than ax, the maximum length of the incrementing subsequence ends with AX is 1.
i.e. f[ x] = Max{1, f[I] + 1 | ai < ax & &A mp I < x};
We give all f[i of the longest increment subsequence length of the sequence {1,4,3,2,6,5} for the reader's reference.
F[1] (1) |
F[2] (4) |
F[3] (3) |
F[4] (2) |
F[5] (6) |
F[6] (5) |
1 |
2 |
2 |
2 |
3 |
3 |
To summarize, the recursive formula for the longest increment subsequence is:
F [1] = 1;
F [i] = max{1, F[ J] + 1 | AJ < ai & & J < I};
Next is an application of the following, we through this case, to get a deeper understanding of the next LIS.
A country has developed a missile interception system to defend against enemy missile attacks. But the missile interception system has a flaw: although its first shells can reach any height, each shot cannot be higher than the height of the previous one. One day, the radar caught the enemy missiles to attack. Since the system is still in the trial phase, it is possible to intercept all missiles by using only one set of systems.
Input:
-
-
number of input test data groups in the first row N (1<=n<=10)
The next line is to enter the number of missile m (1<=m<=20) for this set of test data.
The next line is to enter the height of the missile in turn, and all the height values are positive integers greater than 0.
-
-
Output:
-
-
outputs the maximum number of missiles to intercept
-
-
Sample input:
-
-
28389 207 155 300 299 170 158 65388 34 65
-
-
Sample output:
-
62
#include <stdio.h>int max (int a,int b) {return a > B? a:b;}//Take maximum function int list[26]; To preserve the missile height int dp[26] in the order of attack; Dp[i] Save the longest non-increment subsequence length int main () {int n;while ("%d", &n) = EOF) {for (int i = 1;i <= n;i + +) {scanf ("%d") with the first missile end ", &list[i]);} Enter for (int i = 1;i <= n;i + +) {//To determine each dp[i]int Tmax = 1 According to the order of attack and//The initial value of the maximum value is 1, i.e. the longest non-increment subsequence at the end is at least 1for long (int j = 1;j < ; I;j + +) {//traverse all of its former missile heights if (List[j] >= list[i]) {//If the J missile is no lower than the current missile Tmax = max (Tmax,dp[j] + 1);//The current missile is arranged at the longest non-increment sequence ending with the J missile After the column, calculate its length dp[j] + 1, if it is greater than the current maximum value, update Max} } dp[i] = Tmax;//dp[i] to maximum}int ans = 1;for (int i = 1;i <= n;i + +) {ans = Max (Ans,dp[i]);} Finds the maximum value in the longest non-increment subsequence ending with each element, which is the answer printf ("%d\n", ans); Output}return 0;}
The problem of the longest increment subsequence is the first real dynamic programming problem in our contact. Let's review its features. First, we divide this problem into a number of sub-problems, each of which determines the maximum length of an ascending subsequence that ends with the number of I digits. Second, there is a connection between these sub-problems, with the increment of the length of the subsequence ending with any number, related to the length of the longest increment subsequence ending at the end of all elements that are smaller than it before the number, and only with respect to its number, regardless of its specific arrangement.
Dynamic programming of the algorithm (maximum increment subsequence--lis)