二分怎么写

news/2024/7/7 13:04:30

主要参考这个https://www.topcoder.com/community/data-science/data-science-tutorials/binary-search/讲的非常仔细。

以前做题的时候,经常遇到一些二分的题目,但是对边界条件,主要是加一还是减一,把握的不是很准确,后来看到别人推荐的上面的链接,终于不再迷茫了。

这里需要注意几点:

1.一般二分的情况,准确寻找一个数,求满足条件的最大值,求满足条件的最小值。尤其注意求满足条件的最大值,防止进入死循环,需要进行2个数的情况测试。

原文中描述如下:

The code will get stuck in a loop. It will always select the first element as mid, but then will not move the lower bound because it wants to keep the no in its search space. The solution is to change mid = lo + (hi-lo)/2 to mid = lo + (hi-lo+1)/2, i.e. so that it rounds up instead of down. There are other ways of getting around the problem, but this one is possibly the cleanest. Just remember to always test your code on a two-element set where the predicate is false for the first element and true for the second.

2.再就是使用二分法注意的条件,这也是判断一个题目是否能够使用二分法的关键条件,如果要说解题需要什么套路的话,我感觉这就是套路。下面的黑体字,每一点说的都是非常重要的。

As you see, we used a greedy algorithm to evaluate the predicate. In other problems, evaluating the predicate can come down to anything from a simple math expression to finding a maximum cardinality matching in a bipartite graph.

Conclusion
If you’ve gotten this far without giving up, you should be ready to solve anything that can be solved with binary search. Try to keep a few things in mind:

    • Design a predicate which can be efficiently evaluated and so that binary search can be applied
    • Decide on what you’re looking for and code so that the search space always contains that (if it exists)
    • If the search space consists only of integers, test your algorithm on a two-element set to be sure it doesn’t lock up
    • Verify that the lower and upper bounds are not overly constrained: it’s usually better to relax them as long as it doesn’t break the predicate
 1 #include<bits/stdc++.h>
 2 #define pb push_back
 3 #define FOR(i, n) for (int i = 0; i < (int)n; ++i)
 4 #define dbg(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl
 5 typedef long long ll;
 6 using namespace std;
 7 typedef pair<int, int> pii;
 8 const int maxn = 1e3 + 10;
 9 int a[maxn];
10 int n;
11 //准确的查找一个值
12 //相当于stl里面的 binary_search()
13 int bin_ser(int tar) {
14     int left = 0, right = n - 1;
15     while(left <= right) {
16         int mid = left + (right - left) / 2;
17         if(a[mid] == tar) return mid;
18         else if(a[mid] < tar) left = mid + 1;
19         else right = mid - 1;
20     }
21     return -1;
22 }
23 bool check(int x) {
24     if(x < 10) return 1;
25     return 0;
26 }
27 //寻找满足条件的最小值
28 //相当于stl的 lower_bound()
29 int low_ser() {
30     int left = 0, right = n - 1;
31     while(left < right) {
32         int mid = left + (right - left) / 2;
33         if(check(mid)) {
34             right = mid;
35         } else left = mid + 1;
36     }
37     if(!check(left)) return -1;
38     return left;
39 }
40 //寻找满足条件的最大值
41 //比较常用,容易出bug,需要对2个数的情况进行判断
42 int up_ser() {
43     int left = 0, right = n - 1;
44     while(left < right) {
45         int mid = left + (right - left + 1) / 2;
46         if(check(mid)) left = mid;
47         else right = mid - 1;
48     }
49     if(!check(left)) return -1;
50     return left;
51 }
52 //还有对实数的二分,一般是达到要求的精度或者是迭代一定的次数
53 //然后得到想要的答案。
54 const double eps = 1e-8;
55 double bin_se() {
56     double left = 0, right n - 1;
57     while(right - left > eps) {
58         double mid = left + (right - left) / 2;
59         if(check(mid)) {
60             right = mid;
61         } else left = mid;
62     }
63     return left;
64 }
65 void solve() {
66 
67 }
68 int main() {
69     freopen("test.in", "r", stdin);
70     //freopen("test.out", "w", stdout);
71     solve();
72     return 0;
73 }

 

转载于:https://www.cnblogs.com/y119777/p/6059344.html


http://www.niftyadmin.cn/n/3291207.html

相关文章

C++的static关键字 详解

C的static关键字 详解 一、面向过程设计中的static 1、静态全局变量 在全局变量前&#xff0c;加上关键字static&#xff0c;该变量就被定义成为一个静态全局变量。我们先举一个静态全局变量的例子&#xff0c;如下&#xff1a; //Example 1 #include <iostream.h>voidf…

清除

仔细想想在哪些网上提交过真实个人信息&#xff0c;并逐一清除。 转载于:https://www.cnblogs.com/Cheetos/p/5416147.html

在linux 下查看文件GCC,glibc 版本

输入&#xff1a;gcc -v 得到 gcc version 2.96 ...... 输入&#xff1a;rpm -aq|grep glibc //-aq all query 输出&#xff1a;glibc-2.2.5-34 glibc...

BeanUtils的日期问题

//注册日期类型转换器 //第一种 自定义方法 ConvertUtils.register(new Converter(){ //第一个参数是目标类型 第二个参数是被转换的值 //就是 把第二个参数的值转换成第一个参数类型的值 value----》clazz类型的值 …

CentOS 7.0源码包搭建LNMP方法分享(实际环境下)

CentOS 7.0编译安装Nginx1.6.0MySQL5.6.19PHP5.5.14 一、配置防火墙&#xff0c;开启80端口、3306端口 CentOS 7.0默认使用的是firewall作为防火墙&#xff0c;这里改为iptables防火墙。 1、关闭firewall&#xff1a; systemctl stop firewalld.service #停止firewall systemct…

ssh远程登录Ubuntu报错:Permission denied, please try again.

ssh到server上的时候密码是对的但是报如下信息&#xff1a;# ssh 172.16.81.221root172.16.81.221s password:Permission denied, please try again.这个是由于如果不输入用户名的时候默认的是root用户&#xff0c;但是安全期间ssh服务默认没有开root用户的ssh权限 解决方法&am…

glibc 与 gcc 的概念。

glibc: glibc 是gnu发布的libc库&#xff0c;也即c运行库。 glibc是linux系统中最底层的api&#xff08;应用程序开发接口&#xff09;&#xff0c; 几乎其它任何的运行库都会依赖于glibc。 glibc除了封装linux操作系统所提供的系统服务外&#xff0c; 它本身也提供了许多其它…

Codeforces 346C Number Transformation II 构造

题目链接&#xff1a;点击打开链接 990ms卡过 #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #include<vector> #include<set> using namespace std; #define N 100010 #define L(x) (x<<1) #def…