River Hopscotch
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10842   Accepted: 4654

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 M rocks (0 ≤ MN).

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: L, N, 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
题目大意:奶牛过河游戏,胆小的奶牛只敢跳往最近的石头,河长L米,里面有N块石头,给你每一块石头距离起始位置的距离,
现在让你移除M块石头,使得相邻石头之间的距离最大,让你输出最长的距离是多少。
思路分析:最近几天一直在做二分的的题目,导致做这道题的时候读懂题意,看了下数据范围就基本确定是用二分来写了,基本
都差不多,但是每一道题都有需要值得注意的地方,比如为了防止溢出特地用了__int64(不造有没有用),本题弱还是贡献了
两发wa,原因主要是在起点和终点的处理上,起点和终点的石头是不能移动的!后来把终点单独拿出来进行处理,其余的如果不满足
就去掉右边的石头,成功A掉。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=50000+100;
__int64 a[maxn];
int n,m;
bool check(__int64 x)
{
    int t=0;
    int i;
    __int64 len;
    __int64 now=a[0],next;
    __int64 sum=0;
    for(i=n;i>=0;i--)
    {
        if(a[n+1]-a[i]<x) t++;
        else break;
    }
    int h=i;
    for(i=1;i<=h;i++)
    {
       next=a[i];
       len=next-now;
       if(len<x)
       {
           t++;
           if(t>m) return false;
       }
      else now=a[i];
    }
    return true;
}
int main()
{
    __int64 L;
    while(scanf("%I64d%d%d",&L,&n,&m)!=EOF)
    {
        a[0]=0;
        for(int i=1;i<=n;i++)
            scanf("%I64d",&a[i]);
        a[n+1]=L;
        sort(a,a+n+2);
        __int64 l=0,r=L;
        __int64 ans=0;
        while(l<=r)
        {
            __int64 mid=(l+r)>>1;
            //cout<<mid<<endl;
            if(check(mid)) ans=mid,l=mid+1;
            else r=mid-1;
        }
        //cout<<check(4)<<endl;
        printf("%I64d\n",ans);
    }
    return 0;
}

poj3258 二分 最小值最大化问题的更多相关文章

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

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

  2. Bomb Game HDU - 3622(二分最小值最大化)

    题意: 就是给出n对坐标,每对只能选一个,以选出来的点为圆心,半径自定义,画圆,而这些圆不能覆盖,求半径最小的圆的最大值 解析: 看到最x值最x化,那二分变为判定性问题,然后...然后我就没想到... ...

  3. Stream My Contest UVA - 11865(带权最小树形图+二分最小值最大化)

    #include <iostream> #include <cstdio> #include <sstream> #include <cstring> ...

  4. poj3258 River Hopscotch(二分最小值,好题)

    https://vjudge.net/problem/POJ-3258 二分最小值,判断需要删去的点的个数,如果大于给定,则直接return 0,则说明该数需要再小. 最后注意,起点是0终点是l,起点 ...

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

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

  6. codeforce 1070 E Getting Deals Done(二分求最大化最小值)

    Polycarp has a lot of work to do. Recently he has learned a new time management rule: "if a tas ...

  7. POJ-3258 (最小值最大化问题)

    POJ - 3258 River Hopscotch Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & ...

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

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

  9. [ACM] POJ 3258 River Hopscotch (二分,最大化最小值)

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6697   Accepted: 2893 D ...

随机推荐

  1. Typecho中文验证码Captcha插件

    前言实在是受不了每天都要删除掉上百条的垃圾评论,干脆自己做了个验证码插件,顺带做的完善了些,分享给大家. 本插件是在评论验证码插件基础上完善而来.所不同的是,采用了最新的securimage 3.0. ...

  2. C程序设计语言练习题1-6

    练习1-6 验证表达式getchar() != EOF的值是0还是1. 代码如下: #include <stdio.h> // 包含标准库的信息. int main() // 定义名为ma ...

  3. nRF51 DFU 初始化包介绍及生成工具

    nRF51 DFU 初始化包 当升级数据包时,在应用程序映像传输之前,在DFU中需要初始化包来执行映像的安全检测.这个初始化包作为升级流程的一部分提供了安全检测机制,因此被升级的设备只能接收兼容的应用 ...

  4. Thinking in Java——笔记(21)

    Concurrency However, becoming adept at concurrent programming theory and techniques is a step up fro ...

  5. Effective Java Item3:Enforce the singleton property with a private constructor or an enum type

    Item3:Enforce the singleton property with a private constructor or an enum type 采用枚举类型(ENUM)实现单例模式. ...

  6. PYTHON线程知识再研习B

    使用threading.Thread模块,也有两种使用方法,可以用类,也可以在实例化对象中传入函数或类实例. #!/usr/bin/env python # -*- coding: utf-8 -*- ...

  7. 在WPF中使用AForge.net控制摄像头拍照

    原文:在WPF中使用AForge.net控制摄像头拍照 利用AForge.net控制摄像头拍照最方便的方法就是利用PictureBox显示摄像头画面,但在WPF中不能直接使用PictureBox.必须 ...

  8. hcharts

    折线图 http://www.hcharts.cn/demo/index.php?p=10 饼状图 http://higrid.net/docs/highcharts_cn/#plotOptions- ...

  9. 深度剖析JDK动态代理机制

    摘要 相比于静态代理,动态代理避免了开发人员编写各个繁锁的静态代理类,只需简单地指定一组接口及目标类对象就能动态的获得代理对象. 代理模式 使用代理模式必须要让代理类和目标类实现相同的接口,客户端通过 ...

  10. epoll的原理和使用方法

    设想一个场景:有100万用户同一时候与一个进程保持着TCP连接,而每个时刻仅仅有几十个或几百个TCP连接时活跃的(接收到TCP包),也就是说,在每一时刻,进程值须要处理这100万连接中的一小部分连接. ...