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. E20170623-hm

    verbose  adj. 冗长的,啰唆的,累赘的; reverse   vt. (使) 反转; (使) 颠倒; 掉换,交换; [法] 撤消,推翻;                adj. 反面的; ...

  2. aixcoder智能编程助手开发插件推荐

    1. aixcoder安装使用 1.1. 介绍 1.1.1. 功能 智能代码提示她用强大的深度学习引擎,能给出更加精确的代码提示: 代码风格检查她有代码风格智能检查能力,帮助开发者改善代码质量: 编程 ...

  3. jquery实现图片预加载提高页面加载速度

    使用jquery实现图片预加载提高页面加载速度和用户体 我们在做网站的时候经常会遇到这样的问题:一个页面有大量的图片导致页面加载速度缓慢,经常会出现一个白页用户体验很不好.那么如何解决这个问题 呢?首 ...

  4. Oracle 递归的写法(start with) 以及where条件作用域

    先转一个讲Oracle递归讲得非常透彻的文章: http://blog.csdn.net/weiwenhp/article/details/8218091 前言:嗯,这也是一个前人挖坑,后人来填的故事 ...

  5. 个人作业——Alpha项目测试

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/ 这个作业要求在哪里 https://edu.cnbl ...

  6. Android 自己搭建一个直播系统吧

    服务端用 SRS(Simple Rtmp Server),在这里下载simple-rtmp-server需要Linux系统最好是Ubuntu,装个Ubuntu虚拟机就行了在Linux里,解压缩SRS ...

  7. Ch03 React/JSX/Component 簡介

    Facebook 本身有提供 Test Utilities,但由于不够好用,所以目前主流开发社群比较倾向使用 Airbnb 团队开发的 enzyme,其可以与市面上常见的测试工具(Mocha.Karm ...

  8. SQL基本操作——约束

    我们将主要探讨以下几种约束: 1.NOT NULL 2.UNIQUE 3.PRIMARY KEY 4.FOREIGN KEY 5.CHECK 6.DEFAULT SQL NOTNULL约束:NOT N ...

  9. servlet的多线程并发问题

    package gz.itcast.e_thread; import java.io.IOException; import javax.servlet.ServletException; impor ...

  10. spring 将配置文件中的值注入 属性

    1.编写配置文件 #债权转让 #默认周期 必须大于0 credit.defaultDuration=1 #最小转让金额(元) credit.minBidAmount=1.00 #最小转让时间 到期时间 ...