Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7083   Accepted: 3522

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

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.

Source

 
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
using namespace std;
const int INF=0x4fffffff;
const int EXP=1e-;
const int MS=; int pos[MS];
int N,M; bool judge(int d)
{
int last=;
int now;
for(int i=;i<M;i++) //贪心法给第i头牛找宿舍
{
now=last+;
while(now<N&&pos[now]-pos[last]<d)
now++;
if(now>=N)
return false;
last=now;
}
return true;
} void solve()
{
sort(pos,pos+N);
int l=,r=INF;
while(r-l>) // 注意是区间,很多时候开区间更方便一些。
{
int mid=(l+r)/;
if(judge(mid))
l=mid;
else
r=mid;
}
printf("%d\n",l);
} int main()
{
while(scanf("%d%d",&N,&M)!=EOF)
{
for(int i=;i<N;i++)
scanf("%d",&pos[i]);
solve();
}
return ;
}

Aggressive cows 二分不仅仅是查找的更多相关文章

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

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

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

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

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

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

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

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

  5. POJ2456 Aggressive cows 二分

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

  6. [poj 2456] Aggressive cows 二分

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

  7. Aggressive Cows 二分

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

  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. EntityFramwork6连接MySql错误

    EntityFramwork6连接MySql错误 使用EF6连接MySql产生Exception: ProHub.ssdl(2,2) : 错误 0152: MySql.Data.MySqlClient ...

  2. 【132】iPad使用相关问题

    1.想iPad中导入音乐文件,保留原音乐文件名称 [方法]需要删除音乐文件中的“标题”和“参与创作的艺术家”,直接删除的话,效率比较低,需要借助一款软件——foobar2000,选中导入的文件,然后属 ...

  3. WebRtc VoiceEngine代码解析

    WebRtc中VoiceEngine可以完成大部分的VOIP相关人物,包括采集.自动增益.噪声消除.回声抑制.编解码.RTP传输.下边我们通过代码来解析Voe中处理流程: 创建VoiceEngine和 ...

  4. windows下php cli模式,提示出错

    搞一下php cli,一直提示,找不到php_pdo_mssql.dll模块 原来是在php.ini加载了该模块,但ext下又没有该模块 即便下载了一个php_pdo_mssql.dll,但还是会提示 ...

  5. Linux之sed,awk(流编辑器)

    sed:  s----substitute(替换) 1. 文本替换(使用-i选项,可以将结果应用于原文件) many people在进行替换之后,借助重定向来保存文件(未使用-i选项): $ sed  ...

  6. 计数排序详解以及java实现

    前言 我们知道,通过比较两个数大小来进行排序的算法(比如插入排序,合并排序,以及上文提到的快速排序等)的时间复杂度至少是Θ(nlgn),这是因为比较排序对应的决策树的高度至少是Θ(nlgn),所以排序 ...

  7. JAX-RS入门 一 :基础

    简介 JAX-RS是一套用java实现REST服务的规范,提供了一些标注将一个资源类,一个POJOJava类,封装为Web资源.标注包括: @Path,标注资源类或方法的相对路径 @GET,@PUT, ...

  8. Libvirt 虚拟化库剖析

    讲到向外扩展计算(比如云计算),libvirt 可能是您从未听说过的最重要的库之一.libvirt 提供一种虚拟机监控程序不可知的 API 来安全管理运行于主机上的来宾操作系统.libvirt 本身不 ...

  9. jquery Mobile点击显示加载等待效果

    点击某个按钮或链接时,触发等待加载效果: <script> <!-- $(document).bind("mobileinit", function(){ }); ...

  10. jqery和js如何判断checkbox是否选中

    jquery: <div id="divId" class="divTable"> <div class="tableBody&qu ...