【二分法】POJ3258-River Hopscotch

2023-12-15 22:38

本文主要是介绍【二分法】POJ3258-River Hopscotch,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

【抱大腿】啊啊啊又是一道恶心的题目!!!这道题是出在二分法里面的,因为这跟前面的一道青蛙过河的题特别像但是不一样,按照青蛙过河那个思路来走根本行不通,正好要按照跟那个思路相反的想法来想才行~



【题目】

River Hopscotch
Time Limit: 2000MS Memory Limit: 65536K
xxxxxxxxx马赛克xxxxxxxxx xxxxxxxxx马赛克xxxxxxxxx

Description

Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).

To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.

Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to Mrocks (0 ≤ M ≤ N).

FJ wants to know exactly how much he can increase the shortest distance *before* he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set ofM rocks.

Input

Line 1: Three space-separated integers:  LN, and  M 
Lines 2.. N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.

Output

Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing  M rocks

Sample Input

25 5 2
2
14
11
21
17

Sample Output

4

Hint

Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).

【题目解释】
有一堆牛要过河,河的长度是l,河中间有n个石头,牛只能踩着石头过河,问去掉m个石头后(去掉这m个石头的方式是随机的)的每种情况牛能走的石头间距最小值中,最大的那一个是多少(WTF!?)。
输入有多组实例,每组实例第一行输入l、n、m,下面n行输入石头距离起点的距离(起点为0)。

【解题思路】
这道题注意是要求一堆最小距离里面的最大值,逐个遍历绝逼要超时,要使用二分法。这道题里面注意牛能跳的距离是没有限制的,就是说牛想跳多远就跳多远,对于牛最多能跳多少次也没有限制!但是要求里面的最小值,所以这道题二分法所设置的中间值mid应为两石头间的最小距离并测试在此最小距离下,牛能否在跳到对岸的过程中,不存在比此mid距离更小的石头间距,以及跳完后,去掉的石头数 x(牛在跳跃并遇到小于mid距离的石头时,为了保证最小跳跃长度为mid,小于mid距离的石头不能往上面跳,因为如果一跳就会出现比mid更小的距离了,mid在这里做判断就没有意义了,这里去掉的石头数就是没有被牛跳上去的石头的石头数)跟要求的m数量相比,如果x>m时,说明牛每次跳的距离都太远了,即此时mid太大了,此时right变为mid来更新mid;如果x<m,那么说明牛也可以跳过m-x个石头,不踩就行了,要是x==m,那正好。但是对于每种x<=m的情况,其所出现的最小距离mid可能各不相同,找个变量把每次出来的mid值都比较一下取最大值就好啦~
这就是解题的大致思路。
那么……可能还会有同学有疑惑:“牛过河为毛要用跳的而且还必须要跳到石头上!?”


【代码】

#include<cstdio>
#include<algorithm>
using namespace std;int a[50001]={0},l,n,m,i;
bool fun(int m);int main()
{while(~scanf("%d%d%d",&l,&n,&m)){    int left=0,right=l,mid,ans;for(i=1;i<=n;i++)scanf("%d",&a[i]);sort(a,a+(n+1));while(right>=left){mid=(left+right)/2;//mid表示最小的距离! if(fun(mid)){left=mid+1;ans=mid;}elseright=mid-1;}printf("%d\n",ans);}
}bool fun(int mid)
{int start=0,x=0;//用start表示每次落脚点的坐标,每落一次地更新一次start for(i=1;i<=n;i++){if(a[i]-start<mid)x++;//x表示去掉的石头数,如果mid大于要跳的距离,就跳过当前这个石头,此时x++ ,并且不落地 elsestart=a[i];//此时落地! }if(l-start<mid)//判断最后一跳跳的距离要是小于mid的话那是不可以的 return false;if(x>m)//要是x>m就说明最小距离mid太大啦 return false;return true;
}



(参考了大神Rachel Zhang的代码)


这篇关于【二分法】POJ3258-River Hopscotch的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/498157

相关文章

数据结构基础之《(3)—二分法》

一、认识二分法 1、经常见到的类型是在一个有序数组上,开展二分搜索 2、但有序真的是所有问题求解时使用二分的必要条件吗?不 3、只要能正确构建左右两侧的淘汰逻辑,你就可以二分 二、二分法怎么用 1、在一个有序数组中,找某个数是否存在 public static boolean exist(int[] sortedArr, int num) {if (sortedArr == null |

Kotlin 二分法算法游戏--猜价格

本人最新想学习算法,算法是提高程序性能的关键! 程序就是数据结构和算法! 写了一个二分法的游戏,供大家参考: 当然,语言基于kotlin import java.util.*/*** Created by Administrator on 2017/10/18.*/fun main(args: Array<String>) {// println("请输入商品真实价格")//

二分法变种

package compublic class Test {public static void main(String[] args) {int[] nums = {4,7,7,9,12,13};int index = searchLastSmaller(nums,1);System.out.println(index);}/** 第一个与key相等的元素*/public static int

python基础-递归、二分法查找(for\递归)、三级菜单、压栈思想

递归方法二分法查找 通过for循环实现通过递归实现 递归应用–三级菜单压栈 递归方法 # age(1) n = 1 age(2)+2# age(2) n = 2 age(3)+2# age(3) n = 3 age(4)+2# age(4) n = 4 40def age(n):if n == 4:return 40return age(n+1)+2print

Java 算法的二分法

1.原理 1.1.在升序的集合中对半查找中位的下标,如果中位的下标和要查找的下标相等时,找到目标数,那二分结束。 1.2.如果 集合[中位]大于查找值,说明查找值在中位的左边,那么高位就是此时的中位-1,然后继续二分 1.3.如果集合[中位]小于查找值,说明查找值在中位的右边,那么低位就是此时的中位+1,然后继续二分 1.4.如果低位下标大于高位下标,那就没有找到值 2.实例 /*** @C

二分法与bisect模块

文章目录 二分法与bisect模块二分bisect模块常用函数bisect系,查找indexinsort系,实际插入。 应用 二分法与bisect模块 二分 二分前提是有序,否则不可以二分。二分查找算法的时间复杂度 O ( log ⁡ 2 n ) O(\log_2n) O(log2​n) 将一个有序序列[37, 99, 73, 48, 47, 40, 40, 25, 9

03-1. 二分法求多项式单根(20) MOOC

二分法求函数根的原理为:如果连续函数f(x)在区间[a, b]的两个端点取值异号,即f(a)f(b)<0,则它在这个区间内至少存在1个根r,即f(r)=0。 二分法的步骤为: 检查区间长度,如果小于给定阈值,则停止,输出区间中点(a+b)/2;否则如果f(a)f(b)<0,则计算中点的值f((a+b)/2);如果f((a+b)/2)正好为0,则(a+b)/2就是要求的根;否则如果f((a+b)

【Java 搜索二维矩阵 I II,多数元素 I II,分治法 二分法 摩尔投票法】

搜索二维矩阵 I II,多数元素,分治法 & 二分法 & 摩尔投票法 题目1:力扣-搜索二维矩阵[https://leetcode.cn/problems/search-a-2d-matrix/description/](https://leetcode.cn/problems/search-a-2d-matrix/description/)分治-排除法分治排除法-代码实现 二分法二分法-代

二分法三分法 - 模板

快速幂模板: int fun(int x, int n) //x^n{int pw = 1;while (n > 0) {if ((n % 2) == 1) // n & 1 等价于 (n % 2) == 1pw *= x;x *= x;n /= 2; // n >>= 1 等价于 n /= 2}return

二分法的专题总结——到底应该写小于还是小于等于、两个判断还是三个判断

二分法的专题总结 二分法的本质是:寻找序列中第一个满足某条件的元素的位置。 二分法中通常让人迷惑的地方不外乎 (1)while中什么时候写小于等于,什么时候不写等于; (2)while内部是写两个条件还是三个条件。 首先考虑升序排列的元素(降序等价),应当分为两种情况:(1)没有重复元素;(2)有重复元素。后者是前者的一般化,也就是说后者的算法也同样适用于前者。 (1)没有重复元素 这种情