POJ - 3258

Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

[Submit]   [Go Back]   [Status]

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

Source

 
 
 
 
今天被这个题目坑了好久。。。顺便发现,自己其实还是没能真正完全将分治思想融会贯通。。。坑了好久啊。。
发现自己离ACM真正的神路,还有好远好远。。。一个算法学过虽然能A题了,但其实还没理解清楚其中的原理,只是记住了模板,换一个题目就不行了
就像这道最小值最大化。。。跟之前的最大值最小化完全是兄弟题,做法也相当类似,都是二分,,,只不过,这个题目的二分 要怎么个分法,就跟之前有很大的不同了
这也是坑了我好久的地方,所以,发现,自己着实基本功不够,。。二分是知道怎么做,但是具体怎么分的,就千差万别了。。。感觉还是没理清题目思路,之前还好
做着做着人就笨了。。还看别人的博客,影响了自己的思维。
好吧。。先这样说,具体再解系这个题目的二分方法。。。等明天吧。。貌似还有1分钟就0点了。。。明天再说吧。。晚安,先去睡了
 
 
这个题目的思路是这样的:
1.首先必须要排序,这个是肯定的。而且最好在排完序之后得到此时对应的每两个石头的间距记录下来。
2.确立二分的上下界,上界肯定就是河流的宽度嘛,下界就是最小石头间距。
3.然后就是典型的二分法了,在上下界之间二分,难些的是判断条件,其实理清了思路倒也不难。
4.判断当前mid值得方法是这样的,循环所有的石头间距,逐个累加,如果没有超过当前mid,意味着该石头可以搬开,即搬石头数++,如果超过了当前mid,则不能搬了,而且要把此时的累加距清零,以便后一段继续这样的事
5.根据循环之后的结果,如果搬石头数目超过了规定的m,说明mid值过大。。于是上界缩小。。。如果小于,则下界增大。。。由此二分完毕即得最大化间距。
 
 
贴的是小优大神的代码
 
//Memory Time
//420K 391MS #include<iostream>
#include<algorithm>
using namespace std; int main(void)
{
int L; //河总长
int n; //河中石头数(除起点S和终点外E)
int m; //移除石头数 while(cin>>L>>n>>m)
{
/*Input & Initial*/ int* dist=new int[n+]; //第i块石头到起点石头的距离为dist[i]
dist[]=; //起点S
dist[n+]=L; //终点E int low=L; //上界(一次跳跃的最短距离)
int high=L; //下界(一次跳跃的最大距离)
for(int i=;i<=n+;i++)
{
if(i<=n) //仅输入1~n,当i=n+1时仅用于寻找low
cin>>dist[i]; if(low > dist[i]-dist[i-])
low=dist[i]-dist[i-];
} sort(dist,dist+(n+)); //根据石头到S的距离升序排列 /*Binary-Search*/ while(low<=high)
{
int mid=(low+high)/; //对最大跳和最小跳的距离折中,二分查找mid相对于最优解是偏大还是偏小
//假设mid是移除m个石头后的最短距离 int delrock=; //利用当前的mid值能移除的石头数
int sum=; //类比POJ 3273, 这里是 连续距离的累加值
//当在第i个距离累加后sum for(int i=;i<=n+;)
{
if( (sum+=dist[i]-dist[i-]) <= mid)
{
i++;
delrock++;
}
else //当从第i个距离累加到i+k个距离后,若sum>mid,则k个距离作为一段
{
i++;
sum=; //sum置0,从第i+k+1个距离重新累加
}
} if(delrock<=m) //本题难点之一:即使delrock==m也不一定找到了最优解
low=mid+; //用当前mid值移除的石头数小于规定数,说明mid偏小
else
high=mid-; //反之mid偏大
} /*Output & Relax*/ cout<<low<<endl; delete dist;
} return ;
}
 

POJ-3258 (最小值最大化问题)的更多相关文章

  1. POJ 3258 最小值最大化 二分搜索

    题意:牛要到河对岸,在与河岸垂直的一条线上,河中有N块石头,给定河岸宽度L,以及每一块石头离牛所在河岸的距离, 现在去掉M块石头,要求去掉M块石头后,剩下的石头之间以及石头与河岸的最小距离的最大值. ...

  2. poj 3258 3273

    poj3258 题目  (最大化最小值)(最小值最大化) 题意:牛要到河对岸,在与河岸垂直的一条线上,河中有N块石头,给定河岸宽度L,以及每一块石头离牛所在河岸的距离,现在去掉M块石头,要求去掉M块石 ...

  3. poj 3258 River Hopscotch 题解

    [题意] 牛要到河对岸,在与河岸垂直的一条线上,河中有N块石头,给定河岸宽度L,以及每一块石头离牛所在河岸的距离, 现在去掉M块石头,要求去掉M块石头后,剩下的石头之间以及石头与河岸的最小距离的最大值 ...

  4. POJ--3258 River Hopscotch (最小值最大化C++)

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15273   Accepted: 6465 ...

  5. Halum UVA - 11478(差分约束 + 二分最小值最大化)

    题意: 给定一个有向图,每条边都有一个权值,每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权值减小d,把所有以v为起点的边的权值增加d,最后要让所有边权的最小值非负且尽量大 两个特判 1 ...

  6. Now or later UVALive - 3211(2-SAT 最小值最大化)

    emmm...去吃早饭了... rujia讲的很好.. 最小值最大化问题,,,二分枚举答案   设x1.x2为同一个集合中的元素,y1.y2为另一个集合中的元素,如果x1与y1之差小于mid,那么如果 ...

  7. 二分搜索 POJ 3258 River Hopscotch

    题目传送门 /* 二分:搜索距离,判断时距离小于d的石头拿掉 */ #include <cstdio> #include <algorithm> #include <cs ...

  8. E - River Hopscotch POJ - 3258(二分)

    E - River Hopscotch POJ - 3258 Every year the cows hold an event featuring a peculiar version of hop ...

  9. POJ 3258(二分求最大化最小值)

    题目链接:http://poj.org/problem?id=3258 题目大意是求删除哪M块石头之后似的石头之间的最短距离最大. 这道题目感觉大致代码写起来不算困难,难点在于边界处理上.我思考边界思 ...

随机推荐

  1. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:匿名内部类

    interface A{ public void printInfo() ; // } class B implements A{ // 实现接口 public void printInfo(){ S ...

  2. Ubuntu 16.04非编译安装Zabbix 3.2----服务端和客户端win的配置

    控服务器 - 什么是Zabbix Zabbix是企业级开源分布式监控服务器解决方案. 该软件监控网络的不同参数和服务器的完整性,还允许为任何事件配置基于电子邮件的警报. Zabbix根据存储在数据库( ...

  3. NO34 awk

  4. P1076 Wifi密码

    P1076 Wifi密码 转跳点:

  5. LInux的服务器编码格式的查看与更改

    1.locale 命令查看字符编码 然后修改/etc/sysconfig/i18n,如改成中文编码: LANG=en_US.UTF-8 改为 LANG="zh_CN.GBK" 然后 ...

  6. Kubernetes——命令行操作

    如果集群初始化失败需要的操作: master:  kubeadm reset   #回答y  执行一条它提示给你的iptables命令即可 node:  systemctl stop kubelet  ...

  7. markdown超链接怎么写?

    效果:我的微博 #上面的效果就是下面这种写法: [ 我的微博 ]( http://weibo.com/5833683560/profile?topnav=1&wvr=6&is_all= ...

  8. Django 项目搭建

    django(mvt结构) 虚拟环境 创建虚拟环境 mkvirtualenv django_py3 -p python3 切换虚拟环境 wokeon 虚拟环境名称 删除虚拟环境 rmvirtualen ...

  9. 20200119日志 EPLAN高压房 VFD 单线图 心得

    提纲: EPLAN 画单线图的方法,可以先绘制原理图,然后在一个柜子里面的器件 用方框圈起来.方框名称一样.注意 一个工程里面的器件编号是唯一的. 断路器选型. PT 手车 接地刀作用     具体内 ...

  10. netty权威指南学习笔记一——NIO入门(3)NIO

    经过前面的铺垫,在这一节我们进入NIO编程,NIO弥补了原来同步阻塞IO的不足,他提供了高速的.面向块的I/O,NIO中加入的Buffer缓冲区,体现了与原I/O的一个重要区别.在面向流的I/O中,可 ...