HDU 4004 The Frog's Games (二分 + 贪心 青蛙跳石头)
来源:互联网 发布:男生最喜欢的姿势知乎 编辑:程序博客网 时间:2024/06/04 20:25
题意:河长L,n个石头,青蛙最多跳m次,给出n个石头在数轴上的位置,求出青蛙通过石头可以跳过河的最小跳跃能力(跳跃距离)。
思路:二分石头间的距离。
#include <iostream>#include <cstdio>#include <algorithm>#include <set>#include <string>#include <cstring>#include <cmath>using namespace std;typedef long long ll;int str[500005];int len,n,m;bool cal(int mid){ if(mid*m<len) return false; int i=0,cnt=0; for(int j=1;j<=n+1;j++){ if(str[j]-str[i]>mid) if(j==i+1) return false; else{ --j;//贪心,让每一次跳得尽可能远, i=j;//由于为下一次循环做准备时要执行循环条件的第三部分j++,所以此处先--j。 cnt++; //例:1跳到3 再从3跳到后面,此处就是对3的处理 } } cnt++; if(cnt>m) return false; return true;}int main(){ while(scanf("%d%d%d",&len,&n,&m)==3) { for(int i = 1; i <= n; i++) scanf("%d", &str[i]); str[n+1] = len; sort(str+1,str+n+1); int l = 0,r = len,mid; while(l<=r) { mid=(l+r)/2; if(cal(mid)) r=mid-1; else l=mid+1; } printf("%d\n",l); } return 0;}
阅读全文
0 0
- HDU 4004 The Frog's Games (二分 + 贪心 青蛙跳石头)
- HDU 4004 The Frog's Games(青蛙过河,二分)
- hdu 4004 The Frog's Games ( 二分+贪心 )
- HDU 4004 The Frog's Games (二分贪心)
- HDU - 4004 The Frog's Games (二分)去掉m个石头求最小间距
- HDU 4004 The Frog's Games 二分+贪心
- hdu 4004 The Frog's Games (二分+贪心)
- hdu 4004 二分+ 贪心 The Frog's Games
- hdu 4004 The Frog's Games (二分搜索 + 贪心)
- HDU 4004 The Frog's Games 二分+贪心
- hdu 4004 The Frog's Games (二分+贪心)
- [HDU 4004] The Frog's Games 二分+贪心
- hdu 4004 The Frog's Games 二分+贪心
- 4004 The Frog's Games --二分+贪心
- HDU 4004 THE FROG’S GAMES(二分)
- HDU 4004 The Frog's Games(二分)
- HDU 4004 The Frog's Games(二分)
- hdu 4004 The Frog's Games【二分】
- 计算一个数组的长度
- 从普通JAVA程序员到阿里架构师,他用了六年 JAVA架构演变之路
- UML类图中方法(操作)的表示格式
- 数据库系统概述
- 洛谷 [模板]最小生成树
- HDU 4004 The Frog's Games (二分 + 贪心 青蛙跳石头)
- 百万学子明天勇闯数学考研关,考研趣闻
- WPF中当资源之间相互依赖(xaml资源与类对象资源相互依赖)时,常见无法加载的问题
- servlet&JSP
- servlet&JSP
- youtobe视频随便下,不用跳墙
- iOS与h5交互
- OOP特征
- Ubuntu 14搭建Nginx-RTMP流媒体服务器