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个,使得石头间距的最小值最大. 思路:二分石头间的最短距离,每次贪心地 ...
随机推荐
- C语言获当地时间
代码如下: #include <stdio.h> #include <time.h> #define DEBUGE 1 int main(void) { time_t rawt ...
- LeafLet 简单使用
Leaflet 使用 最近在Angular项目中,用到了地图,由于种种原因放弃了百度地图api使用,最后选择了leaflet,简单介绍一下. 介绍: Leaflet 是一个为移动设备设计的交互式地图的 ...
- html(),val(),text()的区别
.html(),.text(),.val() 三种方法都是用来读取选定元素的内容: .html()是用来读取元素的HTML内容(包括其Html标签): .text()用来读取元素的纯文本内 容,包括其 ...
- oracle管理索引
索引是用于加速数据存取的数据对象,合理的使用索引可以大大降低I/O次数,从而提高数据访问性能.索引有很多种我们主要介绍常用的几种: 为什么添加了索引或,会加快查询速度呢? n 单列索引 单列索引是基 ...
- Python之整数类型
整数:18,73,84 每一个整数都有如下的功能:class int(object): """ int(x=0) -> int or long int(x, bas ...
- vue cli3 子目录问题
在使用 vue-cli3 build的时候,使用非子目录需要在 vue.config.js 中添加如下代码: module.exports = { baseUrl: process.env.NODE_ ...
- jq动态添加代码监听问题
$(document).on('click', '.class', function() { console.log($(this).attr('id')); });
- HDU 5572 An Easy Physics Problem【计算几何】
计算几何的题做的真是少之又少. 之前wa以为是精度问题,后来发现是情况没有考虑全... 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5572 题意: ...
- [HLSL]HLSL 入门参考 (dx11龙书附录B译文)
原文:[HLSL]HLSL 入门参考 (dx11龙书附录B译文) HLSL 高级着色语言 参考文档 龙书DirectX12现已推出中文版,其附录B的高级着色器语言参考的翻译质量比本文更高,有条件的读者 ...
- 用select提取List元素自身序号
var cs = currentCitys.Select((c, i) => new { id = c.CITY_ID, 序号 = (i + 1).ToString(), 城市类型 = c.IS ...