博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POj 3258 River Hopscotch 二分搜索 最大化最小值
阅读量:4112 次
发布时间:2019-05-25

本文共 3637 字,大约阅读时间需要 12 分钟。

River Hopscotch
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9970 Accepted: 4277

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 rocks (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 of M 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 2214112117

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).

Source

错因分析:没有好好理解最大化平均值的思想
分析:本题是个非常典型的二分搜索处理的最大化最小值问题;
           
模型:YES YES YES NO NO NO ,要求的是最大的YES,也就是最大化的最小值。理解模型非常关键
假设取一个无穷小的mid,则任意两个石块之间的距离都是大于它的,所以该种情况是能够在条件下(删除石块
数量<=m)的完成的,也就是代表YES,但是取一个无穷大的数时,则是不能在条件(删除石块数量<=m)下
完成的
,也就是代表NO,问题查找最大的最小值(最小值就是YES的位置)就成了查找最右边的YES的位置,
            核心思想:用二分搜索枚举最短距离的值,然后再判断在移除石块的数量不超过规定数量的情况下,
能否满足
任意两块石块的距离都<=二分枚举的该值,如果能,说明该值是<=最大化的最小距离的,则将该值
作为下限即l=mid,如果不能,则将该值mid减1后作为上限,意最大化的最小值最多只能取到mid-1,即r=mid-1;
           技巧处理:
mid=l+(r-l+1)/2;本题中之所以要加1,是因为当出现了一种特殊情况,也就是YES NO时
l=YES的位置,r=NO的位置,如果写成mid=l+(r-l)/2,那么mid值仍为l,l代入ok(mid)后,l=mid,则l的值并没有
改变,则进入了死循环.
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std; #define MM(a) memset(a,0,sizeof(a)) typedef long long LL; typedef unsigned long long ULL; const int mod = 1000000007; const double eps = 1e-10; const int inf = 0x3f3f3f3f; int x[50005],L,n,m;; int ok(int d) { int cnt=0,last=0; for(int i=1;i<=n;i++) if(x[i]-last
m) return 0; } else last=x[i]; return 1; } int main() { while(~scanf("%d %d %d",&L,&n,&m)) { for(int i=1;i<=n;i++) scanf("%d",&x[i]); n++;x[n]=L;x[0]=0; sort(x+1,x+n+1); int mid,l=0,r=x[n]; while(l
第二种做法是将区间写成左闭右开的形式即[l,r),不过要注意的是因为右边开区间无法取到最右边的值
所以r的初始值应大于x[n],可以设为r=x[n]+1,不然会wa
int mid,l=0,r=x[n]+1;         while(r-l>1)         {
mid=(l+r)/2; if(ok(mid)) l=mid; else r=mid; } printf("%d\n",l);

转载地址:http://fvgsi.baihongyu.com/

你可能感兴趣的文章
Linux下Oracle数据库账户被锁:the account is locked问题的解决
查看>>
记CSDN访问20万+
查看>>
Windows 环境下Webstorm 2020.3 版本在右下角找不到Git分支切换部件的一种解决方法
查看>>
Electron-Vue项目中遇到fs.rm is not a function问题的解决过程
查看>>
飞机换乘次数最少问题的两种解决方案
查看>>
有向无回路图的理解
查看>>
设计模式中英文汇总分类
查看>>
MFC实现五子棋游戏
查看>>
WPF实现蜘蛛纸牌游戏
查看>>
单例模式
查看>>
工厂方法模式
查看>>
模板方法模式
查看>>
数据结构之队列、栈
查看>>
数据结构之树
查看>>
数据结构之二叉树
查看>>
二叉树非递归遍历算法思悟
查看>>
红黑树算法思悟
查看>>
从山寨Spring中学习Spring IOC原理-自动装配注解
查看>>
实例区别BeanFactory和FactoryBean
查看>>
Spring后置处理器BeanPostProcessor的应用
查看>>