最近一直在学dp,但是感觉进度明显慢了很多,希望自己可以加一把劲,不要总是拖延了...

  在LIS的优化中我遇到了二分查找的问题,之前也知道lower_bound和upper_bound两个函数,但是没有做一个具体的总结,在下面我会总结这两个函数的用法,也会给出这两个函数的实现代码,代码是参考c ++ Reference 里面的...

lower_bound:

  这个函数的头文件为#include <algorithm>,函数的返回值为一个指向单调序列[first, last) 中第一个不小于val的元素的地址,如果不存在满足条件的

元素则返回NULL。你可以用该函数得到的指针的值减去数组开头元素的地址得到他在单调序列中的位置。

  下面解释一下该函数的几个参数:lower_bound(first, last, val);

  表示在有序序列[first, lase) 的所有值中,第一个不小于val的元素的地址。

  下面给出我自己对于lower_bound优化后实现的代码:有错误还请多多指出...

  该代码并没有返回指针,而是直接返回了下标......

 int lower_bound(vector<int> &a, int val) {
int first = , last = a.size() - , mid;
while(first <= last) {
mid = last - (last - first) / ;
if(a[mid] >= val) last = mid - ;
else first = mid + ;
}
return first;
}

upper_bound

  这个函数和上面的函数内容只有一点不同,他返回的是单调序列中第一个大于val的元素的地址,如果不存在满足条件的元素则返回NULL,下面再给出我自己写的版本......

 int upper_bound(vector<int> &a, int val) {
int first = , last = a.size() - , mid;
while(first <= last) {
mid = last - (last - first) / ;
if(a[mid] <= val) first = mid + ;
else last = mid - ;
}
return first;
}

lower_bound和upper_bound的实现和基本用法的更多相关文章

  1. 关于lower_bound( )和upper_bound( )的常见用法

    lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begin,end,num):从数 ...

  2. lower_bound( )和upper_bound( )的常见用法

    lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begin,end,num):从数 ...

  3. lower_bound()和upper_bound()用法详解

    lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. lower_bound( begin,end,num):从数组的begin位置到end ...

  4. 【模板】关于vector的lower_bound和upper_bound以及vector基本用法 STL

    关于lower_bound和upper_bound 共同点 函数组成: 一个数组元素的地址(或者数组名来表示这个数组的首地址,用来表示这个数组的开头比较的元素的地址,不一定要是首地址,只是用于比较的& ...

  5. STL之std::set、std::map的lower_bound和upper_bound函数使用说明

    由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊 ...

  6. C++ STL lower_bound()和upper_bound()

    lower_bound()和upper_bound()用法 1.在数组上的用法 假设a是一个递增数组,n是数组长度,则 lower_bound(a, a+n, x):返回数组a[0]~a[n-1]中, ...

  7. 关于lower_bound()和upper_bound()

    关于lower_bound()和upper_bound(): 参考:关于lower_bound( )和upper_bound( )的常见用法 注意:查找的数组必须要是排好序的.因为,它们查找的方式也是 ...

  8. STL:map中的lower_bound和upper_bound

    今天在做leetcode的Longest Increasing Subsequence题目时,需要用到二分查找,于是翻看了<STL源码剖析>这本书,发现map里面有lower_bound和 ...

  9. C++二分查找:lower_bound( )和upper_bound( )

    #include<algorithm>//头文件 //标准形式 lower_bound(int* first,int* last,val); upper_bound(int* first, ...

随机推荐

  1. ansible常用模块即用法

    Linux 中,我们可以通过 ansible-doc -l 命令查看到当前 ansible 都支持哪些模块,通过 ansible-doc  -s  模块名  又可以查看该模块有哪些参数可以使用. 下面 ...

  2. linux获取线程ID

    pthread_self()获取当选线程的ID.这个ID与pthread_create的第一个参数返回的相同.但是与ps命令看到的不同,因此只能用于程序内部,用于对线程进行操作. #include & ...

  3. java 父类引用指向子类对象---动态绑定

    知识点: 1.java 中父类引用指向子类对象时动态绑定针对的只是子类重写的成员方法: 2.父类引用指向子类对象时,子类如果重写了父类的可重写方法(非private.非 final 方法),那么这个对 ...

  4. cxVerticalGrid赋值是实时更新

    procedure TForm1.cxVerticalGrid1Edited(Sender: TObject; ARowProperties: TcxCustomEditorRowProperties ...

  5. delphi异常捕获try except语句 和 try finally语句用法

    原文地址:delphi try except语句 和 try finally语句用法以及区别作者:1865898133 一直写程序都没管他们,也尽量很少用,今天终于想把他给弄个明白,在网上找来,记下! ...

  6. Linux命令:sshpass

    sshpass介绍 sshpass是一款凡是为凡是使用ssl方式访问的操作提供一个免输入密码的非交互式操作,以便于在脚本中执行ssl操作,如ssh,scp等.sshpass是一家以色列公司Lingnu ...

  7. django 认证系统--2

    使用django的认证系统 User 对象 User是认证系统的核心.典型代表是用户和你的站点进行交互还有限制访问.注册用户等等.django认证框架中,只存在一个User类,像'superuser' ...

  8. 升级nodejs的方法(3)

    第一种 找到 目录 删除 再重装 第二种 安装模块n n stable 第三种 nvm https://blog.csdn.net/weibo392/article/details/77368550 ...

  9. pycahrm 激活

    Linux在/etc/hosts中添加 0.0.0.0 account.jetbrains.com就好,直接添加:0.0.0.0 account.jetbrains.comwindows的话没记错应该 ...

  10. 机器学习之overfiting

    有错欢迎指正,别让小弟继续错下去. 我们在使用机器学习过程中,经常会overfiting,overfiting的产生原因是noise.训练样本大的话,还好,不用考虑这个 问题.但是,当数据量小的时候, ...