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. idea 导入svn中java WEB项目

    1.打开idea 2.填写svn路径 3.指定本地路径 4.选择1.8 format 5.添加jdk 6.配置tomcat启动项目 File -- Project Structure

  2. SprintBoot学习(三)

    Thymeleaf模板引擎 1.thymeleaf是一个Java类库,,他是xml/xhtml/html5的模板引擎可以作为view层 2.themeleaf基本语法 引入thymeleaf < ...

  3. Cisco AP-Regulatory Domain

    这一note主要解释Regulatory Domain是什么.以较新的思科AP的型号举例,思科AP的PID一般都是按照如下的命名: AIR-AP3802I-H-K9C 按照自己的理解: AIR:其中的 ...

  4. 实时监听 mysql 操作,Linux 版

    效果 场景:某数据库新增了某条记录,服务器可以监听到变化的数据与操作,如 增加一条记录: id = 1009,name=''test,number = 11 服务器监听结果: 实现过程 测试过程:数据 ...

  5. Android 调用系统Email发送带多附件的邮件

    转自:http://www.open-open.com/lib/view/open1347005126912.html 众所周知,在Android中调用其他程序进行相关处理,都是使用的Intent.当 ...

  6. Linux服务器yum安装mysql

    阿里云Linux 服务器安装mysql 可以用rpm 和yum 方式安装,我这次安装是采用yum方式安装 请直接复制以下链接,看安装步骤 https://www.cnblogs.com/nzplear ...

  7. 获取Linux系统运行情况信息

    代码: #include <stdio.h> #include <unistd.h> /* usleep() */ #include <stdlib.h> #inc ...

  8. SearchRequest用于与搜索文档、聚合、定制查询有关的任何操作

    SearchRequest用于与搜索文档.聚合.定制查询有关的任何操作,还提供了在查询结果的基于上,对于匹配的关键词进行突出显示的方法. 1,首先创建搜索请求对象:SearchRequest sear ...

  9. C++中的内联函数

    (1)定义.函数声明和定义时,在返回值类型前加inline关键字. (2)作用.避免调用时的额外开销. (3)使用情况.函数体很小,且频繁调用. (4)本质.通过牺牲代码段空间,来提高程序运行的时间效 ...

  10. MySQL(window10)加载配置文件的顺序

    mysql加载配置的顺序为:(mysql --help中有详细的说明) C:\WINDOWS\my.ini C:\WINDOWS\my.cnf C:\my.ini C:\my.cnf D:***\my ...