HDU 4004 The Frog's Games
2015-07-20 00:12
330 查看
题意:有一条河长L,中间有n个石头,一只青蛙想跳过去,它最多只能跳m次。它该如何跳,才能跳到对岸,求青蛙最小的跳跃能力。
思路:青蛙的最小跳跃力,实际上就是所有跳跃中的最大距离。这里也就是求青蛙最大距离的最小值。那我们二分青蛙的最大距离就可以了。
思路:青蛙的最小跳跃力,实际上就是所有跳跃中的最大距离。这里也就是求青蛙最大距离的最小值。那我们二分青蛙的最大距离就可以了。
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; int a[500010],n,m; bool check(int mid)//判断在这个最大跳跃长度的条件下青蛙需要跳的次数是否小于等于m。 { int last = 0; int cnt = 0; for(int i = 1; i < n + 2;) { if(a[i] - a[last] > mid) { if(i - last == 1) return false;//如果相邻的石头距离都大于这个最大跳跃值,这个跳跃值肯定是偏小的,返回false; cnt++; last = i - 1; } else i++; } if(a[n + 1] - a[last] <= mid) cnt++;//最后跳到岸上。 if(cnt > m) return false;//次数太多,偏小。 else return true;//次数满足,但是不一定是最小的跳跃值。 } int main() { int L; while(~scanf("%d%d%d",&L,&n,&m)) { a[0] = 0; a[n+1] = L; for(int i = 1; i <= n; i++) scanf("%d",&a[i]); sort(a,a+n+2); int l = 0,r = L,mid; while(l < r) { mid = (l + r) / 2; if(check(mid)) r = mid;//次数满足了,但是有可能有更小的。 else l = mid + 1;//次数大了,说明跳跃值不够大。 } printf("%d\n",r); } return 0; }
相关文章推荐
- 快速排序里的学问:从猜数字开始
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- Search Insert Position,Search for a Range,Pow(x, n),Sqrt(x)
- Find Minimum in Rotated Sorted Array II
- [LeetCode] Sqrt(x)
- [LeetCode] Pow(x, n)
- [LeetCode] Search Insert Position
- [LeetCode] Search for a Range
- [LeetCode] Search in Rotated Sorted Array
- PAT 1057 Stack (30)
- int sqrt(int x)
- Pow(x, n)
- Find Minimum in Rotated Sorted Array
- Divide Two Integers
- Leetcode:Search a 2D Matrix 菜鸟解法
- [BZOJ1257][CQOI2007]余数之和
- POJ2391解题报告
- poj_2002
- Codeforces Round #275 (Div. 2) B. Friends and Presents
- zoj 2855 Google Map