突然发现lower_bound是一个挺好用的东西,

在学习最长不下降子序列的nlogn的算法的时候看到的,C++党写起二分来一行- -P党一排233

感觉如果到时候需要用上二分的话,能用lower_bound代替真是省事啊...有时间的话,再好好的研究下

这里先简单的mark一下lower_bound的用法;

首先说一下lower_bound是用来求在first和last中的前闭后开区间进行二分查找,返回大于或等于x的第一个元素位置

比如,你现在需要求a[i](i=1,2,3......)的第2个数到第10个数中,大于或等于x的第一个元素的位置;(注意这里是从1开始定义)

因为lower_bound返回的是最小的指针,所以还要-a;

pos=lower_bound(a+2+1,a+10+1,x)-a;

具体的运用可以看BZOJ1609;

另外提一下,upper_bound的用法和lower_bound类似,返回的是指向满足a[i]>k的最小指针(区别在于,不含等号)

我们可以用他们求出有序数组a有多少个相同的k

upper_bound(a,a+n,k)-lower_bound(a,a+n,k)

关于lower_bound的使用的更多相关文章

  1. STL源码学习----lower_bound和upper_bound算法

    转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...

  2. 【刷题记录】 && 【算法杂谈】折半枚举与upper_bound 和 lower_bound

    [什么是upper_bound 和 lower_bound] 简单来说lower_bound就是你给他一个非递减数列[first,last)和x,它给你返回非递减序列[first, last)中的第一 ...

  3. STL之lower_bound和upper_bound

    ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...

  4. UVA 10474 大理石在哪 lower_bound

    题意:找输入的数在排完序之后的位置. 主要是lower_bound 函数的使用.它的作用是查找大于或者等于x的第一个位置. #include<cstdio> #include<alg ...

  5. [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  6. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  7. [STL] lower_bound和upper_bound

    STL中的每个算法都非常精妙, ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一 ...

  8. STL lower_bound upper_bound binary-search

    STL中的二分查找——lower_bound .upper_bound .binary_search 二分查找很简单,原理就不说了.STL中关于二分查找的函数有三个lower_bound .upper ...

  9. vector的插入、lower_bound、upper_bound、equal_range实例

    对于这几个函数的一些实例以便于理解: #include <cstdlib> #include <cstdio> #include <cstring> #includ ...

  10. STL中的lower_bound和upper_bound的理解

    STL迭代器表述范围的时候,习惯用[a, b),所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置. 譬如,值val在容器内的时候,从 ...

随机推荐

  1. 检测ORACLE方法汇总数据块损坏

    1:使用初始化参数 使用初始化参数db_block_checksum\db_block_checking能够设置数据库对块的物理一致性和逻辑一致性检查. Db_block_checksum:物理一致性 ...

  2. 向西项目管理工具Maven一片

    前言 相信仅仅要做过 Java 开发的童鞋们,对 Ant 想必都不陌生,我们往往使用 Ant 来构建项目,尤其是涉及到特别繁杂的工作量.一个 build.xml 可以完毕编译.測试.打包.部署等非常多 ...

  3. Office转HTML

    /// <summary> /// word转成html /// </summary> /// <param name="path"></ ...

  4. auto tool: make -2014-1210-0001

    /* *Author : DavidLin *Date : 2014-12-10pm *Email : linpeng1577@163.com or linpeng1577@gmail.com *wo ...

  5. DM8168 layout

    我们学到了以前的系统板的教训,新的版本号DM8168烤... 一级:电源.DM8168.DDR3.FPGA.CPLD.Nandflash.USB.以太网络.SATA.JTAG等待. 的地面电源部充分. ...

  6. bootstrap collapse MVC .net漂亮的折叠List

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta na ...

  7. javascript and dom1

    <script type="text/javascript"> //数组 var beatles=Array(4); var beatles=Array(); cons ...

  8. RegularExpressionValidator控件

    原文:RegularExpressionValidator控件 ★搜Asp.net★(www.soAsp.net),为专业技术文档网站.包括Asp.net开发技术文档·C#开发技术文档·Access/ ...

  9. Linux下使用Photorec恢复误格U盘

    photorec包含在testdisk软件包中, 所以直接通过包管理器直接安装testdisk安装即可. 使用root权限来运行软件,在终端键入 [shell] photorec [/shell] 然 ...

  10. 显示linux开机时间的脚本

    最初的讨论是linux吧吧友@九十钩圈凯_ 发布的主题贴<加到自启动可以看开机时间的玩意> 并给出显示开机秒数的shell语句 [shell] [ $_UTED = 0 ] || noti ...