Binary Search - Jump on the Stones
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。 输出文件只包含一个整数,即最短跳跃距离的最大值。
输入输出样例
25 5 2
2
11
14
17
21
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的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
- Linux查看文件大小5个常用命令
1. 前言 Linux 系统有非常好用的命令,功能也非常丰富,如果你对命令行工具熟悉,可以非常高效率完成维护工具.本文主要介绍Linux系统中,用于查看文件大小的命令. Linux 查看文件大小5个常 ...
- Linux用awk处理文本数据
awk -F',' -v OFS='\t' 'NR>1{print $1, $4, $6, $7}' demo2.csv | sort -t $'\t' -k 1 -r
- 关于Java序列化你应该知道的一切
什么是序列化 我们的对象并不只是存在内存中,还需要传输网络,或者保存起来下次再加载出来用,所以需要Java序列化技术. Java序列化技术正是将对象转变成一串由二进制字节组成的数组,可以通过将二进制数 ...
- 万能媒体播放器 PotPlayer
推荐一款超级牛逼播放器:PotPlayer
- pixi与lottie-web的benchmark测试
生产版本 "dependencies": { "lottie-web": "^5.5.7", "pixi-spin ...
- ARM与Cortex
arm系列从arm11开始,以后的就命名为cortex,并且性能上大幅度提升. 从cortex开始,分为三个系列,a系列,r系列,m系列. m系列与arm7相似,不能跑操作系统(只能跑ucos2),偏 ...
- python 大小写转换函数
capitalize() 首字母大写,其余全部小写 upper() 全转换成大写 lower() 全转换成小写 title() 标题首字大写,如"i love python".t ...
- c# WPF DataGrid 获取选中单元格信息
private void Dg_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) { Console.Write ...
- HttpClient 之 发送Https请求
HttpClient包是一个优秀的Http请求的开源jar. 本文Http工具类的封装基于HttpClient,封装后的工具类支持Https请求. 但是由于项目的需要快速的实现,以下代码还可能会有点过 ...
- js 动态绑定解绑事件
function addEvent(obj, type, handle) { if (obj.addEventListener) { obj.addEventListener(type, handle ...