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. hdu1856 More is better 基础并查集

    #include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> ...

  2. Histogram LightOJ - 1083

    Histogram LightOJ - 1083 题意:给出一个直方图,由n个长条组成,它们的x轴上坐标分别为1-n,读入n之后读入的一行中,第i个表示x轴上坐标为i的长条长度.求直方图最大的正方形面 ...

  3. ACM_LRU页面置换算法

    LRU页面置换算法 Time Limit: 2000/1000ms (Java/Others) Problem Description: sss操作系统没听课, 这周的操作系统作业完全不会, 你能帮他 ...

  4. Backbone.js入门教程第二版笔记(2)

    关于手动触发router,之前看到的例子都是通过在url后面加上#xxx或者点击一个a链接方法来触发, 还有一种情况是通过触发一种规则,来触发另一种规则(表述无能),比如这个例子中,我在url后面加上 ...

  5. solr 常见异常

    solr4.3本地数据提交异常分析 (2013-06-19 16:03:15) 转载▼   异常一. Exception in thread "main" java.lang.No ...

  6. 外文翻译 《How we decide》多巴胺的预言 第三节

    这是第二章的最后一节. 书的导言 本章第一节 本章第二节 本节阅读感言:自我批评是自我提升的妙方. 多巴胺是我们感情的源泉.多巴胺相关的神经系统在不断的记录着我们主观意识没有注意到的一个个模式,将它们 ...

  7. AJPFX对选择和冒泡两种排序的理解

    冒泡排序和直接选择排序都是排序中比较简单和容易实现的算法,先简单说说两者的区别:先以按照元素从小到大为:冒泡排序:将相邻元素两两比较,如果有比较大的,就把比较大的放在右边,这样的结果就是一轮排序完毕后 ...

  8. 【转】深入理解Android中的SharedPreferences

    SharedPreferences作为Android中数据存储方式的一种,我们经常会用到,它适合用来保存那些少量的数据,特别是键值对数据,比如配置信息,登录信息等.不过要想做到正确使用SharedPr ...

  9. 将vue-cli项目配置在nginx上

    登录使用的是node.js搭建的注册登录接口,关于对数据库的读写则是用spring boot的框架来实现的. 1.首先是vue-cli项目里的前端页面的配置: location / { root    ...

  10. js中传统事件绑定模拟现代事件处理

    大家都知道,IE中的现代事件绑定(attachEvent)与W3C标准的(addEventListener)相比存在很多问题, 例如:内存泄漏,重复添加事件并触发的时候是倒叙执行等. 下面是用传统事件 ...