HDU 4004 The Frog's Games(2011年大连网络赛 D 二分+贪心)
其实这个题呢,大白书上面有经典解法
题意是青蛙要跳过长为L的河,河上有n块石头,青蛙最多只能跳m次且只能跳到石头或者对面。问你青蛙可以跳的最远距离的最小值是多大
典型的最大值最小化问题,解法就是贪心二分。其实就是二分答案,再把每次二分出来的答案带入计算是否满足题意,以此来确定这个正确答案在此值的左区间还是右区间。可以这么做的依据是:对于二分出来的值x满足条件的话,则大于x(或者小于x)也满足条件,这样就得到一个单调的规律,满足二分。
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1E-8
/*注意可能会有输出-0.000*/
#define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
#define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
#define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
#define mul(a,b) (a<<b)
#define dir(a,b) (a>>b)
typedef long long ll;
typedef unsigned long long ull;
const int Inf=<<;
const double Pi=acos(-1.0);
const int Max=;
int sto[Max],dis[Max];
int Solve(int n,int ll)//判断情况
{
int ans=,sum=;
for(int i=;i<n;i++)
{
if(sum+dis[i]>ll)
{
ans++;
sum=dis[i];
}
else
sum+=dis[i];
}
ans++;
return ans;
}
int Dic(int l,int n,int m,int manx)//二分
{
int sma=manx,big=l;
while(sma<big)
{
int mid=((big+sma)>>);
if(Solve(n,mid)<=m)
big=mid;
else
sma=mid+;
}
return big;
}
int main()
{
int l,n,m;
while(~scanf("%d %d %d",&l,&n,&m))
{
sto[]=;
for(int i=; i<=n; i++)
scanf("%d",&sto[i]);
n++;
sto[n++]=l;
sort(sto,sto+n);
int nn=;
for(int i=; i<n; i++)
if(sto[i]!=sto[nn])
sto[++nn]=sto[i];
n=++nn;
int manx=;
for(int i=; i<n-; i++)
{
dis[i]=sto[i+]-sto[i];
manx=max(manx,dis[i]);
}
n--;
printf("%d\n",Dic(l,n,m,manx));
}
return ;
}
HDU 4004 The Frog's Games(2011年大连网络赛 D 二分+贪心)的更多相关文章
- HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)
HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others) Memory Limit: ...
- 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 4033 2011成都赛区网络赛 余弦定理+二分 **
二分边长,判断最后内角和是否为2pi,注意l与r的选取,保证能组成三角形 #include<cstdio> #include<iostream> #include<alg ...
- HDU 4001 To Miss Our Children Time(2011年大连网络赛 A 贪心+dp)
开始还觉得是贪心呢... 给你三类积木叫你叠楼房,给你的每个积木包括四个值:长 宽(可以互换) 高 类型d d=0:你只能把它放在地上或者放在 长 宽 小于等于 自己的积木上面 d=1:你只能把它放在 ...
- HDU 5884 Sort(2016年青岛网络赛 G 二分+贪心+小优化)
好题 题意:给你n<=100000个数,每个数范围[0,1000],然后给你一个最大的代价T,每次最多合并k个数成为一个数,代价为k个数的总和.问最后合成1个数的总代价不大于T的最小k 题解:我 ...
- HDU 5869 Different GCD Subarray Query(2016大连网络赛 B 树状数组+技巧)
还是想不到,真的觉得难,思路太巧妙 题意:给你一串数和一些区间,对于每个区间求出区间内每段连续值的不同gcd个数(该区间任一点可做起点,此点及之后的点都可做终点) 首先我们可以知道每次添加一个值时gc ...
随机推荐
- Nginx服务监听端口修改启动bug
监听的端口从80 修改到其他端口出现启动不起来问题. 解决方案如下: yum install policycoreutils-python sudo cat /var/log/audit/audit. ...
- cvs 文件如何解析?
QList<QStringList> RwCvs::readToList(const QString &path, const QString separator) { QList ...
- CentOS 7.4 下安装Epel源和Nginx
EPEL (Extra Packages for Enterprise Linux)是基于Fedora的一个项目,为“红帽系”的操作系统提供额外的软件包,适用于RHEL.CentOS和Scientif ...
- 启动 nodemanger 报错javax.security.sasl.SaslException: GSS initiate failed
最近启动 Hadoop, nodemanger 老挂,报kerberos 验证错误,各种查找原因,时间也同步,kint 也能登录到kerberos,一直找不到原因,最后发现是网关和远端的时间同步,但是 ...
- SpringBoot连接PostgreSQL
这个 org.postgresql.jdbc.PgConnection.createClob() 方法尚未被实作 application.properties spring.datasource.pl ...
- Geforce experience报错:something went wrong try restarting geforce
右键计算机 ——>管理——> 服务和应用程序 ——>服务中
- F110操作手册-自动付款
SAP 系统 F110系统操作手册 目 录 1.自动付款... 3 1.自动付款 事务代号: F110 菜单路径: 会计 →财 ...
- loadrunder之脚本篇——加密解密
密码加密 可以给密码加密,意在把结果字符串作为脚本的参数或者参数值.例如,完整可能有一个用户密码填写的表单,你想测试网站针对不同密码的反应,但是你又想保护密码的安全.Password Encoder允 ...
- C#中往数据库插入空值报错解决方法
C#中的NUll于SQL中的null是不一样的, SQL中的null用C#表示出来就是DBNull.Value 在用C#往数据库里面插入记录的时候, 可能有的字段你不赋值,那么这个字段的值就为null ...
- Linux Shell基础 多个命令中的分号(;)、与(&&) 、 或(||)
概述 在 Bash 中,如果需要让多条命令按顺序执行,则有这样方法,如表 1 所示. 多命令执行符 格 式 作 用 : 命令1 ; 命令2 多条命令顺序执行,命令之间没有任何逻辑关系 &&am ...