题目
https://www.luogu.com.cn/problem/P1020
AC代码
#include<bits/stdc++.h>
using namespace std;
bool cmp(int x,int y){
return x>y;
}
int main()
{
int a[100005],b[100005]={-1};
int n=0,pos1=0,pos2=0;
while(cin>>a[n++])
{
if(getchar()=='\n') break;
}
n--;
for(int i=0;i<n;i++)
{
if(a[i]<=b[pos1]) b[++pos1]=a[i];
else b[upper_bound(b,b+pos1,a[i],cmp)-b]=a[i];
}
fill(b,b+100005,0);
b[0]=-1;
for(int i=0;i<n;i++)
{
if(a[i]>b[pos2]) b[++pos2]=a[i];
else b[lower_bound(b,b+pos2,a[i])-b]=a[i];
}
cout<<pos1+1<<endl<<pos2;
}
参考
题解 P2757 【导弹的召唤(数据加强)】
题解 P1020 【导弹拦截】
关于lower_bound( )和upper_bound( )的常见用法
注意
n--;
必须要有,因为在判断时会有一次多余的输入
注意判断的符号,每一发炮弹都不能高于前一发的高度,所以是<=,第二问就是正好相反的>
这道题最好自己找个例子推导一下,便于理解,我给大家一个例子供参考
例子如下:
输入:
90 103 99 83 102 70 86 70 99 71
输出:
5
3