题目链接: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 ;
}

最新文章

  1. linux kernel elv_queue_empty野指针访问内核故障定位与解决
  2. Oculus安装问题
  3. Qt5_简易画板_详细注释
  4. 20145211 《Java程序设计》第7周学习总结——沧海横流
  5. js中的cookie使用
  6. spring事务失效
  7. mvc bundle功能(1)
  8. 使用win8.1 x64 office2010 php 使用 pdo_odbc 连接excel失败的问题
  9. [wikioi]线段覆盖 2
  10. 10.3 noip模拟试题
  11. oracle_oracle中修改日期的显示格式
  12. 使用wsimport生成不带JAXBElement对象的代理
  13. [转帖]Windows Server 2016各种版本介绍
  14. docker与虚拟机有何不同
  15. axure交互样式(下拉列表和矩形)
  16. react-native-video
  17. guxh的python笔记十:包和模块
  18. 每天一个小程序—0013题(爬图片+正则表达式 or BeautifulSoup)
  19. docker 2 容器数据卷
  20. native.js 判断是否安装某app

热门文章

  1. bzoj3326: [Scoi2013]数数
  2. selenium除错
  3. 第10课 C++异常简介
  4. OpenGL chapter5 基础纹理
  5. /PROC/MEMINFO之谜
  6. 搭建Discuz论坛
  7. CSS3基础
  8. mac 安装 nginx
  9. 《GPU高性能编程CUDA实战》第十一章 多GPU系统的CUDA C
  10. div+css样式命名规则,值得收藏