lower_bound的小技巧

契机

在做导弹拦截这题的时候发现自己不太会用lower_bound和upper_bound,所以写一篇文章备忘。

常规用法

1
2
3
//以下都需要在不下降序列中用
lower_bound(f,f+n,a[i]); //返回f中大于等于a[i]的第一个数
upper_bound(f,f+n,a[i]); //返回f中大于a[i]的第一个数

在不上升序列中用

1
2
3
lower_bound(f,f+n,a[i],greater<int>()); //返回f中大于等于a[i]的第一个数
upper_bound(f,f+n,a[i],greater<int>()); //返回f中大于a[i]的第一个数
//当然greater也可以换成自己写的比较函数
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022-2024 CPY
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信