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 = , r = L, mid;
while (l <= r) {
mid = (l + r) >> ;
if (judge(mid)) l = mid + , ans = mid;
else r = mid - ;
}

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 = , cnt = ;
for (int i = ; i <= n + ; 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:

 /* P2678 跳石头
* Au: GG
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = + ;
int L, n, m, dis[N], ans; bool judge(int x) {
int last = , cnt = ;
for (int i = ; i <= n + ; i++) {
if (dis[i] - dis[last] < x) cnt++;
else last = i;
}
if (cnt > m) return false;
return true;
} int main() {
scanf("%d%d%d", &L, &n, &m);
for (int i = ; i <= n; i++)
scanf("%d", &dis[i]);
dis[n + ] = L; int l = , r = L, mid;
while (l <= r) {
mid = (l + r) >> ;
if (judge(mid)) l = mid + , ans = mid;
else r = mid - ;
}
printf("%d\n", ans);
return ;
}

Binary Search - Jump on the Stones的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  3. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  4. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  5. [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  6. [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  7. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  8. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  9. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

随机推荐

  1. 最强DE战斗力

    最强DE战斗力 时间限制: 1 Sec  内存限制: 128 MB提交: 40  解决: 14[提交][状态] 题目描述 春秋战国时期,赵国地大物博,资源非常丰富,人民安居乐业.但许多国家对它虎视眈眈 ...

  2. Spring通知类型及使用ProxyFactoryBean创建AOP代理

    Spring 通知类型 通过前面的学习可以知道,通知(Advice)其实就是对目标切入点进行增强的内容,Spring AOP 为通知(Advice)提供了 org.aopalliance.aop.Ad ...

  3. python中冒泡 排序法练习题

    # 第四题:写出冒泡排序函数,可以排序任意类型的元素,可以逆序 # 1.实现冒泡排序算法 # 2.可以排序任意类型的元素 # 3.能够通过参数设置进行逆序,默认升序 def my_sort(lt,ke ...

  4. qbxt Day2 on 19-7-25

    qbxt Day2 on 19-7-25 --TGZCBY 上午 1. 矩阵乘法在图论上的应用 有的时候图论的转移方程可以用dp的方式转移 特别是两个数的乘积求和的时候 比如邻接矩阵中f[i][j]表 ...

  5. ruby+selenium-webdriver一步一步完成自动化测试-----准备篇

    这一系列文章目的不是讲ruby,也不是讲selenium-webdriver,而是通过一个登录soso首页的小例子讲怎样一步一步写好自动化测试.目标是:面向对象编程,实现逻辑与数据分离. 如果你觉得已 ...

  6. JS - 模块

    # CommonJS - [CommonJS - Wikipedia](https://en.wikipedia.org/wiki/CommonJS) ## 介绍 主要在浏览器之外地方(例如服务器和桌 ...

  7. 关系型数据库MySQL(三)_触发器

    简介 用来给保证数据完整性的一种方法,经常用于加强数据的完整性: 是与表事件相关的特殊的存储过程,与存储过程的唯一区别是触发器不能执行execute语句调用,而是在用户执行SQL语句时自动触发执行 执 ...

  8. urllib爬取实例

    #汉字转码.多个参数拼接 from urllib import request base_url = "http://www.baidu.com/s?" content = inp ...

  9. spring boot 多环境(dev、test、prod)配置文件---命令行切换

    properties配置格式 在Spring boot 中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对于你的环境标识,比如: ...

  10. HDU 6470 /// 矩阵快速幂

    题目大意: f[1]=1 f[2]=2 f[n]=f[n-1]+2*f[n-2]+n^3 在某博客截的图 现在忘记原博位置了 抱歉 根据递推式1和递推式3构造出两个矩阵 #include <bi ...