POJ River Hopscotch 二分搜索
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 ≤ 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
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
Sample Input
25 5 2
2
14
11
21
17
Sample Output
4
Hint
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<string>
using namespace std;
#define MAXN 50001
#define INF 0x3f3f3f3f
/*
在一个有序序列中移除M个元素:
求元素之间最短距离最大能增大多少
先求出当前最短距离
二分查找当前最短-最终最短 找到通过移除M个元素最大的最短距离
关键还是check函数 在移除M个情况下,最短跳跃x能否到达终点
*/
int a[MAXN], L, n, m, tmp;
bool check(int mid)
{
int i, before = , cnt = ;
for (i = ; i <= n + ; i++)
{
if (a[i] - a[before] >= mid)//这块用于判断是否去掉石头
{
before = i;
}
else
{
cnt++;
if (cnt > m)
return false;
}
}
return true;
}
int main()
{
while (scanf("%d%d%d", &L, &n, &m) != EOF)
{
a[] = ;
for (int i = ; i < n; i++)
{
scanf("%d", &a[i]);
}
a[n+] = L;
sort(a, a + n+);
int beg = , end = L*,ans = ;
while (beg <= end)
{
int mid = (beg + end) / ;
if (check(mid))
{
ans = mid;
beg = mid + ;
}
else
end = mid - ;
}
printf("%d\n", ans);
}
return ;
}
POJ River Hopscotch 二分搜索的更多相关文章
- poj 3258"River Hopscotch"(二分搜索+最大化最小值问题)
传送门 https://www.cnblogs.com/violet-acmer/p/9793209.html 题意: 有 N 块岩石,从中去掉任意 M 块后,求相邻两块岩石最小距离最大是多少? 题解 ...
- 二分搜索 POJ 3258 River Hopscotch
题目传送门 /* 二分:搜索距离,判断时距离小于d的石头拿掉 */ #include <cstdio> #include <algorithm> #include <cs ...
- E - River Hopscotch POJ - 3258(二分)
E - River Hopscotch POJ - 3258 Every year the cows hold an event featuring a peculiar version of hop ...
- POJ 3258 River Hopscotch
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11031 Accepted: 4737 ...
- POJ 3258 River Hopscotch (binarysearch)
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5193 Accepted: 2260 Descr ...
- POJ 3258 River Hopscotch(二分答案)
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21939 Accepted: 9081 Desc ...
- [ACM] POJ 3258 River Hopscotch (二分,最大化最小值)
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6697 Accepted: 2893 D ...
- 【POJ - 3258】River Hopscotch(二分)
River Hopscotch 直接中文 Descriptions 每年奶牛们都要举办各种特殊版本的跳房子比赛,包括在河里从一块岩石跳到另一块岩石.这项激动人心的活动在一条长长的笔直河道中进行,在起点 ...
- POJ 3258:River Hopscotch 二分的好想法
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9326 Accepted: 4016 D ...
随机推荐
- 【XSY3209】RGB Sequence
题目 传送门 解法 用\(f_{i, j, k}\)表示有\(i\)个红石块, \(j\)个绿宝石块, \(k\)个钻石块 可以转移到\(f_{p+1, j, k}\). \(f_{i, p+1,k ...
- zoj3675 BFS+状态压缩
#include <stdio.h> #include <string.h> #include <queue> using namespace std; int n ...
- Offer收割_5
训练 投入 欲望. ---贾森博尔特 第一题:二分枚举答案,check时候模拟一下即可. 时间复杂度: O(n*logn). 第二题: 描述 小Hi在虚拟世界中有一只小宠物小P.小P有K种属性,每种 ...
- 【转】linux之type命令
转自: http://codingstandards.iteye.com/blog/831504 用途说明 type命令用来显示指定命令的类型.一个命令的类型可以是如下之一 alias 别名 keyw ...
- Java常用类库(三) : HashSet和LinkedList特点简析
今天内容: l 浅撩HashSet集合元素不可重复的原理 l 使用LinkedList模拟栈和队列 1.浅撩HashSet集合元素不可重复的原理 我们知道HashSet是添加不了相同的元素的,其原 ...
- 【Java基础】多态
首先先来个总结: 什么是多态 面向对象的三大特性:封装.继承.多态.从一定角度来看,封装和继承几乎都是为多态而准备的.这是我们最后一个概念,也是最重要的知识点. 多态的定义:指允许不同类的对象对同一消 ...
- Android项目实战_手机安全卫士手机防盗界面
#安全卫士手机防盗# ###1.Activity的任务栈 1.类似一个木桶,每层只能放一个木块,我们放入木块和取出木块的时候只能从最上面开始操作 ###2.Android中的坐标系![](http:/ ...
- 【译】x86程序员手册18-6.3.1描述符保存保护参数
6.3 Segment-Level Protection 段级保护 All five aspects of protection apply to segment translation: 段转换时会 ...
- RTL Compiler之synthesis steps
1 synthesis steps 1) Search Paths rc:/> set_attribute lib_search_path path / rc:/> set_attribu ...
- Centos6.6 安装nfs网络文件系统
一.介绍 nfs网络文件系统的,大部分用在内网文件共享,比如,对集群上传文件做共享,经常用在图片部分,当然数据量大了还是要做分离,做为专门的接口比较好,介绍一下基本安装环境: 1)Cnetos6.6 ...