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


Sample Output


题意:

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

思路:

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

代码如下:

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
//const double PI=acos(-1);
#define Bug cout<<"---------------------"<<endl
const int maxn=5e5+;
using namespace std; int a[maxn];//存放每块石头到开始处的距离
int b[maxn];//存放石头之间的差值 int main()
{
int L,n,m;
while(~scanf("%d %d %d",&L,&n,&m))
{
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
a[n+]=L;//最后要跳到河岸上
sort(a+,a++n+);
int MAX=;//答案不可能会小于石头差值中的最大值,否则这两块石头一定跳不过去
for(int i=;i<=n+;i++)
{
b[i]=a[i]-a[i-];
if(b[i]>MAX)
MAX=b[i];
}
int l=MAX;
int r=L;
while(l<=r)//二分查找答案
{
int mid=(l+r)>>;//二分法的中值(即初始青蛙能跳的最大距离)
int num=;//记录跳的步数
for(int i=;i<=n+;)//此处用到贪心,一次尽量多跳几个石头
{
int sum=;//检验到这个石头需要跳的距离
for(int j=i;j<=n+;j++)
{
if(sum+b[j]<=mid)//可以继续跳
{
sum+=b[j];
if(j==n+)//跳到岸了,处理一下
{
num++;
i=n+;
break;
}
}
else//不可以继续跳
{
num++;//跳的次数加1
i=j;//下一次从这个石头起跳
break;
}
}
}
if(num>m)
l=mid+;
else
r=mid-;
}
printf("%d\n",l);
}
return ;
}

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. Android-寒假学习-阶段总结(20集)-口算测试APP

    说在前面: 1.视频教程:https://www.bilibili.com/video/av60445113/?spm_id_from=333.788.videocard.0 2.老师的源码:http ...

  2. 2016蓝桥杯省赛C/C++A组第九题 密码脱落

    题意: X星球的考古学家发现了一批古代留下来的密码. 这些密码是由A.B.C.D 四种植物的种子串成的序列. 仔细分析发现,这些密码串当初应该是前后对称的(也就是我们说的镜像串). 由于年代久远,其中 ...

  3. jsp页面引入不了js路径没错

    最近搞开发,发现有个jsp页面引入不了js:很是神奇,路径什么的都没问题,同事的浏览器可以加载该js,发现放到其他的文件夹下可以加载该js:当时没研究出来,任务紧就没研究了. 最近闲下来了,有去研究, ...

  4. Swift 3 :基于 AVAudioPlayer 的简单音乐播放器

    2017.05.22 17:46* 字数 1585 阅读 5095评论 0喜欢 8赞赏 2 https://www.jianshu.com/p/4d5c257428a1 学习ios以来差不多接近两个月 ...

  5. CSS属性之float浮动属性

    float 属性定义元素在哪个方向浮动.以往这个属性总应用于图像,使文本围绕在图像周围,不过在 CSS 中,任何元素都可以浮动.浮动元素会生成一个块级框,而不论它本身是何种元素. float有四个属性 ...

  6. 微服务基础——厉害了!API网关

    微服务刚刚诞生的时候,人们将服务进行拆分,实现服务之间的松耦合,并且每个服务有专门的团队维护,然后客户端直接和各个子服务进行交互.比如,订单,商品,会员服务. 那么这种客户端直接和后端服务交互的方式会 ...

  7. DispatcherServlet继承体系

    GenericServlet                 implements Servlet, ServletConfig, java.io.Serializable | HttpServlet ...

  8. 自定义View不显示的问题

    问题描述: 我自定义了一个把 SwipeRefreshLayout 和 RecyclerView 封装在一起的 View ,但是发现 List 不能正常的显示出来,本以为是数据源出现问题,debug了 ...

  9. eclipse导入tomcat源码

    我的开发环境:windows7  64位 一.官网下载tomcat源码.在此奉上一站地址:http://archive.apache.org/dist/tomcat/: 二.编译源码生成.jar文件: ...

  10. UML-设计模式-本地服务容错-适配器+工厂模式

    问题1:我们的ProductCatalog存储在了数据库里了,但是数据库瘫掉了,怎么办? 解决:本地(Map)---->Local(文件)---->DB 问题2:如果新加了存储Produc ...