Aggressive cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10728   Accepted: 5288

Description

Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000).

His C (2 <= C <= N) cows don't like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ want to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?

Input

* Line 1: Two space-separated integers: N and C

* Lines 2..N+1: Line i+1 contains an integer stall location, xi

Output

* Line 1: One integer: the largest minimum distance

Sample Input

5 3
1
2
8
4
9

Sample Output

3
思路:最大化最小值、最小化最大值等问题常用二分搜索解决。
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN=;
int n,m;
int x[MAXN];
bool test(int d)
{
int cnt=;
int last=x[];
for(int i=;i<n;i++)
{
while(i<n&&x[i]-last<d)//两牛舍之间的距离均不小最小值d
{
i++;
}
if(i==n) break;
cnt++;
last=x[i];
}
return cnt>=m;
}
int main()
{
while(cin>>n>>m)
{
for(int i=;i<n;i++)
{
cin>>x[i];
}
sort(x,x+n);
int l=;
int r=0x3f3f3f3f;
while(r-l>)
{
int mid=(l+r)>>;
if(test(mid)) l=mid;
else r=mid;
}
cout<<l<<endl;
}
return ;
}

最新文章

  1. less简单入门
  2. 怎样去除织梦版权信息中的Power by DedeCms
  3. 如何区分SNAT和DNAT
  4. 12-2 mysql 查询
  5. 微信支付开发-Senparc.Weixin.MP详解
  6. zb的生日
  7. 转 : c++ 结构体 前向声明
  8. C#部分---利用arraylist集合做滚动抽奖;
  9. java DecimalFormat
  10. linux gcc loudong
  11. sublime3配置Quick-X+自动错误提示
  12. android eclipse——error: device not found解决办法
  13. 《Introduction to Algorithm》-chaper30-多项式与快速傅里叶变换
  14. 进一步探索:Windows Azure 网站中解锁的配置选项
  15. 201521123077 《Java程序设计》第1周学习总结
  16. OAuth2.0学习(1-7)授权方式4-客户端模式(Client Credentials Grant)
  17. Your ApplicationContext is unlikely to start due to a @ComponentScan of the default
  18. Kubelet bootstrap认证配置步骤
  19. C++.sprintf
  20. 数字&amp;字符串

热门文章

  1. 乌云TOP 10 简单介绍
  2. 20145210姚思羽《网络对抗》Web基础
  3. Linux 系统密码破解
  4. Django object filter查询[转]
  5. javax.servlet.jsp.JspException cannot be resolved to a type 和 javax.servlet.jsp.PageContext cannot be resolved to a type 解决办法
  6. HDFS数据复本存放
  7. 调整JVM堆内存解决OutOfMemoryError
  8. value optimized out的问题
  9. Prism 文档 第三章 管理组件之间的依赖关系
  10. Swift 3.0 on Ubuntu 15.10