lower_bound()

来源:互联网 发布:绣春刀2剧情解析知乎 编辑:程序博客网 时间:2024/06/05 15:16

函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置.函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置,且last的位置是越界的.


 iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素。

 iterator upper_bound( const key_type &key ):返回一个迭代器,指向键值> key的第一个元素。


int main(){int point[10] = {1,3,7,7,9};int tmp = upper_bound(point, point + 5, 7) - point;//按从小到大,7最多能插入数组point的哪个位置printf("%d\n",tmp);tmp = lower_bound(point, point + 5, 7) - point;////按从小到大,7最少能插入数组point的哪个位置printf("%d\n",tmp);return 0;}output:42


#include <cstdio>#include <string>#include <queue>#include <iostream>#include <algorithm>#include <cstring>#include <map>#define ll __int64using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int MAXN=100005;long long a[MAXN];long long dp[MAXN];int main(){// hdoj 5748  求序列中每个数的最长上升序列int t;cin>>t;while(t--){int n;scanf("%d",&n);memset(dp,0x3f3f3f3f,sizeof(dp));for(int i=0; i<n; i++){scanf("%lld",&a[i]);*lower_bound(dp,dp+i+1,a[i])=a[i];//*lower_bound, 将返回的地址处赋值 long long le=lower_bound(dp,dp+i+1,a[i])-dp;printf("%lld%s",le+1,i==n-1?"\n":" ");}}}




0 0