牛客网 拦截导弹 (动态规划-最长不上升子序列+最长上升子序列)

拦截导弹

题目大意:
某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度。某天,雷达捕捉到敌国的导弹来袭。由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹。
输入导弹依次飞来的高度(雷达给出的高度数据是不大于30000的正整数),计算这套系统最多能拦截多少导弹,如果要拦截所有导弹最少要配备多少套这种导弹拦截系统。

解题思路:
第一个就是求最长的不上升子序列的长度。
第二个是最长上升子序列长度

Code:

#include<math.h>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<vector>
#define INF 0x3f3f3f3f
  
using namespace std;
 
 
int a[100005];
int sta1[110000],sta2[110000];
 
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int n=0,len1=1,len2=1;
    while(cin>>a[++n]);
        n--;                                     
        int z,minn,k=1,maxx=-1;
        sta1[1]=a[1];
        sta2[1]=a[1];
        for(int i=2;i<=n;i++){
            if(sta1[len1]>=a[i]) sta1[++len1]=a[i]; //最长不上升子序列
            else{
                int j=lower_bound(sta1+1,sta1+1+len1,a[i],greater<int>())-sta1;
                sta1[j]=a[i];
            }
            if(sta2[len2]<a[i]) sta2[++len2]=a[i];  //最长上升子序列
            else{
                int k=lower_bound(sta2+1,sta2+len2+1,a[i])-sta2;
                sta2[k]=a[i];
            }
             
        }
     
 
        cout<<len1<<endl<<len2<<endl;
     
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值