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.

 
思路:可验证,单调,二分答案,代码如下:
(左闭右开)
const int maxm = ;

int buf[maxm], n, k;

bool check(int d) {
int t = , now = buf[];
for (int i = ; i < n; ++i) {
if(buf[i] - now >= d) {
now = buf[i];
t++;
}
if(t >= k)
return true;
}
return false;
} int main() {
scanf("%d%d", &n, &k);
for (int i = ; i < n; ++i)
scanf("%d", &buf[i]);
sort(buf, buf + n);
int l = , r = buf[n - ] - buf[] + , mid;
while(l < r) {
mid = (l + r) >> ;
if(check(mid))
l = mid + ;
else
r = mid;
}
printf("%d\n", l - );
return ;
}

(左闭右闭)

const int maxm = ;

int buf[maxm], n, k;

bool check(int d) {
int t = , now = buf[];
for (int i = ; i < n; ++i) {
if(buf[i] - now >= d) {
now = buf[i];
t++;
}
if(t >= k)
return true;
}
return false;
} int main() {
scanf("%d%d", &n, &k);
for (int i = ; i < n; ++i)
scanf("%d", &buf[i]);
sort(buf, buf + n);
int l = , r = buf[n - ] - buf[], mid;
while(l <= r) {
mid = (l + r) >> ;
if(check(mid))
l = mid + ;
else
r = mid - ;
}
printf("%d\n", r);
return ;
}

小结:对于问题可进行贪心验证+答案具有单调性可以进行二分,注意左闭右闭的while条件

Day3-R-Aggressive cows POJ2456的更多相关文章

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

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

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

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

  3. POJ2456 Aggressive cows

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

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

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

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

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

  6. [ACM] poj 2456 Aggressive cows (二分查找)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5436   Accepted: 2720 D ...

  7. POJ 2456 Aggressive cows

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11192   Accepted: 5492 ...

  8. BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )

    最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #inc ...

  9. 疯牛-- Aggressive cows (二分)

    疯牛 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小 ...

  10. 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛

    1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 217  Solved: ...

随机推荐

  1. kafka单机搭建

    1.安装jdk1.8和zookeeper 2.下载kafka上传服务器 下载地址:http://archive.apache.org/dist/kafka/0.10.0.0/kafka_2.11-0. ...

  2. Hibernate学习(五)

    自关联测试案例 1.创建表 drop table if exists t_category ; create table t_category ( id ) primary key , name ) ...

  3. Leading dimension

    Leading dimension 如果你用LAPACK解过矩阵本征值问题,你一定会接触到这样一个名词,"leading dimension",比如在函数zheev中.我想绝大部分 ...

  4. kafka connector

    Kafka Connect 是一种用于在 Kafka 和其他系统之间可扩展的.可靠的的流式传输数据的工具.它使得能偶快速定义将大量数据集合移入和移除 kafka 连接器变得简单. kafka conn ...

  5. AngularJS学习:第一个demo

    1. 引入angular.js 相应版本下载地址: https://code.angularjs.org/ 2. 编写html <!DOCTYPE html> <html> & ...

  6. layui弹窗全屏显示

    var index =layer.open({ id: 'id', type: 2, area: ['100%', '100%'], fix: false, maxmin: true, shadeCl ...

  7. centos7搭建hadoop2.10完全分布式

    本篇介绍在centos7中大家hadoop2.10完全分布式,首先准备4台机器:1台nn(namenode);3台dn(datanode) IP hostname 进程 192.168.30.141 ...

  8. MySQL高级 InnoDB 和 MyISAM 的区别

    InnoDB:支持事务处理等不加锁读取支持外键支持行锁不支持FULLTEXT类型的索引不保存表的具体行数,扫描表来计算有多少行DELETE 表时,是一行一行的删除InnoDB 把数据和索引存放在表空间 ...

  9. MD5 加密解密字符串

    方法1: using System.Text; using System.Security.Cryptography; public string Hash(string toHash) { MD5C ...

  10. 学习之学习--混沌大学商学院--第一课--HHR计划

    <学习之学习> 第一课:混沌初开 李善友 1,课程目标:建立个人的多元思维模型,帮助企业找到创新驱动的增长战略. 2,创新:第二曲线创新,创新理论之父熊彼特. 3,核心课:第二曲线,非连续 ...