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.

 

#include<iostream>
#include<numeric>
#include<algorithm>
using namespace std;
int pos[];
int n,c;
int C(int d){
int last=;
for(int i=;i<c;i++){
int cur=last+;
while(cur<n&&pos[cur]-pos[last]<d)
cur++;
if(cur==n)
return false;
last=cur;
}
return true;
}
int main(){
cin>>n>>c;
for(int i=;i<n;i++)
cin>>pos[i];
sort(pos,pos+n);
int lb=,ub=*max_element(pos,pos+n);
int mid=(ub-lb)/;
while(ub-lb>=){
if(C(mid)){
lb=mid+;
}
else{
ub=mid-;
}
mid=(lb+ub)/;
}
cout<<mid<<endl;
return ;
}

POJ2456--Aggressive cows(Binary Search) unsolved的更多相关文章

  1. 二分法的应用:最大化最小值 POJ2456 Aggressive cows

    /* 二分法的应用:最大化最小值 POJ2456 Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...

  2. POJ2456 Aggressive cows

    Aggressive cows 二分,关键是转化为二分! #include <cstdio> #include <algorithm> ; ; int N, C; int a[ ...

  3. POJ2456 Aggressive cows 2017-05-11 17:54 38人阅读 评论(0) 收藏

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13993   Accepted: 6775 ...

  4. 二分算法的应用——最大化最小值 POJ2456 Aggressive cows

    Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description Far ...

  5. POJ2456 Aggressive cows 二分

    Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...

  6. poj2456 Aggressive cows(二分查找)

    https://vjudge.net/problem/POJ-2456 二分,从最大长度开始,不断折半试,如果牛全放下了,就是可行,修改下界,否则改上届. #include<iostream&g ...

  7. POJ2456 Aggressive cows(二分+贪心)

    如果C(d)为满足全部牛之间的距离都不小于d. 先对牛舍的位置排序,然后二分枚举d,寻找满足条件的d. #include<iostream> #include<cstdio> ...

  8. POJ2456 Aggressive cows(二分)

    链接:http://poj.org/problem?id=2456 题意:一个数轴上n个点,每个点一个整数值,有c个奶牛,要放在这些点的某几个上,求怎么放可以使任意两个奶牛间距离的最小值最大,求这个最 ...

  9. 【POJ - 2456】Aggressive cows(二分)

    Aggressive cows 直接上中文了 Descriptions 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x ...

随机推荐

  1. ES6 Generator的应用场景

    一.基础知识 API文档 ES6 诞生以前,异步编程的方法,大概有下面四种. 回调函数 事件监听 发布/订阅 Promise 对象 Generator 函数将 JavaScript 异步编程带入了一个 ...

  2. VS2010工程结构及其瘦身策略

    VS2010工程结构: 我们以在VS2010上利用MFC创建的单文档应用程序HelloWorld的文件结构为例,简述VS2010应用程序工程中文件的组成结构. 1.解决方案相关文件 解决方案相关文件包 ...

  3. [Robot Framework] 如何在Setup中用Run Keywords执行多个带参数的关键字

    参考文档:http://www.howtobuildsoftware.com/index.php/how-do/bZ7q/robotframework-setup-teardown-robot-fra ...

  4. redis集群中的主从复制架构(3主3从)

    架构图如下 首先开启6个实例,这里为了演示方便,只是在一个linux上开启了6个redis实例 6380 (主)   6480 (从) 6381(主)   6481(从) 6382(主)   6482 ...

  5. 在win8 App中,StorageFile比Path更好用

    Skip the path: stick to the StorageFile: http://blogs.msdn.com/b/wsdevsol/archive/2012/12/05/stray-f ...

  6. 【轻松前端之旅】元素,标记,属性,<html>标签

    HTML文档是由HTML元素定义的. HTML元素(element)指的是从开始标签(start tag)到结束标签(end tag)的所有代码. 有些元素会使用简写记法,如<img src=' ...

  7. 爬虫之mongodb数据库

    一 mongodb的介绍 1.易用性:mongodb是一款强大.灵活并且易扩展的数据库.他面向于文档的数据库,而不是关系性数据库.不采用关系型主要是为了获得更好的扩展性.还有一个好处就是面向文档的数据 ...

  8. centos7 hive + 远程mysql 搭建笔记

    1.require:java环境,本地可用的hadoop,远程可访问的mysql 2.拷贝hive文件(hive-2.2.1) 3.设置环境变量 export HIVE_HOME=/data/spar ...

  9. centos 7 搭建pip源

    一.安装pip2pi工具: pip install pip2pi 或编译: git clone https://github.com/wolever/pip2pi cd pip2pi python s ...

  10. linux普通用户使用1024以下的端口(80)

    linux对于非root权限用户不能使用1024以下的端口,对于一些服务,过高的权限,会带来一定的风险.那么对于低权限的用户如何对外开放1024以下的端口.我这里找到几种办法并且亲测可行 首先搭建环境 ...