1.经典模版
定义上下限
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=500+1,INF=0x3f3f3f3f;
const ll mod=1e9+7;
int a[N];
int main()
{
int l=startpoint,r=endpoint,mid,x,n;
sort(a,a+n,cmp);
while(l<=r)
{
mid=(l+r)/2;
if(a[mid]<x) l=mid+1;
else if(a[mid]==x) break;
else r=mid-1;
}
}
2.lower_bound(a,a+n,x);
查找有序区间中第一个大于或等于x的位置
int pos=lower_bound(a,a+n,x)-a;//显示位置
int num=*lower_bound(a,a+n,x);//显示数字
hdu 2141 题目详解
3.upper_bound(a,a+n,x);
查找有序区间中第一个大于x的位置
int pos=upper_bound(a,a+n,x)-a;//显示位置
int num=*upper_bound(a,a+n,x);//显示数字
本文介绍了二分查找的经典模板及应用,详细解释了lower_bound和upper_bound函数的使用方法,并通过实例展示了如何查找有序区间内特定元素的位置。
4404

被折叠的 条评论
为什么被折叠?



