二分答案模板

来源:互联网 发布:马扎克系统编程 编辑:程序博客网 时间:2024/05/16 12:02


 ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, last)中的第一个大于等于值val的位置。

     ForwardIter upper_bound(ForwardIter first, ForwardIter last, const _Tp& val)算法返回一个非递减序列[first, last)中第一个大于val的位置。


#include <iostream>  #include <algorithm>//必须包含的头文件  using namespace std;  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;  }  




#include<bits/stdc++.h>  using namespace std;  typedef long long LL;  const LL INF = 1e18+1000;  const int MAXN = 1e6;  LL a[MAXN];  int cnt;  void Init()  {      cnt = 0;      for(LL i=1; i<INF; i*=2)          for(LL j=1; j*i<INF; j*=3)              for(LL k=1; i*j*k<INF; k*=5)                      a[cnt++] = i*j*k;  }  int main()  {      Init();      sort(a,a+cnt);      int t;      cin>>t;      while(t--)      {          LL n;          scanf("%lld",&n);          printf("%lld\n",a[lower_bound(a+1,a+cnt+1,n)-a]);      }      return 0;  }  




原创粉丝点击