这道题有两个问题,第一个好理解,第一个数字表示这套系统最多能拦截多少导弹,由于只能拦截的高度不能高于前面的,所以是最长不上升子序列。第二个问题我理解了很长时间,第二个数字表示如果要拦截所有导弹最少要配备多少套这种导弹拦截系统。最少有几个最长不上升子序列转化成最多有几个最长上升子序列。
本题我还有一个不明白的其实是,区分不了lower与upper,只能先记下来。
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int f1[maxn],f2[maxn],a[maxn],n;
int main(){
while(cin>>a[++n]);n--;
memset(f1,0x3f3f3f3f,sizeof(f1));
int len1=0,len2=0;
for(int i=1;i<=n;i++){
if(a[i]<=f1[len1]) f1[++len1]=a[i];
else{
int p1=upper_bound(f1+1,f1+1+len1,a[i],greater<int>())-f1;
f1[p1]=a[i];
}
if(a[i]>f2[len2])f2[++len2]=a[i];
else{
int p2=lower_bound(f2+1,f2+1+len2,a[i])-f2;
f2[p2]=a[i];
}
}
cout<<len1<<endl<<len2;
return 0;
}