题目:

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.

题意分析:
对于在一维坐标系上的N个点,你要在这N个点上安排C头牛,让这C头牛中相邻两头牛的最小距离尽可能的大。抓住两个最值
最小值指的是C头牛中任意相邻两头牛距离的最小距离。
最大值指的是这个最小距离的最大值。
那么我们需要做的就是用二分找出让这个最小距离满足条件的临界情况。
然后输出的时候输出的左值,因为你的最终判断的是左值和右值的中值,当跳出循环的时候,因为你的中值已经不满足了,那么你的右值肯定也是不满足的,所以输出左值就是正解!
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAXN = 1e5+5;
int N, C;
int Pt[MAXN]; bool fun(int x)
{
int pre = 0, next = 1, cnt = 1;
while(next < N)
{
if(Pt[next] - Pt[pre] < x)
{
next++;
}
else
{
pre = next;
next++;
cnt++;
}
}
return cnt >= C;
} void solve()
{
sort(Pt, Pt+N);
int left = Pt[0], right = Pt[N-1], mid;
while(right - left > 1)
{
mid = (left+right)>>1;
if(fun(mid)) left = mid;
else right = mid;
}
printf("%d\n", left);
} int main()
{
while(~scanf("%d%d", &N, &C))
{
for(int i = 0; i < N; i++)
{
scanf("%d", &Pt[i]);
}
solve();
}
return 0;
}

  

POJ_2456 Aggressive cows 【二分求最大化最小值】的更多相关文章

  1. poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分

    poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...

  2. codeforce 1070 E Getting Deals Done(二分求最大化最小值)

    Polycarp has a lot of work to do. Recently he has learned a new time management rule: "if a tas ...

  3. poj 2456 Aggressive cows(二分搜索之最大化最小值)

    Description Farmer John has built a <= N <= ,) stalls. The stalls are located along a straight ...

  4. POJ 3258(二分求最大化最小值)

    题目链接:http://poj.org/problem?id=3258 题目大意是求删除哪M块石头之后似的石头之间的最短距离最大. 这道题目感觉大致代码写起来不算困难,难点在于边界处理上.我思考边界思 ...

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

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

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

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

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

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

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

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

  9. [poj 2456] Aggressive cows 二分

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

随机推荐

  1. Bootstrap 中的 aria-label 和 aria-labelledby 属性

    这两个属性是为特殊网页阅读器设置的属性,在一些特殊设备上,当浏览到这样的内容设备会将内容读出来.是为了一些有视力障碍的人能够同样”浏览”网页而准备的. 转自http://blog.csdn.net/l ...

  2. mysql中如何不重复插入满足某些条件的重复的记录的问题

    最近在项目中遇到了这样的一个问题“: 在mysql数据库中需要每次插入的时候不能插入三个字段都相同的记录.在这里使用到了 insert into if not exists  和insert igno ...

  3. 690. Employee Importance员工权限重要性

    [抄题]: You are given a data structure of employee information, which includes the employee's unique i ...

  4. 关于recv的返回值

    通常recv有几种返回值 1.==0 表示收到FIN包, 因为FIN包,是状态为标记为FIN的空包,没有携带数据,所以recv的长度为0 2.>0 表示收到了数据, 但是有没有收完,是不知道的 ...

  5. 面试题:HTTP必知必会——常见面试题总结 背1

    1.常用的HTTP方法有哪些?GET: 用于请求访问已经被URI(统一资源标识符)识别的资源,可以通过URL传参给服务器POST:用于传输信息给服务器,主要功能与GET方法类似,但一般推荐使用POST ...

  6. linux下方便的录屏命令

    linux下方便的录屏命令   ffmpeg -f x11grab -s 1024*768 -r 20 -i :0.0 -sameq ~/recode.mpg -r后是刷新屏率,   推出直接Ctrl ...

  7. codefirst 关系处理

    1.http://www.cnblogs.com/libingql/archive/2013/01/31/2888201.html 2.多对多 protected override void OnMo ...

  8. 在VS2008和VS2010中禁用Visual Assist X

    此方法对于VS2008和VS2010 都适用. 在VS2008或VS2010菜单栏中选择“VassistX”选项卡,找到“Enable/Disable Visual Assist X”选项, 点击即可 ...

  9. HTTP文件上传插件开发文档-ASP

    版权所有 2009-2016 荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webplug/http-u ...

  10. 基于HTML5 Ajax文件上传进度条如何实现(jquery版本)

    <!DOCTYPE html> <html> <head> <title>html5_2.html</title> <meta htt ...