River Hopscotch

Time Limit: 2000MS Memory Limit: 65536K

Total Submissions: 17740 Accepted: 7414

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

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

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).


解题心得:

  1. 有n块石头排成一列,每个石头距离原点有一个距离,现在要移走m块石头,问这些牛要跳在(n-m) 块石头上,怎么移走石头让牛每次跳的最短距离最大。输入最短的最大距离。
  2. 看到这个最短距离最大其实也就想到二分了。首先要明白(n-m)块石头,牛要跳n-m+2次(起点和终点没有石头但是也要跳)。然后二分检查。

  1. #include <algorithm>
  2. #include <stdio.h>
  3. #include <cstring>
  4. using namespace std;
  5. const int maxn = 5e5+100;
  6. int pos[maxn],n,m,l,tot;
  7. void init() {
  8. pos[0] = 0;
  9. for(int i=1;i<=n;i++)
  10. scanf("%d",&pos[i]);
  11. pos[n+1] = l;
  12. n += 2;
  13. sort(pos,pos+n);
  14. tot = n-m;
  15. }
  16. bool checke(int len){
  17. int cnt = 1,now = pos[0];
  18. for(int i=1;i<n;i++) {
  19. if(pos[i] - now >= len) {
  20. cnt++;
  21. now = pos[i];
  22. }
  23. }
  24. if(cnt >= tot)
  25. return true;
  26. return false;
  27. }
  28. int binary_search() {
  29. int l = 0,r = 1e9+100;
  30. while(r - l > 1) {
  31. int mid = (l + r) / 2;
  32. if(checke(mid))
  33. l = mid;
  34. else
  35. r = mid;
  36. }
  37. return l;
  38. }
  39. int main() {
  40. while(scanf("%d%d%d",&l,&n,&m) != EOF) {
  41. init();
  42. int ans = binary_search();
  43. printf("%d\n",ans);
  44. }
  45. return 0;
  46. }

POJ:3258-River Hopscotch的更多相关文章

  1. 二分搜索 POJ 3258 River Hopscotch

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

  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

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

  4. POJ 3258 River Hopscotch (binarysearch)

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

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

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

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

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

  7. POJ 3258 River Hopscotch 二分枚举

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

  8. POJ 3258 River Hopscotch(二分法搜索)

    Description Every year the cows hold an event featuring a peculiar version of hopscotch that involve ...

  9. poj 3258 River Hopscotch 题解

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

  10. POJ 3258 River Hopscotch (二分法)

    Description Every year the cows hold an event featuring a peculiar version of hopscotch that involve ...

随机推荐

  1. vueHistory 模式下,布置到服务器上路由刷新会报nginx404错误

    之前写完vue项目后,布置到服务器,用nginx反向代理后,一开始进去,进各种路由都是没问题的,但是一旦f5刷新后就会出现一个nginx404的错误. 经过翻阅vue文档后,发现这是vueHistor ...

  2. pm2的的常用命令及用法

    使用pm2启动静态文件服务器的方法如下: pm2 serve path port pm2 serve . 9001 这样就可以把当前文件夹下的静态文件跑起来了,而且端口号是9001, 同样也支持进阶的 ...

  3. CSS基础必备盒模型及清除浮动

    盒模型 盒模型是有两种标准的,一个是标准模型,一个是IE模型. css如何设置两种模型 这里用到了CSS3 的属性 box-sizing /* 标准模型 */ box-sizing:content-b ...

  4. ArcGIS 10.2之地图服务的发布、使用

    2.发布地图服务 2.1 地图服务器的建立 打开ArcCatalog,在左侧的GIS Servers下,双击Add ArcGIS Server, 弹出添加界面,选择,Administer GIS服务项 ...

  5. What is mobile platform?

    高屋建瓴 From Up to Down Outside into inside The Internet Of Things. http://wenku.baidu.com/view/5cdc026 ...

  6. 在C++Builder中定义事件的实现方法

    ++Builder是由Borland公司推出的一款可视化集成开发工具.C++Builder的集成开发环境(IDE)提供了一系列可视化快速应用程序开发(RAD)工具,让程序员可以很轻松地建立和管理自己的 ...

  7. 关于安卓手机访问一些网站或者Fiori应用弹出安装证书的提示

    有朋友问遇到在安卓手机上安装Fiori Client,打开的时候提示需要安装证书,如下图所示: 我在自己的Android手机试了试,因为我没有装Fiori Client,所以就用手机浏览器直接访问ht ...

  8. MD5的32位加密方法

    /// <summary> /// MD532位加密方式 /// </summary> /// <param name="str">用户原始密码 ...

  9. Spring Security 之Session管理配置

    废话不多说,直接上代码.示例如下: 1.   新建Maven项目  session 2.   pom.xml <project xmlns="http://maven.apache.o ...

  10. 谈谈JDK8中的字符串拼接

    字符串拼接问题应该是每个Java程序员都熟知的事情了,几乎每个Java程序员都读过关于StringBuffer/StringBuilder来拼接字符串. 在大多数的教程中,也许你会看到用+号拼接字符串 ...