River Hopscotch-[二分查找、贪心]
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 ≤ 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 解题思路:
一开始题意并不太好理解。稍微转换一下思路,给定N+2个石头,从中选出N+2-M个石头使他们之间最小的距离最大。
经过分析容易发现,无论如何增减,石头之间的距离是有限的,最小为当前石头之间最小距离min,最大为L。那么问题可以转换成这样:
在[min,L]范围内搜索满足下列条件的距离dm:
1)有且仅有N+2-M个石头(包括起始和终点处的两块石头),他们之间的最小距离为di。
(即:任意增减一个石头,他们之间的最小距离就不是di)
2)dm=max{di};
注意:条件(1)(2)保证dm的唯一性。
那么我们可以二分查找di,以di为间隔依次挑选石头,数量记为cnt,
如果cnt>N+2-M,说明di选小了;
如果cnt<N+2-M,说明di选大了;
如果cnt=N+2-M,不一定满足条件(2),需要继续向上(增大)查找。 代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <ctime>
using namespace std;
#define print_time_ printf("%f\n",double(clock())/CLOCKS_PER_SEC);
#define maxn 50000
int s[maxn+];
int L,N,M; bool judge(int mid){
int cnt=;
int i=; while(){
i=lower_bound(s+i, s+N+, s[i]+mid)-s;
if(i<N+&&s[N+]-s[i]>=mid) cnt++;
else break; }
return cnt-(N-M)>=;
} int Bsearch(int a,int b){
if(!N) return b;
int mid=(a+b)/;
int ans=;
while(a<=b){
bool flag=judge(mid);
if(flag){
if(mid>ans) ans=mid;
a=mid+;
mid=(a+b)/;
}
else {
b=mid-;
mid=(a+b)/;
}
}
return ans;
}
int main() {
scanf("%d%d%d",&L,&N,&M);
for(int i=;i<N;i++)
scanf("%d",&s[i+]);
s[]=;s[N+]=L;
sort(s, s+N+);
int low_bound=(<<)-;
for(int i = ; i <= N+; i++)
{
low_bound = min(low_bound, s[i]-s[i-]);
}
printf("%d\n",Bsearch(low_bound, L));
//print_time_
return ;
}
River Hopscotch-[二分查找、贪心]的更多相关文章
- POJ 3258 River Hopscotch(二分查找答案)
一个不错的二分,注释在代码里 #include <stdio.h> #include <cstring> #include <algorithm> #include ...
- Can you find it?——[二分查找]
Description Give you three sequences of numbers A, B, C, then we give you a number X. Now you need t ...
- River Hopscotch(二分最大化最小值)
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9923 Accepted: 4252 D ...
- C. Tavas and Karafs 二分查找+贪心
C. Tavas and Karafs #include <iostream> #include <cstdio> #include <cstring> #incl ...
- poj 3258 River Hopscotch 【二分】
题目真是不好读,大意例如以下(知道题意就非常好解了) 大致题意: 一条河长度为 L,河的起点(Start)和终点(End)分别有2块石头,S到E的距离就是L. 河中有n块石头,每块石头到S都有唯一的距 ...
- 【POJ - 3258】River Hopscotch(二分)
River Hopscotch 直接中文 Descriptions 每年奶牛们都要举办各种特殊版本的跳房子比赛,包括在河里从一块岩石跳到另一块岩石.这项激动人心的活动在一条长长的笔直河道中进行,在起点 ...
- bzoj 1650: [Usaco2006 Dec]River Hopscotch 跳石子【贪心+二分】
脑子一抽写了个堆,发现不对才想起来最值用二分 然后判断的时候贪心的把不合mid的区间打通,看打通次数是否小于等于m即可 #include<iostream> #include<cst ...
- poj3258 River Hopscotch(二分最小值,好题)
https://vjudge.net/problem/POJ-3258 二分最小值,判断需要删去的点的个数,如果大于给定,则直接return 0,则说明该数需要再小. 最后注意,起点是0终点是l,起点 ...
- POJ3258 River Hopscotch(二分最大化最小值)
题目链接:http://poj.org/problem?id=3258 题意:给n个石头,起点和终点也是两个石头,去掉这石头中的m个,使得石头间距的最小值最大. 思路:二分石头间的最短距离,每次贪心地 ...
随机推荐
- Leetcode929.Unique Email Addresses独特的电子邮件地址
每封电子邮件都由一个本地名称和一个域名组成,以 @ 符号分隔. 例如,在 alice@leetcode.com中, alice 是本地名称,而 leetcode.com 是域名. 除了小写字母,这些电 ...
- spark-ML基础
一.ML组件 ML的标准API使用管道(pipeline)这样的方式,可以将多个算法或者数据处理过程整合到一个管道或者一个流程里运行,其中包含下面几个部分: 1. dataFrame:用于ML的dat ...
- Valgrind 初次接触
Valgrind 英文的意思是:堆内存 它有很多小工具,作用各不相同 学习于: http://blog.csdn.net/sduliulun/article/details/7732906 http: ...
- RGBA(0,0,0,0)调色
前三个值(红绿蓝)的范围为0到255之间的整数或者0%到100%之间的百分数.这些值描述了红绿蓝三原色在预期色彩中的量. 第四个值,alpha值,制定了色彩的透明度/不透明度,它的范围为0.0到1.0 ...
- DELL(linux 系统里系统掉盘)(阵列Foreign)命令行里重做阵列
故障现象 (阵列Foreign 系统下重做阵列) 例:四合一机器(DELL_XENCOMPA09) 有四个硬盘 如图 少认到一个 df -h fdisk -l 2>/dev/null | gr ...
- Directx11教程38 纹理映射(8)
原文:Directx11教程38 纹理映射(8) 上篇日志中,我们用纹理和光照颜色调制的方式得到最终颜色,本章我们尝试用纹理采样的颜色,直接做为材质的漫反射系数Kd,并用它来做光照计算,最后 ...
- md5小工具
<?php$str = "123456";echo md5($str);?>
- hdu5442 Favorite Donut 后缀数组 长春网赛
wa从一点到晚上11点没停过,也不知道为什么错,第二天换了个思路做,终于过了.这题还是有点问题的,数据有点水,我看到有人贴的代码baabbaab这组数据是4 0,明显错的,但是却可以过. 下面的是我第 ...
- Python与Java异常类层级区别
- json 2016-09-18 22:03 207人阅读 评论(18) 收藏
JSON:JavaScript 对象表示法(JavaScript Object Notation) JSON是什么? JSON(JavaScript Object Notation) 是一种轻量级的数 ...