HDUOJ----4004The Frog's Games(二分+简单贪心)
2024-09-04 10:05:11
The Frog's Games
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 3263 Accepted Submission(s): 1596
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 2 2 25 3 3 11 2 18
Sample Output
4 11
Source
参考别人的思路做的,挫....
利用二分来选择适合的数。。。然后再验证,是枚举的优化版。 好吧,这个思路是别人的 ,我还是太挫啦
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<functional>
const int maxn= ;
using namespace std;
int stone[maxn],n,m;
bool judge(int dis)
{
int i=,pre=,count=;
while(i<=n+)
{
count++;
if(dis<stone[i]-stone[i-]) return false ; //这个石头跳不过去,所以失败
while (i<=(n+)&&dis>=stone[i]-stone[pre]) i++;
pre=i-;
if(count>m) return false ;
}
return true ;
}
int main()
{
int length,i;
while(scanf("%d%d%d",&length,&n,&m)!=EOF)
{
memset(stone,,sizeof(int)*(n+));
for(i=;i<=n;i++)
scanf("%d",&stone[i]);
stone[i]=length;
sort(stone,stone+(n+),less<int>()); //升序安放stone
int ans,low=,high=length;
while(low<=high)
{
ans=(low+high)/; //跳这么远的时候能否满足要求
if(ans*m>=length&&judge(ans)) high=ans-;
else low=ans+;
}
printf("%d\n",low);
}
return ;
}
最新文章
- Scala入门之Array
- U-boot.lds文件分析
- hdu4547 lca tarjan
- POJ2253 Frogger
- return break continue
- Redis - hash类型操作
- Linux之select系统调用_2
- Otto开发初探——微服务依赖管理新利器
- 理解RunLoop
- connot find one or more components. please reinstall the application
- BZOJ2300: [HAOI2011]防线修建
- 【英国毕业原版】-《伯明翰城市大学毕业证书》BCU一模一样原件
- Java多线程基础(二)
- Promise的两种处理异步的方式
- jdk的下载
- linux 学习之路:mkdir命令使用
- SwipeableFlatList 实现类似于QQ列表滑动
- 训练题(代码未检验)(序列前k大和问题)
- jqprint的网页打印,打印预览可以包含图片
- [z]dbms_stats.lock_table_stats对于没有统计信息的表分区同样有效