题目:有一个导弹拦截系统,当它拦截一个导弹后,下次拦截就不能超过这个高度,问拦截的最多导弹数。
分析:dp、最大不下降子序列。设f(i)为以第i个导弹为最后拦截目标时,能拦截导弹的最大数量,则有:
f(i) = max{ f(j)+1 },( j < i 且只有当第j的高度小于i时才成立)
说明:注意输出第二行前面的两个空格╮(╯▽╰)╭。
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
int h[10001];
int f[10001];
int main()
{
int tests = 1;
while ( scanf("%d",&h[0]) && h[0] != -1 ) {
int count = 1;
while ( scanf("%d",&h[count]) && h[count] != -1 )
count ++;
int max = 1;
for ( int i = 0 ; i < count ; ++ i ) {
f[i] = 1;
for ( int j = 0 ; j < i ; ++ j )
if ( h[j] >= h[i] && f[j] >= f[i] )
f[i] = f[j]+1;
if ( max < f[i] ) max = f[i];
}
if ( tests > 1 ) printf("\n");
printf("Test #%d:\n",tests ++);
printf(" maximum possible interceptions: %d\n",max);
}
return 0;
}