http://acm.hdu.edu.cn/showproblem.php?pid=4004

Problem Description

The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over the river. The width of the river is L (1<= L <= 1000000000). There are n (0<= n <= 500000) stones lined up in a straight line from one side to the other side of the river. The frogs can only jump through the river, but they can land on the stones. If they fall into the river, they 
are out. The frogs was asked to jump at most m (1<= m <= n+1) times. Now the frogs want to know if they want to jump across the river, at least what ability should they have. (That is the frog's longest jump distance).

Input

The input contains several cases. The first line of each case contains three positive integer L, n, and m. 
Then n lines follow. Each stands for the distance from the starting banks to the nth stone, two stone appear in one place is impossible.

Output

For each case, output a integer standing for the frog's ability at least they should have.

Sample Input

  1.  

Sample Output

  1.  

题意:

青蛙王国运动会开始了,最受欢迎的游戏是铁蛙三项赛,其中一项是跳跃过河项目。这个项目需要青蛙运动员通过跳跃过河。河的宽度是L。在河面上有直线排列的n个石头。青蛙可以利用这些石头跳跃过河,如果落入河中则失败。青蛙们能够跳跃的最多次数是m。现在想要知道青蛙们至少需要具备多大的跳跃距离,才能够顺利完成比赛。

思路:

用二分去查找一个单次跳跃的最大距离,看以这个距离能否完成。直到找到最小的那个数,二分的上边界是河水宽 L。

代码如下:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <string>
  5. #include <math.h>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <stack>
  9. #include <queue>
  10. #include <set>
  11. #include <map>
  12. #include <sstream>
  13. const int INF=0x3f3f3f3f;
  14. typedef long long LL;
  15. const int mod=1e9+;
  16. //const double PI=acos(-1);
  17. #define Bug cout<<"---------------------"<<endl
  18. const int maxn=5e5+;
  19. using namespace std;
  20.  
  21. int a[maxn];//存放每块石头到开始处的距离
  22. int b[maxn];//存放石头之间的差值
  23.  
  24. int main()
  25. {
  26. int L,n,m;
  27. while(~scanf("%d %d %d",&L,&n,&m))
  28. {
  29. for(int i=;i<=n;i++)
  30. scanf("%d",&a[i]);
  31. a[n+]=L;//最后要跳到河岸上
  32. sort(a+,a++n+);
  33. int MAX=;//答案不可能会小于石头差值中的最大值,否则这两块石头一定跳不过去
  34. for(int i=;i<=n+;i++)
  35. {
  36. b[i]=a[i]-a[i-];
  37. if(b[i]>MAX)
  38. MAX=b[i];
  39. }
  40. int l=MAX;
  41. int r=L;
  42. while(l<=r)//二分查找答案
  43. {
  44. int mid=(l+r)>>;//二分法的中值(即初始青蛙能跳的最大距离)
  45. int num=;//记录跳的步数
  46. for(int i=;i<=n+;)//此处用到贪心,一次尽量多跳几个石头
  47. {
  48. int sum=;//检验到这个石头需要跳的距离
  49. for(int j=i;j<=n+;j++)
  50. {
  51. if(sum+b[j]<=mid)//可以继续跳
  52. {
  53. sum+=b[j];
  54. if(j==n+)//跳到岸了,处理一下
  55. {
  56. num++;
  57. i=n+;
  58. break;
  59. }
  60. }
  61. else//不可以继续跳
  62. {
  63. num++;//跳的次数加1
  64. i=j;//下一次从这个石头起跳
  65. break;
  66. }
  67. }
  68. }
  69. if(num>m)
  70. l=mid+;
  71. else
  72. r=mid-;
  73. }
  74. printf("%d\n",l);
  75. }
  76. return ;
  77. }

HDU-4004 The Frog's Games (分治)的更多相关文章

  1. HDU 4004 The Frog's Games(二分答案)

    The Frog's Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) ...

  2. HDU 4004 The Frog's Games(二分+小思维+用到了lower_bound)

    The Frog's Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) ...

  3. hdu 4004 The Frog's Games

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4004 The annual Games in frogs' kingdom started again ...

  4. HDU 4004 The Frog's Games(二分)

    题目链接 题意理解的有些问题. #include <iostream> #include<cstdio> #include<cstring> #include< ...

  5. HDU 4004 The Frog's Games(2011年大连网络赛 D 二分+贪心)

    其实这个题呢,大白书上面有经典解法  题意是青蛙要跳过长为L的河,河上有n块石头,青蛙最多只能跳m次且只能跳到石头或者对面.问你青蛙可以跳的最远距离的最小值是多大 典型的最大值最小化问题,解法就是贪心 ...

  6. 杭电 4004 The Frog's Games 青蛙跳水 (二分法,贪心)

    Description The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog T ...

  7. The Frog's Games(二分)

    The Frog's Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) ...

  8. HDUOJ----4004The Frog's Games(二分+简单贪心)

    The Frog's Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) ...

  9. HDU 4004 二分

    The Frog's Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) ...

  10. The Frog's Games

    The Frog's Games Problem Description The annual Games in frogs' kingdom started again. The most famo ...

随机推荐

  1. PL/SQL表结构/数据的导出

    1.表结构导出 方法一:在sql页面点击表名,进入表编辑页面,点击右下角“查看SQL”按钮,即可看到表结构 方法二:工具--导出用户对象 去掉所有者勾选项,即***.表名:用户名字在其他用户导入数据库 ...

  2. Aduino Nano 技术性能指标

    纵览 在Adnuino Nano网站上节选了该控制器的价格等,在中国买非常便宜,我用10元左右的人民币就买到了这个产品,在Arduino网站上的价格是22美金,还不包括税.这种差别是如何造成的?是国外 ...

  3. bzoj 4236JOIOJI

    一开始忘掉特殊情况也是蛋疼2333(有一直到头的.mp[0][0]是要特判的) 做法也就是找mp[i][j]相同的东西.(貌似可以写成线性方程组(z=x+A,z=y+B)过这个的就是相等(可以先从2维 ...

  4. 使用Spring AOP实现MySql的读写分离

    转自:http://blog.csdn.net/xlgen157387/article/details/53930382 一.前言 分布式环境下数据库的读写分离策略是解决数据库读写性能瓶颈的一个关键解 ...

  5. 第二阶段scrum-3

    1.整个团队的任务量: 2.任务看板: 会议照片: 产品状态: 前端制作完成,数据库正在配置

  6. 吴裕雄--天生自然 JAVASCRIPT开发学习:语句

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. Ubuntu 16.04 上安装 CUDA 9.0 详细教程

    https://blog.csdn.net/QLULIBIN/article/details/78714596 前言: 本篇文章是基于安装CUDA 9.0的经验写,CUDA9.0目前支持Ubuntu1 ...

  8. 快速排序_python

    def quicksort(ls,result): if len(ls)<=1: result+=ls # return result else: flag=ls[0] left=[x for ...

  9. phpstorm 的下载、安装与激活

    1.phpstorm的下载地址 https://www.jetbrains.com/phpstorm/ 下载后的安装包如图: 2.phpstorm的安装过程 跟据电脑系统下载安装对应版本 一路点击下一 ...

  10. c#中的Task异步编程

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/index翻译 1. 引入 Task异步 ...