zoukankan      html  css  js  c++  java
  • Binary Search

    Binary Search algorithm.

    Wikipedia definition:

    In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array.

    Binary search compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful. If the search ends with the remaining half being empty, the target is not in the array.

    Binary search core part:

        int l = 0, r = L, mid;
        while (l <= r) {
            mid = (l + r) >> 1;
            if (judge(mid)) l = mid + 1, ans = mid;
            else r = mid - 1;
        }

    The "judge" module is a function that returns the possibility of owning such a situation. This module can be different on different problems.

    Such as this (P2678):

    bool judge(int x) {
        int last = 0, cnt = 0;
        for (int i = 1; i <= n + 1; i++) {
            if (dis[i] - dis[last] < x) cnt++;
            else last = i;
        }
        if (cnt > m) return false;
        return true;
    }

    例题:Luogu P2678 跳石头

    题目背景

    一年一度的“跳石头”比赛又要开始了!

    题目描述

    这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石。组委会已经选择好了两块岩石作为比赛起点和终点。在起点和终点之间,有 N 块岩石(不含起点和终 点的岩石)。在比赛过程中,选手们将从起点出发,每一步跳向相邻的岩石,直至到达 终点。

    为了提高比赛难度,组委会计划移走一些岩石,使得选手们在比赛过程中的最短跳 跃距离尽可能长。由于预算限制,组委会至多从起点和终点之间移走 M 块岩石(不能 移走起点和终点的岩石)。

    输入输出格式

    输入格式:

    输入文件名为 stone.in。

    输入文件第一行包含三个整数 L,N,M,分别表示起点到终点的距离,起点和终 点之间的岩石数,以及组委会至多移走的岩石数。

    接下来 N 行,每行一个整数,第 i 行的整数 Di(0 < Di < L)表示第 i 块岩石与 起点的距离。这些岩石按与起点距离从小到大的顺序给出,且不会有两个岩石出现在同 一个位置。

    输出格式:

    输出文件名为 stone.out。 输出文件只包含一个整数,即最短跳跃距离的最大值。

    输入输出样例

    输入样例#1:
    25 5 2 
    2
    11
    14
    17 
    21
    输出样例#1:
    4

    说明

    输入输出样例 1 说明:将与起点距离为 2 和 14 的两个岩石移走后,最短的跳跃距离为 4(从与起点距离 17 的岩石跳到距离 21 的岩石,或者从距离 21 的岩石跳到终点)。

    另:对于 20%的数据,0 ≤ M ≤ N ≤ 10。 对于50%的数据,0 ≤ M ≤ N ≤ 100。

    对于 100%的数据,0 ≤ M ≤ N ≤ 50,000,1 ≤ L ≤ 1,000,000,000。

    CODES:

     1 /* P2678 跳石头
     2  * Au: GG
     3  */
     4 #include <cstdio>
     5 #include <cstdlib>
     6 #include <cstring>
     7 #include <cmath>
     8 #include <iostream>
     9 #include <algorithm>
    10 using namespace std;
    11 const int N = 50000 + 3;
    12 int L, n, m, dis[N], ans;
    13 
    14 bool judge(int x) {
    15     int last = 0, cnt = 0;
    16     for (int i = 1; i <= n + 1; i++) {
    17         if (dis[i] - dis[last] < x) cnt++;
    18         else last = i;
    19     }
    20     if (cnt > m) return false;
    21     return true;
    22 }
    23 
    24 int main() {
    25     scanf("%d%d%d", &L, &n, &m);
    26     for (int i = 1; i <= n; i++)
    27         scanf("%d", &dis[i]);
    28     dis[n + 1] = L;
    29 
    30     int l = 0, r = L, mid;
    31     while (l <= r) {
    32         mid = (l + r) >> 1;
    33         if (judge(mid)) l = mid + 1, ans = mid;
    34         else r = mid - 1;
    35     }
    36     printf("%d
    ", ans);
    37     return 0;
    38 }

    Post author 作者: Grey
    Copyright Notice 版权说明: Except where otherwise noted, all content of this blog is licensed under a CC BY-NC-SA 4.0 International license. 除非另有说明,本博客上的所有文章均受 知识共享署名 - 非商业性使用 - 相同方式共享 4.0 国际许可协议 保护。
  • 相关阅读:
    学习使用Markdown
    开发落网电台windows phone 8应用的计划(10)-----收尾
    MyEclipse10安装SVN插件
    HBuilder连接码云
    sqlserver2004数据库备份,在sqlserver2008还原
    fiddler抓包时出现了tunnel to ......443
    Fiddler抓取Android真机上的HTTPS包
    Fiddler证书安装(查看HTTPS)
    Mac安装普元移动开发平台Primeton Mobile_7.2LA
    Mac下安装MySQL、Workbench以及建数据库建表最基础操作
  • 原文地址:https://www.cnblogs.com/greyqz/p/7200102.html
Copyright © 2011-2022 走看看