Dynamically plan Missile interception and plan Missile interception
A: The first shell of a missile interception system can reach any height, but each shell in the future cannot be higher than the previous one. One day, radar capturing
A missile strikes the enemy's country. 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 height of missiles flying in sequence
Measure the test taker's knowledge about how many missiles the system can intercept. How many missile interception systems are required to intercept all missiles?
The first question is very simple, constantly changing the position of the termination point and updating the dp array.
# Include <iostream> # include <cstdio> # include <algorithm> # include <cstring> using namespace std; int dp [1010], a [1010]; int main () {int cases; cin> cases; while (cases --) {memset (dp, 0, sizeof (dp); int n; cin> n; for (int I = 0; I <n; I ++) scanf ("% d", & a [I]); dp [0] = 1; // The minimum subsequence must be 1, and no smaller for (int I = 1; I <n; I ++) for (int j = 0; j <I; j ++) if (a [I] <a [j] & dp [j] + 1> dp [I]) {dp [I] = dp [j] + 1 ;}cout <* max_element (dp, dp + n) <endl ;}}
The second question is difficult.
Let's abstract the question of the second question, that is: divide a series into the longest ascending subsequence with the least number.
Dilworth Theorem
# Include <iostream> # include <cstdio> # include <algorithm> # include <cstring> using namespace std; int dp [1010], a [1010]; int main () {int cases; cin> cases; while (cases --) {int n; cin> n; fill (dp, dp + n, 1 ); for (int I = 0; I <n; I ++) scanf ("% d", & a [I]); dp [0] = 1; // The minimum subsequence must be 1, and no smaller for (int I = 1; I <n; I ++) for (int j = 0; j <I; j ++) if (a [j] <a [I] & dp [j] + 1> dp [I]) {dp [I] = dp [j] + 1 ;}// changes; cout <* max_element (dp, dp + n) <endl ;}}
The idea is to record them from the beginning to the tail, so you can stick them together.