题目链接

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

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

题目描述

某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度。某天,雷达捕捉到敌国的导弹来袭。由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹。

输入导弹依次飞来的高度(雷达给出的高度数据是不大于50000的正整数),计算这套系统最多能拦截多少导弹,如果要拦截所有导弹最少要配备多少套这种导弹拦截系统。

输入输出格式

输入格式:

一行,若干个整数(个数少于100000)

输出格式:

2行,每行一个整数,第一个数字表示这套系统最多能拦截多少导弹,第二个数字表示如果要拦截所有导弹最少要配备多少套这种导弹拦截系统。

输入输出样例

输入样例#1:389 207 155 300 299 170 158 65

输出样例#1:6 2

 

【n2代码】

思路:第一问求最长非升子序列,第二问贪心

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<string>
#include<cstring>
using namespace std;
const int maxn=999999999;
const int minn=-999999999;
inline int read() {
    char c = getchar();
    int x = 0, f = 1;
    while(c < '0' || c > '9') {
        if(c == '-') f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int n,m,maxx,ans1,x;
int a[10050],b[10050],h[41100];
int main() {
    int i=1;
    while(cin>>a[i]) {
        maxx=0;
        for(int j=1; j<=i-1; ++j) {
            if(a[j]>=a[i]) {
                if(b[j]>maxx) {
                    maxx=b[j];
                }
            }
        }
        b[i]=maxx+1;
        if(b[i]>m) {
            m=b[i];
        }
        x=0;
        for(int k=1; k<=n; ++k) {
            if(h[k]>=a[i]) {
                if(x==0) {
                    x=k;
                } else if(h[k]<h[x]) {
                    x=k;
                }
            }
        }
        if(x==0) {
            n++;
            x=n;
        }
        h[x]=a[i];
        i++;
    }
    cout<<m<<'\n'<<n;
    return 0;
}

[nlogn代码]:

就是第二问求最长上升子序列

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<string>
#include<cstring>
using namespace std;
const int maxn=999999999;
const int minn=-999999999;
int a[100005],c[100005],b[100005];
struct cmp {
    bool operator()(int a,int b) {
        return a>b;
    }
};
int main() {
    int n=1;
    while(cin>>a[n])n++;
    n--;
    int con=1,cont=1;
    b[1]=c[1]=a[1];
    for(int i=2; i<=n; i++) {
        if(b[cont]>=a[i])
            b[++cont]=a[i];
        else
            b[upper_bound(b+1,b+cont+1,a[i],cmp())-b]=a[i];
        if(c[con]<a[i])
            c[++con]=a[i];
        else
            c[lower_bound(c+1,c+con+1,a[i])-c]=a[i];
    }
    cout<<cont<<" "<<con;
    return 0;
}

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄