hdu4004 Frog’s Game【二分+贪心】
来源:互联网 发布:unix远程登录软件 编辑:程序博客网 时间:2024/06/06 00:09
The Frog's Games
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 5667 Accepted Submission(s): 2724
Problem 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).
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.
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 2225 3 311 218
Sample Output
411
开始的想法是递归做……
然而题目规模这么大,必然是TLE的
百度了一下…
因为这个题规模真的不小…L<=1000000000
所以呢,是可以用二分的
对步长进行二分并带入判断。
判断的时候用到了贪心,尽量调到最远的距离
判断函数:
1.判断这个步长能不能跳过去,M*i<L?
2.从1开始判断。
2.0 步数++;
2.1 步长小于相邻两个石头的距离,return false;
2.2 从当前的步循环判断可以跳到的最远石头。
2.3 跳(变当前坐标)
2.4 判断步数够不够。
#include <cstdio>#include <algorithm>using namespace std;#define maxn 500005int L,N,M;int dis[maxn];bool check(int x){//1.判断步长是否可以 if(x*M<L){return false;} int step=0, i=1, pre=0;while(i<=N+1){//2.从头开始跳跃step++;if(x<dis[i]-dis[i-1])return false;//不能跳过去 while(i<=(N+1)&&x>=dis[i]-dis[pre]) i++;//找能跳到的最远距离 pre=i-1;//贪心 if(step>M) return false;}return true; }int main(){while(scanf("%d%d%d",&L,&N,&M)!=EOF){dis[0]=0;for(int i=1;i<=N;i++){scanf("%d",&dis[i]);} dis[N+1]=L;sort(dis+1,dis+N+1);int l=0,r=L;while(l<=r){int mid=(l+r)/2;if(check(mid)){//可以缩小距离r=mid-1; }else{//不可以就扩大距离 l=mid+1;}}printf("%d\n",l);}return 0;}
开始的想法是递归做……
然而题目规模这么大,必然是TLE的
我的渣代码也先放上来……
//TLE的代码……
#include <cstdio>#include <algorithm>using namespace std;#define inf 1000000002#define maxn 500005#define max(x,y) x>y?x:y#define min(x,y) x<y?x:ylong A[maxn];long L,n,M,ans;void fun(long last,long now,long m){if(m>min) return ;//剩余,目前坐标,最大步数 if(last==0&&now!=n){//找不到 return;}else if(now==n){ans=min(ans,m);} for(long k=1;now+k<=n;k++){m=max(m,A[now+k]-A[now]);fun(last-1,k+now,m);}} int main(){while(scanf("%ld%ld%ld",&L,&n,&M)!=EOF){min=inf; for(long i=0;i<n;i++){scanf("%ld",&A[i]);}sort(A,A+n);A[n]=L;ans=inf;for(long j=0;j<=n;j++){fun(M-1,j,A[j]);}printf("%ld\n",ans);}return 0;}
百度了一下…
因为这个题规模真的不小…L<=1000000000
所以呢,是可以用二分的
对步长进行二分并带入判断。
判断的时候用到了贪心,尽量调到最远的距离
判断函数:
1.判断这个步长能不能跳过去,M*i<L?
2.从1开始判断。
2.0 步数++;
2.1 步长小于相邻两个石头的距离,return false;
2.2 从当前的步循环判断可以跳到的最远石头。
2.3 跳(变当前坐标)
2.4 判断步数够不够。
#include <cstdio>#include <algorithm>using namespace std;#define maxn 500005int L,N,M;int dis[maxn];bool check(int x){//1.判断步长是否可以 if(x*M<L){return false;} int step=0, i=1, pre=0;while(i<=N+1){//2.从头开始跳跃step++;if(x<dis[i]-dis[i-1])return false;//不能跳过去 while(i<=(N+1)&&x>=dis[i]-dis[pre]) i++;//找能跳到的最远距离 pre=i-1;//贪心 if(step>M) return false;}return true; }int main(){while(scanf("%d%d%d",&L,&N,&M)!=EOF){dis[0]=0;for(int i=1;i<=N;i++){scanf("%d",&dis[i]);} dis[N+1]=L;sort(dis+1,dis+N+1);int l=0,r=L;while(l<=r){int mid=(l+r)/2;if(check(mid)){//可以缩小距离r=mid-1; }else{//不可以就扩大距离 l=mid+1;}}printf("%d\n",l);}return 0;}
0 0
- hdu4004 Frog’s Game【二分+贪心】
- hdu4004 The Frog's Games 二分
- [二分答案]HDU4004 The Frog's Games
- HDU4004 The Frog's Games(二分)
- hdu4004 The Frog's Games 二分查找+贪心 TWT Tokyo Olympic 1COMBO-2
- hdu4004 The Frog's Games
- hdu4004(二分+贪心)
- hdu4004 简单二分+贪心
- 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 (二分+贪心)
- HDU 4004 The Frog's Games (二分贪心)
- [HDU 4004] The Frog's Games 二分+贪心
- 最新百度开放云平台应用引擎BAE空间使用教程
- PathRemoveFileSpec
- 智能聊天机器人小黄鸡及其制作方法
- 数据持久化
- SimpleDateFormat的线程安全问题与解决方案
- hdu4004 Frog’s Game【二分+贪心】
- Linux之将多个文件里的内容按行方式依次添加到文件Z的末尾
- Java-文件拷贝(字节流)
- 顺序表应用2:多余元素删除之建表算法
- 我眼中的Linux设备树(一 概述)
- C#不安全代码只会在使用 /unsafe 编译的情况下出现
- vim 输入一对尖括号 <>时,怎样使光标自动移到括号中间
- web动画的方式requestAnimationFrame
- lintcode ----合并排序数组