HDU 4004 The Frog's Games(二分)
来源:互联网 发布:java软件开发项目经验 编辑:程序博客网 时间:2024/06/05 19:11
题目链接:Click here~~
题意:
一条线段长度为L,线段上有n个点,最多选取 m-1 个点,使得包括线段端点在内的相邻点之间的最大距离值最小。
解题思路:
首先,对于某个最大距离值 d ,如果 d 能满足要求,设 d' > d,那么 d' 也一定能满足要求。而问题的解就是那个满足要求的最小的 d 。
那么,如何判断对于某个最大距离值 d 能否满足要求呢?
由于最多选取 m-1 个点,所以当选的点超过这个数目的时候,不满足,另外,若两个相邻点距离大于 d 的时候,也不满足。
为了满足点的数目的限制,我们在选点的时候应该尽量选择距离远的,这样可以选取更少的点,更容易满足要求。
于是问题可转化为二分模型求解。
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int L,n,m,pos[500002];bool across(int l){ int step = 0 , cur = 0; for(int i=0;i<=n;) { if(++step>m || pos[i]-cur>l) return false; while(i<=n && pos[i]-cur<=l) i++; cur = pos[i-1]; } return true;}int main(){ while(~scanf("%d%d%d",&L,&n,&m)) { for(int i=0;i<n;i++) scanf("%d",&pos[i]); pos[n] = L; sort(pos,pos+n); int l = 0, r = L; while(l < r) { int mid = (l+r)/2; if(across(mid)) r = mid; else l = mid+1; } printf("%d\n",r); } 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<二分>
- 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 (二分搜索)
- 微软RDP控件的属性
- Linux常用命令
- ImageUtil类
- 那些年,我们应该读的书 《Effective_C#_中文版_改善C#程序的50种方法》第二章
- Ubuntu 12.04 使用gnome3图形界面
- HDU 4004 The Frog's Games(二分)
- ftp自动上传下载脚本
- 简单的makefile框架
- 你真的愿意到了50岁还要做编程吗?
- 汉诺塔
- vs2005应用程序正常初始化失败的解决方案
- IT“咏叹调”之--你公司哪个部门是“老大”?
- Hi,我们的代码重构了
- 运维架构师-并不遥远的彼岸