poj2456(二分+贪心)
2024-08-26 20:54:16
题目链接:http://poj.org/problem?id=2456
题意:
有n个呈线性排列的牲畜堋,给出其坐标,有c头牛,求把两头牛的最短距离的最大值。
思路:
先将坐标排个序。两头牛的最短距离最小为0,最大为a[n-1]-a[0]。结果一定在这之间,那么就用二分一步步逼近,若m满足要求,则在[m+1,r]之间查找,否则在[l,m-1]之间查找。判断的过程用到贪心的思想。详见代码:
#include<cstdio>
#include<algorithm>
using namespace std; int n,c,res;
int a[]; int check(int k){
int last=,next;
for(int i=;i<c;++i){
next=last+;
while(next<n&&a[next]-a[last]<k)
++next;
if(next>=n) return ;
last=next;
}
return ;
} int main(){
scanf("%d%d",&n,&c);
for(int i=;i<n;++i)
scanf("%d",&a[i]);
sort(a,a+n);
int l=,r=a[n-]-a[],m;
while(l<=r){
m=(l+r)>>;
if(check(m))
res=m,l=m+;
else
r=m-;
}
printf("%d\n",res);
return ;
}
最新文章
- linux kernel elv_queue_empty野指针访问内核故障定位与解决
- Oculus安装问题
- Qt5_简易画板_详细注释
- 20145211 《Java程序设计》第7周学习总结——沧海横流
- js中的cookie使用
- spring事务失效
- mvc bundle功能(1)
- 使用win8.1 x64 office2010 php 使用 pdo_odbc 连接excel失败的问题
- [wikioi]线段覆盖 2
- 10.3 noip模拟试题
- oracle_oracle中修改日期的显示格式
- 使用wsimport生成不带JAXBElement对象的代理
- [转帖]Windows Server 2016各种版本介绍
- docker与虚拟机有何不同
- axure交互样式(下拉列表和矩形)
- react-native-video
- guxh的python笔记十:包和模块
- 每天一个小程序—0013题(爬图片+正则表达式 or BeautifulSoup)
- docker 2 容器数据卷
- native.js 判断是否安装某app