如果你用了 STL 的二分查找函数,而不是手写,一定注意 lower _ bound 和upper _ bound 的区别。
求最长不下降子序列时,应该用 upper _ bound
for(int i=n-1;i>=1;i--)
{
if(a[i]>=b[mx])b[++mx]=a[i];//注意等号
else
{
int j=upper_bound(b+1,b+mx+1,a[i])-b;
b[j]=a[i];
}
}
求导弹系统个数,应用 lower _ bound
int j=lower_bound(jg+1,jg+cnt+1,x)-jg;
if(j>cnt)
{
cnt++;
jg[cnt]=x;
}
else jg[j]=x;