HDU 4004 The Frog's Games(2011年大连网络赛 D 二分+贪心)
2016-08-31 20:21
381 查看
其实这个题呢,大白书上面有经典解法
题意是青蛙要跳过长为L的河,河上有n块石头,青蛙最多只能跳m次且只能跳到石头或者对面。问你青蛙可以跳的最远距离的最小值是多大
典型的最大值最小化问题,解法就是贪心二分。其实就是二分答案,再把每次二分出来的答案带入计算是否满足题意,以此来确定这个正确答案在此值的左区间还是右区间。可以这么做的依据是:对于二分出来的值x满足条件的话,则大于x(或者小于x)也满足条件,这样就得到一个单调的规律,满足二分。
题意是青蛙要跳过长为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=1<<28; const double Pi=acos(-1.0); const int Max=500010; int sto[Max],dis[Max]; int Solve(int n,int ll)//判断情况 { int ans=0,sum=0; for(int i=0;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)>>1); if(Solve(n,mid)<=m) big=mid; else sma=mid+1; } return big; } int main() { int l,n,m; while(~scanf("%d %d %d",&l,&n,&m)) { sto[0]=0; for(int i=1; i<=n; i++) scanf("%d",&sto[i]); n++; sto[n++]=l; sort(sto,sto+n); int nn=0; for(int i=1; i<n; i++) if(sto[i]!=sto[nn]) sto[++nn]=sto[i]; n=++nn; int manx=0; for(int i=0; i<n-1; i++) { dis[i]=sto[i+1]-sto[i]; manx=max(manx,dis[i]); } n--; printf("%d\n",Dic(l,n,m,manx)); } return 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 (二分 + 贪心 青蛙跳石头)
- 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(2011年大连网络赛 D 二分+贪心)
- 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 (二分 + 贪心 青蛙跳石头)
- 【杭电oj】4004 - The Frog's Games(STL & 二分 & 贪心)
- 【HDU 4004 The Frog's Games】+ 二分
- hdu 4004 The Frog's Games (二分+贪心)
- hdu 4004 The Frog's Games ( 二分+贪心 )
- HDU 4004 The Frog's Games(二分)