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. PropertyInfo 类

    [AttributeUsage(AttributeTargets.Property)] //Models 特性        public class CanWriteAttribute : Attr ...

  2. Django day27 认证组件,权限组件()

    一:认证组件 1.写一个类 class LoginAuth(): # 函数名一定要叫authenticate,接收必须两个参数,第二个参数是request对象 def authenticate(sel ...

  3. 常见的Java Script内存泄露原因及解决方案

    前言 内存泄漏指由于疏忽或错误造成程序未能释放已经不再使用的内存.内存泄漏并非指内存在物理上的消失,而是应用程序分配某段内存后,由于设计错误,导致在释放该段内存之前就失去了对该段内存的控制,从而造成了 ...

  4. 自己的myeclipse添加javaee7步骤

    1 new一个javaee名字,然后add jars 就可以

  5. JSP所需要掌握的部分

    JSP基本语法 指令 <%@ 指令%> JSP指令是JSP的引擎 主要的两种指令是page和include(taglib) <%@ page import="java.ut ...

  6. Leetcode03---Longest Substring Without Repeating Characters

    Description: Given a string, find the length of the longest substring without repeating characters. ...

  7. Unity学习-摄像机的使用(六)

    快速对齐摄像机 [选择摄像机-GameObject-Align With View] Game模板中显示的界面,就是摄像机拍摄后的画面 本次学习案例 添加一个地形,一个点光源,三个Cube   了解摄 ...

  8. Extension Methods(扩展方法)

    在 OOPL 中,有静态方法.实例方法和虚方法,如下:   public sealed class String {      public static bool  IsNullOrEmpty(st ...

  9. yield让代码更加简洁

    不能传入out或ref public IEnumerable<Shop> GetShop() { ; i < ; i++) { yield return new Shop { ID ...

  10. 【Java设计模式】工厂模式

    简单工厂模式 简单工厂模式不是23种里的一种,简而言之,就是有一个专门生产某个产品的类.比如下图中的鼠标工厂,专业生产鼠标,给参数0,生产戴尔鼠标,给参数1,生产惠普鼠标. 示例代码: //一个产品接 ...