The state dimension is used to classify dynamic planning:
1. The status is one-dimensional.
1.1 downloading/non-Downloading subsequence problems:
Problem description: {the essence of the question is mined. This method can be used to solve the problem once it is abstracted into such a description}
In an unordered sequence A1, A2, A3, A4... An, find the longest sequence satisfying: AI <= AJ <= ak... <= Am, And I <j <K... <M. (maximum non-descending subsequence) or AI> AJ> AK…> Am, And I> j> K…> M. (The longest descent subsequence ).
Problem Analysis:
If a [k] (a [k]> A [I] or a [k] <= A [I]) the longest sequence, plus the number of I a [I], is the longest sequence used in the number of I. So the use of a [k] of the longest sequence requires the number of K-1 ......
From the above analysis, we can see that the problem of Division meets the optimal sub-structure, so does it meet the problem of no aftereffect? Obviously for the number of I only consider the number of I-1 before, obviously meet no aftereffect, can use dynamic programming solution.
Take the longest subnon-descending sequence as an example, for example:
, And 79 are stored in the array input [I;
In addition, we define the maximum number of elements in the ascending subsequence of the Flag [I] array to store subscripts from 0 to I. For example, flag [5] indicates the longest incremental sub-sequence length from input [0] to input [5. Because the flag [0] has only input [0] In the identification interval, the flag [0] = 1. Next, we enter the loop and find that mathematical induction plays an impracticable role in algorithm writing! We assume that the flag [0] To the flag [I-1] has been obtained, and then the flag [I] is obtained. So what is the association between flag [I] and flag [0] to flag [I-1? The following is an example:
Input [0] = 23 --> flag [0] = 1;
Input [1] = 54 --> flag [1] = Flag [0] + 1. The premise here is that input [0] <= input [1];
Input [2] = 16 --> flag [2] = 1;
Input [3] = 43 --> flag [3] = Flag [0] + 1;
Input [4] = 67 --> flag [4] = Flag [1] + 1, provided that input [1] <= input [4];
...
From the above simulation, we can conclude the method of flag [I]: that is, under the premise of input [k] <= input [I], k <I, find the largest flag [k] And set flag [I] = Flag [k] + 1. The Code is as follows:
Flag [0] = 1;
For (Int J = 1; j <n; j ++)
For (int K = 0; k <j; k ++)
{
If (input [k] <= input [J] & flag [J] <flag [k] + 1)
Flag [J] = Flag [k] + 1;
}
Then cyclically traverse the flag [] to find the maximum value that we want to find. The time complexity of this algorithm is O (n ^ 2 ).
The following is an example of code that focuses on ideas and processes:
# Include <iostream>
# Include <string>
Using namespace STD;
# Define max_n 100
Int DP [max_n], s [max_n], path [max_n];
Void output (int K) {// Save the sequence to be searched
If (k =-1) return;
Output (path [k]);
Cout <s [k] <"";
}
Int main (){
Int N;
While (CIN> N ){
Fill_n (DP, n + 1, 1 );
Memset (path,-1, sizeof (PATH); // you can understand the usage of these two functions.
Int I = 0, J, K;
While (I <n)
Cin> S [I ++];
K = 0;
For (I = 1; I <n; I ++ ){
For (j = 0; j <I; j ++ ){
If (s [I]> S [J] & DP [J] + 1> DP [I]) {
DP [I] = DP [J] + 1;
Path [I] = J;
If (DP [k] <DP [I])
K = I;
}
}
}
Cout <"Longest Common subsequence length:" <DP [k] <Endl;
Cout <"The Longest Common subsequence is (one of them):" <Endl;
Output (k );
Cout <Endl;
}
Return 0;
}
Similar questions have another algorithm that is hard to come up. I think this kind of algorithm is similar to the KMP algorithm.
Reference http://hi.baidu.com/zongwobuwang/blog/item/e48745c479072bc6d10060c5.html
If the given data contains non-homogeneous data, the set container of the STL algorithm is attached:
# Include <iostream>
# Include <set>
Using namespace STD;
Int main ()
{
Size_t N;
While (CIN> N)
{
Set <long> last;
For (size_t I = 0; I <n; I ++ ){
Long temp;
Set <long>: iterator index;
Cin> temp;
Last. insert (temp );
Index = last. Find (temp );
If (++ index! = Last. End () Last. Erase (INDEX );
}
Cout <last. Size () <Endl;
}
Return 0;
}
Example:
Missile interception
Source: noip1999 (raise group) Question 1
[Problem description]
A country develops a missile interception system to defend against missile attacks by the enemy. However, this missile interception system has a defect: although its first shell can reach any height, it cannot be higher than the previous one in the future. One day, the radar captured the enemy's missiles. Because the system is still in the trial phase, there is only one system, so it may not be able to intercept all missiles.
Input the heights of missiles flying in sequence (the height data given by the radar is a positive integer not greater than 30000), and calculate the maximum number of missiles the system can intercept, the minimum number of such missile interception systems required to intercept all missiles.
[Input file] missile. In
A single line lists the heights of missiles flying in sequence.
Output file: Missile. Out
The two lines are the maximum number of missiles to intercept, and the minimum number of systems to intercept all missiles
[Input example]
389 207 155 300 299 170 65
[Output example]
6
2
[Problem Analysis]
Experienced players can easily see that this is a problem of finding the longest descent sequence. Obviously, the standard algorithm is dynamic planning.
The design status OPT [I] indicates the number of missiles that can be intercepted by the first one in sequence.
State transition equation:
Flag [0] = 1;
Flag [I] = Flag [k] + 1; (input [I] <input [K], 0 <= k <I)
The largest flag [I] is the final solution.
This only solves the first question. The most intuitive way for the second question is to find out OPT [I] And then remove the missile you just want to fight, after an OPT [I] request is made until all missiles are completed, it is wrong to do so.
It is not difficult to give an example: 6 1 7 3 2
Error solution: 6 3 2/1/7 positive solution: 6 1/7 3 2
In fact, after carefully analyzing the question, we can find that the final result of each missile is to be beaten. If there is a missile later than it, the device that hits it cannot launch the missile anyway. After such an analysis, the problem is abstracted into the problem of finding the longest ascending sequence in the known sequence.
The longest ascending sequence is the same as the longest non-ascending sequence mentioned above.
Complexity: the time complexity is O (n ^ 2), and the space complexity is O (n ).