P1020 导弹拦截

P1020 导弹拦截

  链接:https://www.luogu.org/problemnew/show/P1020

  题意:某导弹拦截系统,它每次所拦截的导弹高度均不能超过前一次所拦截的高度(第一次可以达到任意高度),求该系统最多能拦截几枚导弹以及最少需要多少个这样的系统才能拦截所有的导弹。

  思路:最长不上升子序列+最长上升子序列

     最长不上升子序列用来求该系统最多能拦截几枚导弹,最长上升子序列用来求需要几个系统才能拦截所有的导弹。

     为什么会是最长上升子序列?我打个比方,突然有一个导弹的高度大于你当前的拦截最大高度,你肯定拦截不了,所以你肯定需要再来一个系统才能拦截下来。所以只需求最长上升子序列的长度即是需要的系统数量。

  代码:(写的比较丑。。。。)

  

#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
int n,num;
int a[maxn];
int dp1[maxn];
int dp2[maxn];
struct cmp{
    bool operator()(int a,int b){return a>b;}
};
int main()
{
    n=1;
    while(cin>>a[n])n++;
    n--;
    if(n==0)
    {
        cout<<0<<endl<<0<<endl;
        return 0;
    }
    dp1[1]=a[1];
    dp2[1]=a[1];
    int len1=1,len2=1;
    for(int i=2;i<=n;i++)
    {
        if(a[i]<=dp1[len1])dp1[++len1]=a[i];
        else
        {
            int j=upper_bound(dp1+1,dp1+len1+1,a[i],cmp())-dp1;
            dp1[j]=a[i];
        }
        if(a[i]>dp2[len2])dp2[++len2]=a[i];
        else
        {
            int j=lower_bound(dp2+1,dp2+len2+1,a[i])-dp2;
            dp2[j]=a[i];
        }
    }
    cout<<len1<<endl<<len2<<endl;
    return 0;
}

 

posted @ 2019-06-20 13:35  杯酒朝阳  阅读(302)  评论(0编辑  收藏  举报