1 #include <bits/stdc++.h> 2 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 3 #define pb push_back 4 #define maxn 100 5 using namespace std; 6 7 int main() 8 { 9 vector<int> v{1,9,8,5,3,2,39,100,92}; 10 sort(v.begin(),v.end(),less<int>()); 11 cout << v[lower_bound(v.begin(),v.end(),39)-v.begin()] << endl; 12 cout << upper_bound(v.begin(),v.end(),39)-v.begin() << endl; 13 cout << binary_search(v.begin(),v.end(),40) << endl; 14 return 0; 15 }
lower_bound(开始地址,结束地址,待查找数),返回第一个大于等于待查找数字的地址
upper_bound(开始地址,结束地址,待查找数),返回第一个大于待查找数字的地址
binary_search(开始地址,结束地址,待查找数),找到返回1,没找到返回0
int x[maxn]; bool C(int d)//C为测试函数,判断此解是否可行 { } int solve(int N) { sort(x,x+N); int lb = 0,ub = INF; while(ub - lb > 1)//for(int i = 0;i < 100;i ++) or 1改成eps { int mid = lb+(ub-lb)/2; if(C(mid)) lb = mid; else ub = mid; } return lb; }