关于lower_bound的使用
突然发现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的使用的更多相关文章
- STL源码学习----lower_bound和upper_bound算法
转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...
- 【刷题记录】 && 【算法杂谈】折半枚举与upper_bound 和 lower_bound
[什么是upper_bound 和 lower_bound] 简单来说lower_bound就是你给他一个非递减数列[first,last)和x,它给你返回非递减序列[first, last)中的第一 ...
- STL之lower_bound和upper_bound
ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...
- UVA 10474 大理石在哪 lower_bound
题意:找输入的数在排完序之后的位置. 主要是lower_bound 函数的使用.它的作用是查找大于或者等于x的第一个位置. #include<cstdio> #include<alg ...
- [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 ...
- 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 ...
- [STL] lower_bound和upper_bound
STL中的每个算法都非常精妙, ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一 ...
- STL lower_bound upper_bound binary-search
STL中的二分查找——lower_bound .upper_bound .binary_search 二分查找很简单,原理就不说了.STL中关于二分查找的函数有三个lower_bound .upper ...
- vector的插入、lower_bound、upper_bound、equal_range实例
对于这几个函数的一些实例以便于理解: #include <cstdlib> #include <cstdio> #include <cstring> #includ ...
- STL中的lower_bound和upper_bound的理解
STL迭代器表述范围的时候,习惯用[a, b),所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置. 譬如,值val在容器内的时候,从 ...
随机推荐
- 10个devexpress ASPxPivotGrid常见问题
原文:10个devexpress ASPxPivotGrid常见问题 1.DXperience ASPxGridView如何开启lightweight模式 描述:ASPxGridView样式主题中pa ...
- POJ 2081 Recaman's Sequence(水的问题)
[简要题意]:这个主题是很短的叙述性说明.挺easy. 不重复. [分析]:只需要加一个判断这个数是否可以是一个数组,这个数组的范围. // 3388K 0Ms #include<iostrea ...
- C#关于HttpClient的应用(二):极光推送IM集成
public class JPushClient:BaseHttpClient { private String appKey; private String masterSecret; public ...
- 【百度地图API】如何制作自定义样式的公交导航结果面板?
原文:[百度地图API]如何制作自定义样式的公交导航结果面板? 摘要: 百度地图API有默认的公交导航结果面板,但样式比较单一:而百度地图上的结果面板就比较美观.如何利用百度地图API来制作一个比较美 ...
- krpano音量控制(我们已经转移到krpano中国网站 krpano360.com)
需求: 实现音量控制,这是官网的样例, 本文已经转移 到 krpano中文网 p=148">http://krpano360.com/? p=148 很多其它教程关注微信公众号 krp ...
- SQL2005性能分析一些细节功能你是否有用到?(二)
原文:SQL2005性能分析一些细节功能你是否有用到?(二) 上一篇:SQL2005性能分析一些细节功能你是否有用到? 我简单的提到了些关于SQL性能分析最基本的一些方法,下面的文章我会陆续补充.前面 ...
- Oracle性能分析11:系统统计信息
早期Oracle查询优化器的开销计算是基于运行SQL语句所须要的物理读,这种方法被叫做I/O开销模式(I/O cost model),这种方法的主要缺点是觉得单块读和多块读开销相当.在Oracle 8 ...
- Php设计模式(三):行为型模式part2
原文详见:http://www.ucai.cn/blogdetail/7023?mid=1&f=5 可以在线运行查看效果哦! <接上文> 5.中介者模式(Mediator) : 用 ...
- json.net 比jsonIgnore 更好的方法 修改源码
关于 JsonIgnore 问题, EF T4 模板 中 存在主外键关系 namespace WindowsFormsApplication1{ using System; using ...
- asp.net JSON(一)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...