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);//显示数字