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. shell 脚本基础

    弱类型语言 bash 变量类型 本地变量 环境变量 局部变量 位置参数变量 特殊变量 运行 无执行权限 bash hello.sh 有执行权限 检查语法 bash -n user.sh 跟踪每一行的执 ...

  2. MSE初始化和基本操作

    MSE默认的登录账户密码可能是:login/password (admin/admin). l 初始化配置完成后,下次使用root登录时,仅显示Linux shell提示符,而不是安装脚本. 您可以随 ...

  3. mark mark mark

    编译并使用静态lib---->>>>转自:Walkthrough: Creating and Using a Static Library (C++) 跟踪内存分配:http: ...

  4. bootstrap-suggest-plugin input可选可输(表单) 好用的前端插件

    bootstrap-suggest-plugin          DEMO下载 1.准备:页面引入(点击下载) <link rel="stylesheet" href=&q ...

  5. 【HV】HVIL-High Voltage Interlock Loop

    HVIL高压互锁功能 1.HVIL作用 High Voltage Interlock Loop 高压互锁,是在"ISO6469-3:电动汽车安全技术规范第三部分:人员电气伤害防护" ...

  6. comsol日常联系

  7. Esp32的一些资料链接

    http://wiki.ai-thinker.com/doku.php/utils/esp32_idf_menuconfig   http://wiki.ai-thinker.com/doku.php ...

  8. HD Tune检查硬盘各参数的含义

    01 =Read Error Rate / (底层)数据读取错误率指从磁盘表面读取数据时发生的硬件读取错误的比率,Raw值对于不同的厂商有着不同的体系,单纯看做1个十进制数字是没有任何意义的.以上为W ...

  9. mysql之指定为definer的用户不存在

    问题描述: java.sql.SQLException: The user specified as a definer ('tsingsoft'@'%') does not exist 解决: 1. ...

  10. rem布局,在用户调整手机字体大小/用户调整浏览器字体大小后,布局错乱问题

    一.用户调整浏览器字体大小,影响的是从浏览器打开的web页. 浏览器设置字体大小,影响浏览器打开的页面.通过js可控制用户修改字体大小,使页面不受影响. (function(doc, win) { / ...