HDU-4004 The Frog's Games (分治)
http://acm.hdu.edu.cn/showproblem.php?pid=4004
Problem 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。现在想要知道青蛙们至少需要具备多大的跳跃距离,才能够顺利完成比赛。
思路:
用二分去查找一个单次跳跃的最大距离,看以这个距离能否完成。直到找到最小的那个数,二分的上边界是河水宽 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 (分治)的更多相关文章
- 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) ...
- 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(二分)
题目链接 题意理解的有些问题. #include <iostream> #include<cstdio> #include<cstring> #include< ...
- HDU 4004 The Frog's Games(2011年大连网络赛 D 二分+贪心)
其实这个题呢,大白书上面有经典解法 题意是青蛙要跳过长为L的河,河上有n块石头,青蛙最多只能跳m次且只能跳到石头或者对面.问你青蛙可以跳的最远距离的最小值是多大 典型的最大值最小化问题,解法就是贪心 ...
- 杭电 4004 The Frog's Games 青蛙跳水 (二分法,贪心)
Description The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog T ...
- The Frog's Games(二分)
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 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) ...
- The Frog's Games
The Frog's Games Problem Description The annual Games in frogs' kingdom started again. The most famo ...
随机推荐
- EF Core的安装及入门
一.环境准备 1.开发环境:.NET Core 3.1 2.IDE工具:Visual Studio 2019 3.数据库:SQL Server 2012 二.EF Core的安装 1.新建一个项目,如 ...
- hash表系列(转)
http://www.cnblogs.com/mumuxinfei/p/4441826.html 前言: 我以前在百度的mentor, 在面试时特喜欢考察哈希表. 那时的我满是疑惑和不解, 觉得这东西 ...
- 解决TeamViewer提示商业用途
安装此插件 提取码:i8o3
- 阿里P7Java最全面试296题:阿里天猫、蚂蚁金服含答案文档解析
[阿里天猫.蚂蚁.钉钉面试专题题目加答案] 不会做别着急:文末有答案以及视频讲解,架构师资料 1. junit用法,before,beforeClass,after, afterClass的执行顺序 ...
- linux_c_tcp_example
server #include <stdio.h> #include <stdlib.h> #include <string.h> #include <uni ...
- 【php】Swoole之php高性能通信框架
Swoole介绍 swoole是由c语言开发的异步网络通信引擎,被编译为so文件(swoole.so)作为php的extesion扩展. 与其他普通扩展不同: 与普通的扩展不同的是普通的扩展只是提供一 ...
- 19 01 04 CSS3 圆角 grba(带通明的) tansition动画 transform变换 animation动画
CSS3圆角 设置某一个角的圆角,比如设置左上角的圆角:border-top-left-radius:30px 60px; 同时分别设置四个角: border-radius:30px 60px 120 ...
- Vue框架的介绍及使用
Vue框架 定义:渐进式 JavaScript 框架 渐进式:可以控制一个页面的一个标签,可以控制一系列标签,也可以控制整个页面,甚至可以控制整个前台项目. 通过对框架的了解与运用程度,来决定其在整个 ...
- Cracking Digital VLSI Verification Interview 第一章
目录 Digital Logic Design Number Systems, Arithmetic and Codes Basic Gates Combinational Logic Circuit ...
- java线程——线程局部变量
一,线程局部变量ThreadLocal的作用 用于实现线程内部的数据共享,既对于相同的程序代码,多个模块在同一个线程中运行时要共享一份数据,在另一个线程访问的时候,访问的由是另一份数据. 每个线程调用 ...