
题目链接
https://www.luogu.org/problemnew/show/P1020
题目描述
某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度。某天,雷达捕捉到敌国的导弹来袭。由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹。
输入导弹依次飞来的高度(雷达给出的高度数据是不大于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=;
const int minn=-;
inline int read() {
char c = getchar();
int x = , f = ;
while(c < '' || c > '') {
if(c == '-') f = -;
c = getchar();
}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int n,m,maxx,ans1,x;
int a[],b[],h[];
int main() {
int i=;
while(cin>>a[i]) {
maxx=;
for(int j=; j<=i-; ++j) {
if(a[j]>=a[i]) {
if(b[j]>maxx) {
maxx=b[j];
}
}
}
b[i]=maxx+;
if(b[i]>m) {
m=b[i];
}
x=;
for(int k=; k<=n; ++k) {
if(h[k]>=a[i]) {
if(x==) {
x=k;
} else if(h[k]<h[x]) {
x=k;
}
}
}
if(x==) {
n++;
x=n;
}
h[x]=a[i];
i++;
}
cout<<m<<'\n'<<n;
return ;
}
[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=;
const int minn=-;
int a[],c[],b[];
struct cmp {
bool operator()(int a,int b) {
return a>b;
}
};
int main() {
int n=;
while(cin>>a[n])n++;
n--;
int con=,cont=;
b[]=c[]=a[];
for(int i=; i<=n; i++) {
if(b[cont]>=a[i])
b[++cont]=a[i];
else
b[upper_bound(b+,b+cont+,a[i],cmp())-b]=a[i];
if(c[con]<a[i])
c[++con]=a[i];
else
c[lower_bound(c+,c+con+,a[i])-c]=a[i];
}
cout<<cont<<" "<<con;
return ;
}