012:拦截导弹

描述某国为了防御敌国的导弹袭击,开发出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度。某天,雷达捕捉到敌国的导弹来袭,并观测到导弹依次飞来的高度,请计算这套系统最多能拦截多少导弹。拦截来袭导弹时,必须按来袭导弹袭击的时间顺序,不允许先拦截后面的导弹,再拦截前面的导弹。输入输入有两行,
第一行,输入雷达捕捉到的敌国导弹的数量k(k<=25),
第二行,输入k个正整数,表示k枚导弹的高度,按来袭导弹的袭击时间顺序给出,以空格分隔。
输出输出只有一行,包含一个整数,表示最多能拦截多少枚导弹。
样例输入

8
300 207 155 300 299 170 158 65

样例输出

6

来源医学部计算概论2006期末考试题

AC:

将问题分解为规模更小的子问题进行求解。对于一个序列,就是降低序列的长度,“掐头去尾切两半”

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<map>
#include<cstring>
#define DEBUG(x) cout << #x << " = " << x << endl
using namespace std;
const int MAXN=30;
int K;
int height[MAXN];
int block(int n,int h)
{
    if(n==K)return 0;
    int n1=-1,n2=-1;
    if(h<height[n]){
        n1=block(n+1,h);
    }
    else {
        n1=block(n+1,h);
        n2=block(n+1,height[n])+1;
    }
    return max(n1,n2);
}
int main()
{
//    freopen("in.txt","r",stdin);
    scanf("%d",&K);
    for(int i=0;i<K;i++){
        scanf("%d",&height[i]);
    }
    cout<<block(0,1<<30);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/MalcolmMeng/p/9135057.html