nyoj 79 missile interception

Source of the topic: http://acm.nyist.net/JudgeOnline/problem.php?pid=79

interceptor missile

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

A certain country is developing a missile interception system in order to defend against enemy missile attacks. But this missile interception system has a flaw: although its first shell can reach any height, each subsequent shell cannot be higher than the height of the previous one. One day, the radar picked up an incoming enemy missile. Since the system is still in the trial phase, only one system is used, so it may not be possible to intercept all missiles.

enter
Enter the number of test data sets N (1<=N<=10) in the first line,
enter the number of missiles m in this set of test data in the next line (1<=m<=20)
, and enter the height of the missiles flying in sequence in the next line. , all height values ​​are positive integers greater than 0.
output
Output the maximum number of missiles that can be intercepted
sample input
2
8
389 207 155 300 299 170 158 65
3
88 34 65
Sample output
6
2
source
[Zhang Jiefeng] Original
Uploaded by
Zhang Jiefeng


In fact, this question has the same meaning as the previous question about nested rectangles, but it's just another way of saying, the code is similar, simple dp

However, this question can be done with LCS. The answer is obtained by finding the length of the longest common subsequence between the input sequence and the Max....1 sequence.

The solution method depends on your own hobbies. For dynamic programming questions, I try to use dp as much as possible. After I get used to it, I can quickly give the dp equation.

# include <stdio.h>
# include <memory.h>
# include <iostream>
using namespace std;
int high[21];
int dp[21];
int main()
{
int N, n, i, j, count;
freopen("in.txt", "r", stdin);
scanf("%d", &N);
while(N--){


scanf("%d", &n);
count = 0;
for(i = 0; i < n; i++){
scanf("%d", &high[i]);
dp[i] = 1;
}
// subproblems are subsequences of the input sequence  
for(i = 0; i < n; i++){
for(j = 0; j < i; j++){
if(high[i] < high[j]) dp[i] = max(dp[i], dp[j] + 1);
}
count = max(dp[i], count);
}

printf("%d\n", count);
}
return 0;
}

Guess you like

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