nyoj 814 see also interceptor missile

see also interceptor missile

Time Limit: 3000 ms | Memory Limit: 65535 KB
Difficulty: 3
 
describe

Everyone should be familiar with the topic of intercepting missiles. Let me describe the meaning of the topic: a certain country has developed a new missile interception system in order to defend against missile attacks by enemy countries. But this missile interception system has a flaw: its first shell can reach any height, but each subsequent shell cannot exceed the height of the previous one. Suddenly one day, the radar picked up the incoming missile from the enemy country. Due to the defects of this system, if you want to intercept all the missiles, you must prepare several more sets of such missile interception systems. However, since the cost of this system is too high, in order to reduce the cost, please calculate the minimum number of interception systems required.

 
enter
There are multiple sets of test data.
Enter an integer N (N≤3000) for each set of data, which means that there are N missiles attacking. Next, there are N numbers, which represent the height of the missiles of the missiles that fly in sequence. When N=-1, it means the end of input.
output
Each set of output data occupies one line, indicating the minimum number of interception systems required.
sample input
8
389 207 155 300 299 170 158 65
5
265 156 123 76 26
Sample output
2
1
1  /* *
 2      Analysis:
 3          Ⅰ. We need to record the maximum distance of the added missile defense system in real time
 4          Ⅱ, traverse the system to find out the missile defense system that meets the conditions and update its maximum distance
 5              Ⅱ ( ②) If all anti -missile systems If the missile does not meet the conditions, it will rejoin a system at the distance just entered.
 6              
7      core code:
 8          for (i = 1; i < k; ++ i) {
 9              if (A [i] >= num) {
 10                  A[i] = num;
 11                  break;
 12              }
 13          } 
 14          if (i == k) {
 15              A[k ++] = num;
 16          }
 17  * */

C/C++ code implementation (AC):

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 int n;
 6 
 7 int main () {
 8     while (~scanf ("%d", &n), n != -1) {
 9         int A [3005], k = 1, i, num;
10         while (n --) {
11             scanf ("%d", &num);
12             for (i = 1; i < k; ++ i) {
13                 if (A [i] >= num) {
14                     A [i] = num;
15                     break;
16                 }
17             }
18             if (i == k) {
19                 A [k ++] = num;
20             }
21         }
22         printf ("%d\n", k - 1);
23     }
24 } 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325198876&siteId=291194637