HDU 4004 The Frog's Games(二分)
2014-03-01 10:41
411 查看
http://acm.hdu.edu.cn/showproblem.php?pid=4004
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 3072 Accepted Submission(s): 1520
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).
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.
Output
For each case, output a integer standing for the frog's ability at least they should have.
Sample Input
Sample Output
Source
The 36th ACM/ICPC
Asia Regional Dalian Site —— Online Contest
青蛙过河,总共距离为L,河中有石,知道石头离起点的距离,青蛙跳m次到对岸,求青蛙最小的最大跳跃距离
二分,预处理出青蛙跳跃距离的上界和下界,对距离二分求出跳跃次数,具体见代码注释
The Frog's Games
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 3072 Accepted Submission(s): 1520
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).
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.
Output
For each case, output a integer standing for the frog's ability at least they should have.
Sample Input
6 1 2 2 25 3 3 11 2 18
Sample Output
4 11
Source
The 36th ACM/ICPC
Asia Regional Dalian Site —— Online Contest
青蛙过河,总共距离为L,河中有石,知道石头离起点的距离,青蛙跳m次到对岸,求青蛙最小的最大跳跃距离
二分,预处理出青蛙跳跃距离的上界和下界,对距离二分求出跳跃次数,具体见代码注释
#include<cstdio> #include<iostream> #include<cstdlib> #include<algorithm> #include<ctime> #include<cctype> #include<cmath> #include<string> #include<cstring> #include<queue> #include<vector> #define sqr(x) (x)*(x) #define INF 0x1f1f1f1f #define PI 3.1415926535 #define LL long long #define mm 500005 using namespace std; int stone[mm]; int l,n,m,d,le,ri; int main() { while (~scanf("%d%d%d",&l,&n,&m)) { le=0; for (int i=0;i<n;i++) { scanf("%d",&stone[i]); } stone[n++]=l; //把河对岸当做最后一块石头 sort(stone,stone+n); int begin=0; for (int i=0;i<n;i++) { le=max(le,stone[i]-begin); //下界是两块石头间的最大距离 begin=stone[i]; } ri=l; //上界,跳一下就到对岸 while (le<ri) { int mid=(le+ri)>>1; int cnt=1,begin=0; //cnt=1 如果初始化为0,最后一次跳跃就没有被计算,想想这是为什么 for (int i=0;i<n;i++) { if (stone[i]-begin>mid)//跳不过去了 { begin=stone[i-1]; cnt++; } if (cnt>m) //这里可以进行优化 { break; } } // printf("l:%d r:%d mid:%d cnt:%d\n",le,ri,mid,cnt); /* 注意mid我用的是下取整 */ if (cnt>m) { le=mid+1; } else { ri=mid; } } printf("%d\n",le); } 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 game_二分搜索
- 4004 The Frog's Games --二分+贪心
- HDU 4004 The Frog's Games [2011 大连网络赛]
- HDU 4004 - The Frog\'s Games 第36届ACM/ICPC亚洲大连赛区网络赛
- 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 (二分 + 贪心 青蛙跳石头)