River Hopscotch
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9326   Accepted: 4016

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, Lunits
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个石头,给了它们到起点的距离,还有一个终点。问要去掉这n个石头中的m个,使得其间距的最小值最大。

一开始在想,这个题怎么二分。最后也是看了别人的思路才反应过来,二分最小值,再比较最小值是mid时去掉的石头个数来改动left还是right。这种想法真是很好,尤其是遍历一遍就知道当最短值是mid时去掉的石头个数,那处的想法很nice,涨姿势~

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int L, N, M;
int dis[50005]; bool check(int mid)
{
int i,before = 0,cnt = 0;
for (i = 1; i <= N + 1; )
{
if (dis[i] - dis[before] >= mid)//这块用于判断是否去掉石头
{
i++;
before = i - 1;
}
else
{
i++;
cnt++;
}
}
if (cnt <= M)
{
return true;
}
else
{
return false;
}
} int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int i,left,right,mid;
cin >> L >> N >> M; dis[0] = 0;
dis[N + 1] = L; for (i = 1; i <= N; i++)
{
cin >> dis[i];
} sort(dis, dis + 1 + N); left = 0;
right = 2*L;//可能要把所有的石头都去掉,所以可能取到最大值L,这里索性将右端点扩大了很多 while (right - left > 1)
{
mid = (right + left) / 2;
if (check(mid))
{
left = mid;
}
else
{
right = mid;
}
}
cout << left << endl;
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 3258:River Hopscotch 二分的好想法的更多相关文章

  1. POJ 3258 River Hopscotch(二分答案)

    River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21939 Accepted: 9081 Desc ...

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

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

  3. poj 3258 River Hopscotch(二分+贪心)

    题目:http://poj.org/problem?id=3258 题意: 一条河长度为 L,河的起点(Start)和终点(End)分别有2块石头,S到E的距离就是L. 河中有n块石头,每块石头到S都 ...

  4. POJ 3258 River Hopscotch 二分枚举

    题目:http://poj.org/problem?id=3258 又A一道,睡觉去了.. #include <stdio.h> #include <algorithm> ]; ...

  5. poj 3258 River Hopscotch 二分

    /** 大意:给定n个点,删除其中的m个点,其中两点之间距离最小的最大值 思路: 二分最小值的最大值---〉t,若有距离小于t,则可以将前面的节点删除:若节点大于t,则继续往下查看 若删除的节点大于m ...

  6. 二分搜索 POJ 3258 River Hopscotch

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

  7. POJ 3258 River Hopscotch

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11031   Accepted: 4737 ...

  8. poj 3258 River Hopscotch 题解

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

  9. POJ 3258 River Hopscotch (binarysearch)

    River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5193 Accepted: 2260 Descr ...

随机推荐

  1. oozie的常见错误

    1.变量或路径的英文字母写错,常常是大小写搞混,或者是字母顺序颠倒. 2.本地 oozie_works 工作目录下的文件,如job.properties,workflow.xml等,修改后,忘记上传到 ...

  2. 无法删除oracle用户的问题

    http://blog.sina.com.cn/s/blog_684848d60101hj8a.html

  3. Rabbitmq与spring整合之重要组件介绍——AMQP声明式配置&RabbitTemplate组件

    上一节是使用rabbitAdmin的管理组件进行声明队列,交换器,绑定等操作,本节则是采用AMQP声明式配置来声明这些东西.AMQP声明主要是通过@Bean注解进行的. 配置: package com ...

  4. 使用vue框架开发前端项目的步骤

    前端项目的开发 1. 本地安装nodejs https://nodejs.org/en/download/ 2. 测试安装 > node -v 3. 本地安装git > git --ver ...

  5. DataTable.Copy()

    DataTable dtpocopy = dtPO.Copy(); DataRow[] dr = dtpocopy .Select("客户名称='" + cusName + &qu ...

  6. oracle通用帮助类

    需要的dll( EntityFramework.6.0.0Oracle.ManagedDataAccess.12.1.2400System.Configuration.dllEmitMapper.1. ...

  7. ACM-寻宝

    题目描述:寻宝 有这么一块神奇的矩形土地,为什么神奇呢?因为上面藏有很多的宝藏.该土地由N*M个小正方形土地格子组成,每个小正方形土地格子上,如果标有“E”,则表示该格可以通过:如果标有“X”,则表示 ...

  8. ACM-小偷的背包

    题目描述:小偷的背包   设有一个背包可以放入的物品重量为S,现有n件物品,重量分别是w1,w2,w3,...,wn.问能否从这n件物品中选择若干件放入背包中,使得放入的重量之和正好为S.如果有满足条 ...

  9. Web基础之Spring AOP与事务

    Spring之AOP AOP 全程Aspect Oriented Programming,直译就是面向切面编程.和POP.OOP相似,它也是一种编程思想.OOP强调的是封装.继承.多态,也就是功能的模 ...

  10. Mac 配置代码高亮 Git状态显示

    Mac 一个为开发者量身定做的笔记本,分享给大家希望能帮助大家配置一个好的开发环境,好的开发环境才有好的心情Code. 首先进入到Home到目录一般默认打开的都是Home,如果不是输入 cd ~ 回车 ...