zoukankan      html  css  js  c++  java
  • ACM模板——二分查找

     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

     1 int x[maxn];
     2 bool C(int d)//C为测试函数,判断此解是否可行
     3 {
     4 
     5 }
     6 int solve(int N)
     7 {
     8     sort(x,x+N);
     9     int lb = 0,ub = INF;
    10     while(lb < ub)//for(int i = 0;i < 100;i ++) or 1改成eps 
    11     {
    12         int mid =  lb+(ub-lb)/2;
    13         if(C(mid)) lb = mid+1;
    14         else ub = mid;
    15     }
    16     return lb;
    17 }
    假定一个解代入测试
  • 相关阅读:
    杭电ACM 1197
    杭电ACM 1196
    杭电ACM题目分类
    杭电ACM 1178
    指针转化(二重)
    怎么查看一个类的内存分布
    how find out what is causing Visual Studio to think each project is out of date
    MSB8013
    File mapping
    SHFileOperation 解决double-null terminated
  • 原文地址:https://www.cnblogs.com/Asurudo/p/10115822.html
Copyright © 2011-2022 走看看