Aggressive cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 24979   Accepted: 11594

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

Hint

OUTPUT DETAILS:

FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3.

Huge input data,scanf is recommended.

Source

一句话题意:
有n个牛栏,k头牛。将这k头牛放入牛栏中,如何保证牛与牛之间最小的距离最大。
解析:
明显的是二分算法。
L=0;R= 最大值
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=;
int n,k;
int a[maxn];
int check(int x){
int cnt=;
int y=a[];
for(int i=;i<=n;i++)
if(a[i]-y>=x){
cnt++;y=a[i];
if(cnt>=k) return ;
}
return ;
}
int main(){
cin>>n>>k;
for(int i=;i<=n;i++){
cin>>a[i];
}
sort(a+,a++n);
int l=,r=a[n],mid;
while (l<r){
mid=(l+r+)/;
if(check(mid)) l=mid;
else r=mid-;
}
cout<<l<<endl;
return ;
}

最新文章

  1. SQLAlchemy(一)
  2. 更改pip安装源的镜像解决安装总是timeout的情况
  3. 【转】Web Service单元测试工具实例介绍之SoapUI
  4. 增加Activity Monitor中的作业保存数量
  5. c#sqlhelper之用法
  6. iLinuxBot: Designing Botnets to Manage Linux Clients
  7. Codevs 1063 合并果子
  8. Centos7安装Xmind
  9. 百度云管家-V4.6.1-单文件版绿色版
  10. uva 11997 K Smallest Sums 优先队列处理多路归并问题
  11. shell参数传递
  12. Spring Security(三十六):12. Spring MVC Test Integration
  13. 基于OpenCV做“三维重建”(2)--封装标定过程
  14. 如何用ABP框架快速完成项目(13) - 用ABP遇到难题项目受阻时如何避免项目延迟
  15. cf1114D 区间dp基础
  16. Python特色数据类型(列表)(上)
  17. XPath简介及节点
  18. 微信小程序https发起请求失败的解决方法
  19. 利用 Azure Devops 创建和发布 Nuget 包
  20. flask中邮件发送方法

热门文章

  1. Linux 之 软件安装
  2. django内置缓存
  3. Windows Server 2008上网设置——IE
  4. IAR 为 STM32新建工程模板(最详细)
  5. Linux:入门基础
  6. ES6中let、const和var的区别
  7. JavaScript异步学习笔记——主线程和任务队列
  8. python 杂记-unittest
  9. MySQL 新建用户并赋予权限
  10. Windows 创建Raid