题目:

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. 189. Rotate Array 从右边开始翻转数组

    [抄题]: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ...

  2. Linux 的文件系统

    Linux 文件属性 文件属性示意图 第一栏代表这个文件的类型与权限(permission): FHS Filesystem Hierarchy Standard(文件系统层次化标准) 1. / (r ...

  3. 【Java】java中的compareTo和compare的区别

    compare 从这里可以看出,compare是Comparator接口中的一个类,再看一下源代码中的解释 Compares its two arguments for order. Returns ...

  4. APUE(3)---文件I/O (1)

    一.引言 UNIX系统中的大多数文件对I/O只需用到5个函数:open/read/write/lseek和close,这些函数都是不带缓冲I/O(Unbuffered I/O).只要涉及到多个进程间共 ...

  5. POJ3041 Asteroids(二分图最小点覆盖)

    Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape o ...

  6. JAVA自动装箱拆箱与常量池

    java 自动装箱与拆箱 这个是jdk1.5以后才引入的新的内容,作为秉承发表是最好的记忆,毅然决定还是用一篇博客来代替我的记忆: java语言规范中说道:在许多情况下包装与解包装是由编译器自行完成的 ...

  7. android开关控件Switch和ToggleButton

    序:今天项目中用到了开关按钮控件,查阅了一些资料特地写了这篇博客记录下. 1.Switch <Switch android:id="@+id/bt" android:layo ...

  8. Newtonsoft.Json.Linq

    var json = "{\"name\":\"ok1\",\"sex\":\"man\"}"; / ...

  9. 适配器(Adapter)模式

    一. 适配器(Adapter)模式 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本接口不匹配而无法在一起工作的两个类能够在一起工作 二. 类的Adapter模式的结构: 目标(Ta ...

  10. 使用CodeMaid自动程序排版[转]

    前言 「使用StyleCop验证命名规则」这篇文章,指引开发人员透过StyleCop这个工具,来自动检验项目中产出的程序代码是否合乎命名规则. [Tool] 使用StyleCop验证命名规则 但是在项 ...