NYOJ 79 intercepts missiles and nyoj79 intercepts missiles
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 or equal to the height of the previous one. One day, the radar captured the enemy's missiles. Because the system is still in the trial phase, only one system is used, so it may not be able to intercept all missiles.
-
Input
-
Number of input test data groups in the first line N (1 <= N <= 10)
In the next line, enter the total number of missiles in this set of test data (1 <= m <= 20)
Next, enter the height of the missile flying in sequence. All the height values are positive integers greater than 0.
-
Output
-
Maximum number of missiles that can be intercepted
-
Sample Input
-
28389 207 155 300 299 170 158 65388 34 65
-
Sample output
-
62
The data for this question is relatively small. Each group has a maximum of 20 data records. You can use dfs without timeout. However, I feel it is more time-saving to use dynamic rules and better code writing.
When I handed in for the first time, it was wrong because I didn't see that it could not be equal to the previous one. 1 # include <iostream>
2 # include <cmath>
3 # include <algorithm>
4 using namespace std;
5
6 int main ()
7 {
8 int n, m, a [25], B [25]; // a array record height, B array record the Maximum descending sequence ending with the number of I
9 cin> n;
10 while (n --)
11 {
12 cin> m;
13 for (int I = 1; I <= m; I ++) // input data
14 {
15 cin> a [I];
16 B [I] = 1; // by default, the Maximum descending order ending with each number is 1, which will be updated later.
17}
18 int max = 0;
19 for (int I = 2; I <= m; I ++)
20 {
21 for (int j = 1; j <I; j ++)
22 {
23 if (a [I] <a [j]) // if the number of I is smaller than the number of j in front of it, judge and take a big
24 {
25 if (B [I] <B [j] + 1)
26 B [I] = B [j] + 1;
27}
28}
29 if (B [I]> max)
30 max = B [I];
31}
32 cout <max <endl;
33}
34 return 0;
35} View Code