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<algorithm>
#include<cstdio>
using namespace std;
#define MAXN 100001
#define INF 1000000001
/*
将奶牛分配到到多个点,使得他们之间的最小距离最大
check(x)函数 最小距离为x的时候能否放下n头奶牛
*/
int n, c, a[MAXN];
bool check(int x)
{
int tmp = a[], cnt = ;
for(int i=;i<n;i++)
{
if (a[i] - tmp >= x)
{
cnt++;
tmp = a[i];
}
}
return (cnt >= c);
}
int main()
{
while (scanf("%d%d", &n, &c) != EOF)
{
int Max = -;
for (int i = ; i < n; i++)
{
scanf("%d", &a[i]);
Max = max(Max, a[i]);
}
int beg = , end = Max,ans=;
sort(a, a + n);
while (beg <= end)
{
int mid = (beg + end) / ;
if (check(mid))
{
ans = mid;
beg = mid + ;
}
else
end = mid - ;
}
printf("%d\n", ans);
}
}

Aggressive Cows 二分的更多相关文章

  1. POJ 2456 Aggressive cows(二分答案)

    Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22674 Accepted: 10636 Des ...

  2. POJ - 2456 Aggressive cows 二分 最大化最小值

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18099   Accepted: 8619 ...

  3. POJ 2456 Aggressive cows (二分 基础)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7924   Accepted: 3959 D ...

  4. POJ2456 Aggressive cows 二分

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

  5. [poj 2456] Aggressive cows 二分

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

  6. Aggressive cows 二分不仅仅是查找

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7083   Accepted: 3522 Description Farme ...

  7. [POJ] 2456 Aggressive cows (二分查找)

    题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...

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

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

  9. poj 2456 Aggressive cows 二分 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2456 解法 使用二分逐个尝试间隔距离 能否满足要求 检验是否满足要求的函数 使用的思想是贪心 第一个点放一头牛 后面大于等于尝试的距离才放 ...

随机推荐

  1. 【原创】Vue项目中各种功能的实现

    已完成: 后台的管理功能: 这里用的组件是 element-UI  ====> NavMenu ◆首先是排版 : <div class="manage-page fillcont ...

  2. 百度地图JavaScript API获取用户当前经纬度和详细地理位置,反之通过详细地理位置获取当前经纬度

    前言: 前端时间刚好使用了百度地图的js api定位获取用户当前经纬度并获取当前详细位置和通过当前用户详细地理位置换取用户当前经纬度坐标的功能,为了方便下次找起来方便一些自己在这里记录一下,希望也能够 ...

  3. JavaScript--innerHTML 属性

    innerHTML 属性用于获取或替换 HTML 元素的内容. 语法: Object.innerHTML 注意: 1.Object是获取的元素对象,如通过document.getElementById ...

  4. VMware 14 Pro 永久许可证激活密钥

    VMware 14 Pro 永久许可证激活密钥 FF31K-AHZD1-H8ETZ-8WWEZ-WUUVACV7T2-6WY5Q-48EWP-ZXY7X-QGUWD

  5. Ubuntu 系统的常用快捷键

    Ubuntu操作基本快捷键 ibus-setup :设置系统输入法 scp filename username@serverIp:/home/xxx/xxx/filename   回车输入该usern ...

  6. 【转】Java 集合系列06之 Vector详细介绍(源码解析)和使用示例

    概要 学完ArrayList和LinkedList之后,我们接着学习Vector.学习方式还是和之前一样,先对Vector有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它.第1部分 Vec ...

  7. 【sqli-labs】 less46 GET -Error based -Numeric -Order By Clause(GET型基于错误的数字型Order By从句注入)

    http://192.168.136.128/sqli-labs-master/Less-46/?sort=1 sort=4时出现报错 说明参数是添加在order by 之后 错误信息没有屏蔽,直接使 ...

  8. vue项目中使用百度地图的方法

    1.在百度地图申请密钥: http://lbsyun.baidu.com/  将 <script type="text/javascript" src="http: ...

  9. PAT-day1

    1001 害死人不偿命的(3n+1)猜想 (15 分)   卡拉兹(Callatz)猜想: 对任何一个正整数 n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把 ( 3n+1)砍掉一半.这样一直 ...

  10. WebService附加到IIS调试,未命中断点

    写好了一个WebService,部署到IIS上,用浏览器访问发现并不能命中断点. 经过多次的查找发现是由于附加的代码类型选择错误. 下图由于错误的选择了托管代码,导致调试时不命中断点,勾选自动解决.