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

6 1 2
2
25 3 3
11
2
18
 

Sample Output

4
11
 

输入河宽L,石头数量N,步数M,在区间【1,L】内用二分法判断,算出区间里数作为一步的最大值时到对岸(最优解)需要多少的步数,若步数大于等于m,则记下步数,令右区间减一,否则令左区间加一,直到求出最小的能力。

 #include<cstdio>
#include<algorithm>
using namespace std;
int a[+];
int l,n,m,i,le,ri,mid,ans;
bool f(int k)      //返回值为真或假
{
int num=,sum=;
if(a[] > k)
return ;
for(i = ; i <= n ; i++)
{
if(a[i] - a[i-] > k)
return ;
if((a[i]-sum) > k)
{
num++;
sum=a[i-];
}
}
return (num+) <= m;
}
int main()
{
while(scanf("%d %d %d",&l,&n,&m)!=EOF)
{
for(i = ; i < n ; i++)
{
scanf("%d",&a[i]);
}
a[n]=l;
sort(a,a+n);
le=;
ri=l;
while(le <= ri)
{
mid=(le+ri)/;
if( f(mid))
{
ans=mid;
ri=mid-;
}
else
{
le=mid+;
}
}
printf("%d\n",ans);
}
}

 #include<cstdio>
#include<algorithm>
using namespace std;
int a[+];
int l,n,m,i,le,ri,mid,ans;
int f(int k)      //返回值为最大能力为k时的步数
{
int num=,sum=;
if(a[] > k)
return m+;
for(i = ; i <= n ; i++)
{
if(a[i] - a[i-] > k)
return m+;
if((a[i]-sum) > k)
{
num++;
sum=a[i-];
}
}
return num+;
}
int main()
{
while(scanf("%d %d %d",&l,&n,&m)!=EOF)
{
for(i = ; i < n ; i++)
{
scanf("%d",&a[i]);
}
a[n]=l;
sort(a,a+n);
le=;
ri=l;
while(le <= ri)
{
mid=(le+ri)/;
if( f(mid) <= m)
{
ans=mid;
ri=mid-;
}
else
{
le=mid+;
}
}
printf("%d\n",ans);
}
}
 

杭电 4004 The Frog's Games 青蛙跳水 (二分法,贪心)的更多相关文章

  1. 杭电oj 4004---The Frog Games java解法

    import java.util.Arrays; import java.util.Scanner; //杭电oj 4004 //解题思路:利用二分法查找,即先选取跳跃距离的区间,从最大到最小, // ...

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

    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(二分+小思维+用到了lower_bound)

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

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

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

  5. hdu 4004 The Frog's Games

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

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

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

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

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

  8. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  9. The Frog's Games

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

随机推荐

  1. Centos6.8 搭建Lvs+Keepalived

    Keepalived keepalived是一个类似于layer3, 4 & 7交换机制的软件,也就是我们平时说的第3层.第4层和第7层交换.Keepalived是自动完成,不需人工干涉. 简 ...

  2. bzoj 4860 [BeiJing2017]树的难题

    题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4860 题解 点分治 设当前重心为v 假设已经把所有边按照出发点第一关键字, 颜色第二关键字排 ...

  3. BZOJ1026(数位dp)

    数位dp第二道-就当成搜索,套板子写了写.我写的dp[pos][pre][state0]记录的是当前pos位没有限制时.前面的数是pre时.前面是否都是0时的方案数. #include <cst ...

  4. 素数+map BestCoder Round #54 (div.2) 1002 The Factor

    题目传送门 题意:给出一个数列,问数列的乘积的一个满足条件的最小因子是什么,没有输出-1.条件是不是素数 分析:官方题解:对于每一个数字,它有用的部分其实只有它的所有质因子(包括相等的).求出所有数的 ...

  5. 递推DP URAL 1244 Gentlemen

    题目传送门 /* 题意:给出少了若干卡片后的总和,和原来所有卡片,问少了哪几张 DP:转化为少了的总和是否能有若干张卡片相加得到,dp[j+a[i]] += dp[j]; 记录一次路径,当第一次更新的 ...

  6. 制作ubuntu启动U盘:Windows,Mac osx ,Ubuntu

    1.How to create a bootable USB stick on Windows https://www.ubuntu.com/download/desktop/create-a-usb ...

  7. android动画(1)各种动画属性表,简单代码,xml配置

    1.动画的属性表 1.1 常用属性 它们可以通过代码setXXX,getXXX,也可在xml中配置. 属性[类型] 功能 备注 Duration[long] 属性为动画持续时间 时间以毫秒为单位 fi ...

  8. PowerShell~文件操作和对象遍历

    ps提供了丰富的文件操作,如建立,删除,改名,移动,复制,文件夹建立,显示文件列表,同时对数组对象的遍历也很方便,如果在使用PS脚本时,希望现时传入参数,可以把参数声明为param,当然需要把它写在文 ...

  9. List<DTO>转 Map<String,List<DTO>> 两种写法

    List<TeamScheduleDTO> list = JSON.parseArray(response.getData().getJSONArray("list") ...

  10. MyBatis使用懒加载mybatis-config.xml配置

    在mybatis-config.xml添加如下配置 <settings> <!--要使延迟加载生效必须配置下面两个属性--> <setting name="la ...