杭电 4004 The Frog's Games 青蛙跳水 (二分法,贪心)
Description
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
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
Sample Input
Sample Output
输入河宽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 青蛙跳水 (二分法,贪心)的更多相关文章
- 杭电oj 4004---The Frog Games java解法
import java.util.Arrays; import java.util.Scanner; //杭电oj 4004 //解题思路:利用二分法查找,即先选取跳跃距离的区间,从最大到最小, // ...
- HDU 4004 The Frog's Games(二分答案)
The Frog's Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) ...
- 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) ...
- HDUOJ----4004The Frog's Games(二分+简单贪心)
The Frog's Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) ...
- hdu 4004 The Frog's Games
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4004 The annual Games in frogs' kingdom started again ...
- HDU 4004 The Frog's Games(2011年大连网络赛 D 二分+贪心)
其实这个题呢,大白书上面有经典解法 题意是青蛙要跳过长为L的河,河上有n块石头,青蛙最多只能跳m次且只能跳到石头或者对面.问你青蛙可以跳的最远距离的最小值是多大 典型的最大值最小化问题,解法就是贪心 ...
- HDU 4004 The Frog's Games(二分)
题目链接 题意理解的有些问题. #include <iostream> #include<cstdio> #include<cstring> #include< ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- The Frog's Games
The Frog's Games Problem Description The annual Games in frogs' kingdom started again. The most famo ...
随机推荐
- Java笔记-序列化的注意点
1.使用serialVersionUID 在Eclipse中,如果一个类实现了Serializable接口,且没有给这个类设置一个serialVersionUID,就会有一个警告标志: The ser ...
- 写一个类继承socket时遇到的问题(TCP)
主要为题出在服务器端的accept()函数,他返回两个参数,一个套接字和一个客户端的ip和端口组成的元组. 问题就出在这个套接字这里,我们继承了socket这个类,这个套接字创建的时候是通过socke ...
- Salazar Slytherin's Locket CodeForces - 855E
Salazar Slytherin's Locket CodeForces - 855E http://www.cnblogs.com/ftae/p/7590187.html 数位dp: http:/ ...
- 中国剩余定理 POJ 1006 Biorhythms
题目传送门 题意:POJ有中文题面 分析:其实就是求一次同余方程组:(n+d)=p(%23), (n+d)=e(%28), (n+d)=i(%33),套用中国剩余定理模板 代码: /********* ...
- GDI双缓冲绘图
一.简介 在进行复杂图形绘制时,若直接在屏幕DC上进行绘制,则会出现明显的闪烁.闪烁产生的原因是当绘制的图形较为 复杂时,图形绘制过程中就被刷新到屏幕上,导致结果断断续续地显示出来.双缓冲绘图的原理是 ...
- 百度地图API简单初始化
<script src="http://api.map.baidu.com/api?key=&v=2.0&ak=youkey"></script& ...
- AJPFX总结I/O流操作(二)
FileWriter:该类没有特有的方法只有自己的构造函数.该类特点在于1,用于处理文本文件.2,该类中有默认的编码表,3,该类中有临时缓冲.构造函数:在写入流对象初始化时,必须要有一个存储数据的目的 ...
- Android程序打包为APK
Andriod安装包文件(Android Package),简称APK,后缀名为.apk. 1.生成未签名的安装包 Build -> Build Bundle(s)/APK(s) -> B ...
- Android开发实现高德地图定位
1.获取Key 参考官方文档:http://lbs.amap.com/api/android-location-sdk/guide/create-project/get-key 对于签名文件的获取建议 ...
- 解决Ueditor在bootstarp 模态框中全屏问题
基本的一些配置就不说了.先说一下要注意的问题:首先是zIndex的设置.记住最好都显示设置模态框和ueditor的zIndex.理清他们的层叠关系. 特别是用到ueditor里面的图片上传功能的更要设 ...