[二分-最大化最小值]POJ - 3258

原创 2018年04月16日 21:14:39
River Hopscotch
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 17955 Accepted: 7505

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



题意:

牛要到河对岸,在与河岸垂直的一条线上,河中有N块石头,给定河岸宽度L,以及每一块石头离牛所在河岸的距离,

 现在去掉M块石头,要求去掉M块石头后,剩下的石头之间以及石头与河岸的最小距离的最大值。

看到题目很明显就是一个最大化最小值的问题,

这种问题对于小的值来说都是满足的,而我们需要寻找的最大的那个能满足条件的,很显然是满足单调性的

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 5e4+10;
int arr[maxn],n,l,m;
bool check(int mid) {
    int cnt = 0,now = 0,i = 0;
    while(i < n) {
        if(arr[i] < mid+now) cnt++;
        else now = arr[i];
        i++;
        //printf("%d ",now);
    }
    if(l < now + mid) cnt++;
    //printf("%d\n",cnt);
    return cnt <= m;
}
int main()
{
    while(~scanf("%d%d%d",&l,&n,&m)) {
        for(int i=0;i<n;i++) scanf("%d",&arr[i]);
        sort(arr,arr+n);
        int left = 0,right = 1e9+10;
        while(left <= right) {
            int mid = (left + right) >> 1;
            if(check(mid)) left = mid + 1;
            else right = mid - 1;
        }
        printf("%d\n",right);
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_38013346/article/details/79966623

Linux运维高薪入门及进阶全新经典视频-老男孩Linux第四部

2015年6月23日最新发布,老男孩老师亲讲,全新Linux运维入门经典!以简单、易用、高效的方式为初学者讲解如何学好LINUX的核心学习思想,讲解计算机硬件知识,及企业应用场景如何利用硬件优化服务,高并发集群。 北京老男孩教育,全国最高端、最专业、最负责的Linux运维实战教育机构! 2014-2015运维班就业质量全国最高,几乎100%就业,平均10K+,架构师15K+!
  • 2015年06月08日 12:02

POJ 3273 二分求最大化最小值

Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20086 Accep...
  • qq_22902423
  • qq_22902423
  • 2016-01-28 16:12:26
  • 2299

poj3258 二分及一些思考

题意:牛要到河对岸,在与河岸垂直的一条线上,河中有N块石头,给定河岸宽度L,以及每一块石头离牛所在河岸的距离,           现在去掉M块石头,要求去掉M块石头后,剩下的石头之间以及石头与河岸...
  • Non_Cease
  • Non_Cease
  • 2012-03-18 14:50:12
  • 3810

二分法典型应用(二)最大化最小值 POJ2456 Aggressive cows

问题描述: Farmer John has built a new long barn, with N (2
  • karry_zzj
  • karry_zzj
  • 2017-04-18 00:04:53
  • 481

POJ 2456 Aggressive cows(二分查找 最大化最小值)

题意:给出n个牛棚的位置,选择其中的m个给牛住,使得牛之间的距离的最小值最大。 首先我们可以知道距离的最小值一定产生于相邻的牛之间,所以要保证相邻牛之间的距离大于等于这个最小值d。所以把牛棚的位置...
  • secfly
  • secfly
  • 2016-03-25 16:25:55
  • 377

POJ 3258(二分——最大化最小值)

River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14284 Accep...
  • Twillz
  • Twillz
  • 2017-07-16 20:16:04
  • 76

POJ 2456 Aggressive cows(二分答案,最大化最小值)

题目链接 题意:给出n个牛舍坐标(在一直线上),然后有m个牛要入住,现在问相邻牛之间的最小距离最大是几 解答:二分答案,函数ok(d)检查距离是d合法与否 #include #include//#...
  • u013167299
  • u013167299
  • 2015-11-21 16:35:57
  • 497

poj 2456 二分法 最大化最小值

题目:http://poj.org/problem?id=2456 重新练习下二分法,发现还是手速不够 从这道题学到一下几点: 1、线性分几段的方法,看我的Judge()代码; 2、二分的wh...
  • u011026968
  • u011026968
  • 2014-05-10 21:44:48
  • 1139

poj 3258 最大化最小值

//必须特判一下特殊情况,只有起始点和终点。。。。。。 #include int L,M,N,a[500050]; int judge(int x)//判断距离为x时,此时可不可行 {   ...
  • sky_zdk
  • sky_zdk
  • 2017-03-15 21:41:19
  • 132

POJ 3258 River Hopscotch (最大化最小值)二分

题目:http://poj.org/problem?id=3258 题意:牛要到河对岸,在与河岸垂直的一条线上,河中有N块石头,给定河岸宽度L,以及每一块石头离牛所在河岸的距离,现在去掉M块石头,要...
  • discreeter
  • discreeter
  • 2016-07-22 09:46:43
  • 225
收藏助手
不良信息举报
您举报文章:[二分-最大化最小值]POJ - 3258
举报原因:
原因补充:

(最多只允许输入30个字)