lower_bound和upper_bound的实现和基本用法
最近一直在学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的实现和基本用法的更多相关文章
- 关于lower_bound( )和upper_bound( )的常见用法
lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begin,end,num):从数 ...
- lower_bound( )和upper_bound( )的常见用法
lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begin,end,num):从数 ...
- lower_bound()和upper_bound()用法详解
lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. lower_bound( begin,end,num):从数组的begin位置到end ...
- 【模板】关于vector的lower_bound和upper_bound以及vector基本用法 STL
关于lower_bound和upper_bound 共同点 函数组成: 一个数组元素的地址(或者数组名来表示这个数组的首地址,用来表示这个数组的开头比较的元素的地址,不一定要是首地址,只是用于比较的& ...
- STL之std::set、std::map的lower_bound和upper_bound函数使用说明
由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊 ...
- C++ STL lower_bound()和upper_bound()
lower_bound()和upper_bound()用法 1.在数组上的用法 假设a是一个递增数组,n是数组长度,则 lower_bound(a, a+n, x):返回数组a[0]~a[n-1]中, ...
- 关于lower_bound()和upper_bound()
关于lower_bound()和upper_bound(): 参考:关于lower_bound( )和upper_bound( )的常见用法 注意:查找的数组必须要是排好序的.因为,它们查找的方式也是 ...
- STL:map中的lower_bound和upper_bound
今天在做leetcode的Longest Increasing Subsequence题目时,需要用到二分查找,于是翻看了<STL源码剖析>这本书,发现map里面有lower_bound和 ...
- C++二分查找:lower_bound( )和upper_bound( )
#include<algorithm>//头文件 //标准形式 lower_bound(int* first,int* last,val); upper_bound(int* first, ...
随机推荐
- __file__ 作用以及模块导入方法
python 执行py 文件的时候,默认就会把当前目录增加到sys.path中 import os print(__file__) #打印文件当前的位置 直接在目录里面执行,结果显示当前文件(pych ...
- 线程池之 newScheduledThreadPool中scheduleAtFixedRate(四个参数)
转自:https://blog.csdn.net/weixin_35756522/article/details/81707276 说明:在处理消费数据的时候,统计tps,需要用一个线程监控来获得tp ...
- dev16 cxgrid 在DLL里报0地址错
dev16 cxgrid 在DLL里Form里使用,报0地址错,在EXE里正常.c++builder 的DLL报错,delphi也报错. First chance exception at $09CE ...
- vue 监听state 任意值变化、监听mutations actions
// store.watch((state) => state.count + 1, (newCount) => { // console.log(' 监听') // }) // stor ...
- KMP算法next数组求解
关于KMP算法,许多教材用的是递推式求解,虽然代码简洁,但是有些不好理解,这里我介绍一种迭代求next数组的方法 KMP算法关键部分就是滑动模式串,我们可以每次滑动一个单位,直到出现可能匹配的情况,此 ...
- [Linux].deb软件包:wine-qq2013-longeneteam安装与卸载
--------------------------------------------------------------------------------------------- 首先切换到r ...
- linux下搭建生成HLS所需的.ts和.m3u8文件
要想利用HLS来实现视频的在线播放,就得需要将一个完整的视频文件切割成多个ts视频流,然后利用m3u8的索引文件来播放. 在Mac下,苹果提供了streamingTools的工具,里面有mediafi ...
- eclipse git 分享项目到GitHub上
先在github上创建仓库
- 吴裕雄 python oracle子查询的用法(3)
import cx_Oracle conn = cx_Oracle.connect("scott/admin@localhost:1521/orcl")cursor = conn. ...
- c3 新特性
渐变 线性渐变 .line { height: 100px; /*线性渐变语法*/ background-image: linear-gradient( to right,/* ...