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. 2019牛客暑期多校训练营(第十场) Han Xin and His Troop (高精度+拓展中国剩余定理)

    题意 裸题 思路 题中的模数之间并不互质,所以应该用拓展中国剩余定理. 但是交上去会炸,__int128过不了,所以用高精度的板子或者java大数都挺好过的. 这里推荐java大数,因为高精度板子用起 ...

  2. PTA的Python练习题(十二)-第4章-6 输出前 n 个Fibonacci数

    接下来应该做到 第4章-6 输出前 n 个Fibonacci数 了 def fib(n): a,b = 0,1 for i in range(n+1): a,b = b,a+b return a n= ...

  3. mybatis用mybatis-generator-core-1.3.5.jar自动生成实体类

    原文出处:https://blog.csdn.net/shuoshuo_12345/article/details/80626241,本文只是个人总结而已! 方法1:在pom文件中添加依赖 只需在搭建 ...

  4. Vue项目引进ElementUI组件

    1.https://blog.csdn.net/Mr_JavaScript/article/details/80741914 1.1 安装 npm install element-ui -save 1 ...

  5. 软件版本 Alpha、Beta、Rc

    软件版本的周期 α.β.γ 表示软件测试中的三个阶段 α :第一阶段,内部测试使用 β: 第二阶段,消除了大部分不完善的地方,仍可能存在漏洞,一般提供给特定的用户使用 γ: 第三阶段,产品成熟,个别地 ...

  6. pytorch资料

    torchvision是独立于pytorch的关于图像操作的一些方便工具库. torchvision的详细介绍在:https://pypi.org/project/torchvision/ torch ...

  7. 在Linux CentOS 下安装JDK 1.8

    一首先去官网下载linux 版本JDK1.8  http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-21331 ...

  8. Fedora26下Mysql改密码Unknown column 'password' in 'field list'

    本意向修改一个用户的密码,网上搜到的命令为如下 1 mysql> update user set password=password(“新密码”) where user=”用户名”; 执行后报错 ...

  9. centos610安装postgresql

    1.安装仓储 安装仓库依赖: yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-6-x86_64/pgdg-r ...

  10. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 网格系统实例:列排序

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...