Aggressive cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10728   Accepted: 5288

Description

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
思路:最大化最小值、最小化最大值等问题常用二分搜索解决。
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN=;
int n,m;
int x[MAXN];
bool test(int d)
{
int cnt=;
int last=x[];
for(int i=;i<n;i++)
{
while(i<n&&x[i]-last<d)//两牛舍之间的距离均不小最小值d
{
i++;
}
if(i==n) break;
cnt++;
last=x[i];
}
return cnt>=m;
}
int main()
{
while(cin>>n>>m)
{
for(int i=;i<n;i++)
{
cin>>x[i];
}
sort(x,x+n);
int l=;
int r=0x3f3f3f3f;
while(r-l>)
{
int mid=(l+r)>>;
if(test(mid)) l=mid;
else r=mid;
}
cout<<l<<endl;
}
return ;
}

POJ2456(最大化最小值)的更多相关文章

  1. 二分法的应用:最大化最小值 POJ2456 Aggressive cows

    /* 二分法的应用:最大化最小值 POJ2456 Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...

  2. POJ3285 River Hopscotch(最大化最小值之二分查找)

    POJ3285 River Hopscotch 此题是大白P142页(即POJ2456)的一个变形题,典型的最大化最小值问题. C(x)表示要求的最小距离为X时,此时需要删除的石子.二分枚举X,直到找 ...

  3. POJ_2456_Agressive_cows_(二分,最大化最小值)

    描述 http://poj.org/problem?id=2456 有n个小屋,线性排列在不同位置,m头牛,每头牛占据一个小屋,求最近的两头牛之间距离的最大值. Aggressive cows Tim ...

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

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

  5. hihocoder 二分·二分答案【二分搜索,最大化最小值】 (bfs)

    题目 这道题做了几个小时了都没有做出来,首先是题意搞了半天都没有弄懂,难道真的是因为我不打游戏所以连题都读不懂了? 反正今天是弄不懂了,过几天再来看看... 题意:一个人从1点出发到T点去打boss, ...

  6. 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 ...

  7. 二分算法的应用——最大化最小值 POJ2456 Aggressive cows

    Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description Far ...

  8. POJ-2456.Aggressivecows.(二分求解最大化最小值)

    本题大意:在坐标轴上有n个点,现在打算在这n个点上建立c个牛棚,由于牛对厂主的分配方式表示很不满意,它很暴躁,所以它会攻击离它很近的牛来获得快感,这件事让厂主大大知道了,他怎么可能容忍?所以他决定有策 ...

  9. POJ:2456 Aggressive cows(z最大化最小值)

    描述 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1,000, ...

随机推荐

  1. iOS_数据存取(二)

    本节内容目录: 一.SQLite3 二.Core Data 一.SQlite3 SQLite3是⼀款开源的嵌入式关系型数据库,可移植性好.易使用.内存开销小SQLite3是⽆类型的,意味着你可以保存任 ...

  2. ubuntu+vm+ftp

    为了将windows下的文件传到linux中去,使用FZ来做服务器,在linux中进入ftp状态获取. 1.下载FileZilla服务器,在windows下安装就行了(试过汉化插件,用了就报错,所以还 ...

  3. Anaconda创建环境、删除环境、激活环境、退出环境

    Anaconda创建环境: //下面是创建python=3.6版本的环境,取名叫py36 conda create -n py36 python=3.6  删除环境(不要乱删啊啊啊) conda re ...

  4. echache缓存的简单使用方法

    1.需要echache的jar包 2.需要配置文件ehcache.xml和ehcache.xsd,主要是在ehcache.xml中进行配置 3.修改配置文件ehcache.xml  ,例如添加配置如下 ...

  5. 正则表达式java,javaScript应用

    dfa nfa 混合:捕获:断言:  正则引擎大体上可分为不同的两类:DFA和NFA,而NFA又基本上可以分为传统型NFA和POSIX NFA.   1.正则语法 捕获组: 没用()的字符都是一个一个 ...

  6. 单选框radio 选择问题

    <input type="radio"  name="test"/> <input type="radio"  name= ...

  7. 关于tcp的知识记录

    1. 概念 TCP(Transmission Control Protocol,传输控制协议),是一种面向连接的,可靠地,基于字节流的传输层通信协议.当应用层向TCP层发送用于网络间传输的用8位字节表 ...

  8. 【疯了C#】神奇的换肤(二)

    昨天参照了网上的资料练习了换肤,今天进一步的实现选择换肤 其实很简单,需要实现的功能如下点击combobox中的不同项目然后面板会自动的切换到相应的界面主题. 界面如下: 下述代码参照 “张隽永” 博 ...

  9. jsonp 解决跨域传输

    JSONP是JSON with Padding的略称.它是一个非官方的协议,它允许在服务器端集成Script tags返回至客户端,通过javascript callback的形式实现跨域访问(这仅仅 ...

  10. 安装SQL 2008失败 (win7 旗舰版 32位)

    本机系统 win7 32位 旗舰版 机器已经有sql 2005了,2008 不能安装成功,而且无任何错误提示. 那么通过windows install clean up (下载 windows ins ...