#include <bits/stdc++.h>
using namespace std;
int a[] = {0,1,3,3,5,6,7,8,9,20,21,21,21,30,41,41,41,41,41,45,60};
// 查找第一个大于等于x的位置
int lower_bound(int l, int r, int x) {
while (l <= r) {
int mid = l+r>>1;
if (a[mid] < x) l = mid+1;
else r = mid-1;
}
return l;
}
// 查找第一个大于x的位置
int upper_bound(int l, int r, int x) {
while (l <= r) {
int mid = l+r>>1;
if (a[mid] <= x) l = mid+1;
else r = mid-1;
}
return l;
}
int main() {
cout << lower_bound(1,20,41) << endl;
cout << upper_bound(1,20,41) << endl;
return 0;
}

c++实现lower_bound和upper_bound的更多相关文章

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

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

  2. [STL] lower_bound和upper_bound

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

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

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

  4. STL中的lower_bound和upper_bound的理解

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

  5. STL 源码分析《5》---- lower_bound and upper_bound 详解

    在 STL 库中,关于二分搜索实现了4个函数. bool binary_search (ForwardIterator beg, ForwardIterator end, const T& v ...

  6. lower_bound和upper_bound算法

    参考:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html ForwardIter lower_bound(ForwardIte ...

  7. lower_bound 和 upper_bound

    Return iterator to lower bound Returns an iterator pointing to the first element in the range [first ...

  8. STL源码学习----lower_bound和upper_bound算法[转]

    STL中的每个算法都非常精妙,接下来的几天我想集中学习一下STL中的算法. ForwardIter lower_bound(ForwardIter first, ForwardIter last,co ...

  9. [转] STL源码学习----lower_bound和upper_bound算法

    http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html PS: lower_bound of value 就是最后一个 < ...

  10. STL algorithm算法lower_bound和upper_bound(31)

    lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...

随机推荐

  1. 解决Macbook Pro 2017安装Windows10双系统后在Windows系统中Apple蓝牙鼠标不能使用问题

    MAC BOOK PRO 2017安装Windows10双系统 在Windows系统中蓝牙鼠标不能使用解决办法 最近因工作需要,macOS系统不能要求,大部分工作必须要在Windows系统中进行,最初 ...

  2. centos7在命令行下安装图形界面

    yum groupinstall "GNOME Desktop" "Graphical Administration Tools" ln -sf /lib/sy ...

  3. Selenium常见报错问题(1)- 先来认识下selenium常见异常类

    如果你在跑selenium脚本时,需要某些异常不知道怎么解决时,可以看看这一系列的文章,看看有没有你需要的答案 https://www.cnblogs.com/poloyy/category/1749 ...

  4. Java一个简单的贪吃蛇

    Java一个简单的贪吃蛇 虽然GUI已经要淘汰了,但是手动写写界面还是有助于理解语法的,像构造函数 ,函数调用,内部类,继承,接口.有助于半初学者强化理解. 直接上代码 游戏主体类: package ...

  5. 日志分析工具ELK(三)

    目前官网更新特别快,不到半年时间就更新了好几个版本,目前最新的是5.1 以下安装配置使用4.5版本的 https://www.elastic.co/guide/en/kibana/4.5/index. ...

  6. opencv-6-图像绘制与opencv Line 函数剖析

    opencv-6-图像绘制与opencv Line 函数剖析 opencvc++qt 开始之前 越到后面, 写的越慢, 之前还抽空去看了下 学堂在线那篇文章提供的方法, 博客第一个人评论的我, 想想还 ...

  7. Ali_Cloud++:阿里云服务器部署【禅道】项目管理系统

    1.开源版安装包下载 地址一:百度云下载 10.0  提取码:2dyg  地址二:官方下载 2.直接解压安装包到/opt目录下 注意:这里我安装的是Linux一键安装包官方给出的方法就是直接解压到/o ...

  8. POJ1077 八数码问题

    题目:八数码 网址:http://poj.org/problem?id=1077 在一个3×3的网格中,1~8这8个数字和一个"X"恰好不重不漏地分布在这3×3的网格中. 例如: ...

  9. axios的使用小技巧:如何绕过字符串拼接,直接传递对象

     Vue.js官方推荐使用axios作为发送http请求的工具,在使用axios中,有些小技巧是不容易发现的.当我们不知道这些技巧时,我们可能会使用其他"奇技淫巧",比如,我们很容 ...

  10. SQL SERVER 性能优化二: 数据库初始值大小及增长方式设置

    数据库增长方式主要有两种,按百分比自动增长和按固定大小自动增长,设置初始大小和增长方式需谨慎. 初始大小就是建库的大小,设小了,容易造成磁盘碎片,频繁增长也会影响IO响应.设大了,也不行,设大了,每次 ...