函数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:
4
2
#include <cstdio>
#include <string>
#include <queue>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
#define ll __int64
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const 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":" ");
}
}
}