题目链接

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.

分析:

类似最大化最小值或者最小化最大值的问题,通常用二分搜索法就可以很好的解决。

定义:C(d):表示,可以安排牛的位置使得最近的两头牛的距离不小于d

那么问题就变成了求满足C(d)的最大的d。另外,最近的间距不小于d也可以说成是所有牛的间距都不小于d,因此就有,

C(d),表示,可以安排牛的位置使得任意的牛的间距都不小于d

这个问题的判断使用贪心法便可以很好的解决。

1.对牛舍的位置x进行排序

2.把第一头牛放入x0的位置

3.如果第i头牛放入了xj的话,第i+1头牛要放入满足xj+d的最小的xk中,

对x的排序只需要在最开始的时候进行一次就可以了,每一次判断对每头牛最多进行一次处理。

代码:

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int N,M;
int x[100009];
bool C(int d)
{
int last=0;
for(int i=1; i<M; i++)///看看这个距离能不能放下
{
int crt=last+1;
while(crt<N&&x[crt]-x[last]<d)
crt++;
if(crt==N) return false;
last=crt;
}
return true;
} void solve()
{
sort(x,x+N);
int lb=0,ub=0x3f3f3f3f;
while(ub-lb>1)
{
int mid=(lb+ub)/2;
if(C(mid)) lb=mid;
else ub=mid;
}
printf("%d\n",lb);
} int main()
{
scanf("%d%d",&N,&M);
for(int i=0; i<N; i++)
scanf("%d",&x[i]);
solve();
return 0;
}

POJ 2456 Aggressive cows ( 二分搜索)的更多相关文章

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

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

  2. 二分搜索 POJ 2456 Aggressive cows

    题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...

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

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

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

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

  5. POJ 2456 Aggressive cows (二分)

    题目传送门 POJ 2456 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) s ...

  6. [ACM] poj 2456 Aggressive cows (二分查找)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5436   Accepted: 2720 D ...

  7. POJ 2456 Aggressive cows

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11192   Accepted: 5492 ...

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

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

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

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

  10. poj 2456 Aggressive cows 贪心+二分

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25944   Accepted: 11982 ...

随机推荐

  1. LintCode-374.螺旋矩阵

    螺旋矩阵 给定一个包含 m x n 个要素的矩阵,(m 行, n 列),按照螺旋顺序,返回该矩阵中的所有要素. 样例 给定如下矩阵: [     [ 1, 2, 3 ],     [ 4, 5, 6 ...

  2. iOS- 无处不在,详解iOS集成第三方登录(SSO授权登录<无需密码>)

    1.前言   不多说,第三登录无处不在!必备技能,今天以新浪微博为例. 这是上次写的iOS第三方社交分享:http://www.cnblogs.com/qingche/p/3727559.html 可 ...

  3. Winform 子窗体设置刷新父窗体

    方法1:所有权法 父窗体:Form1    子窗体:Form2 //Form1:窗体代码 //需要有一个公共的刷新方法 public void Refresh_Method() { //... } / ...

  4. Node js MySQL简单操作

    //win7环境下node要先安装MySQL的相关组件(非安装MySQL数据库),在cmd命令行进入node项目目录后执行以下语句 //npm install mysql var mysql = re ...

  5. 腾讯云 activeMQ Illegal character in hostname at index 7

    查找问题步骤: 1.  /usr/local/apache-activemq-5.9.1/data/activemq.log 看一下这个.log后缀的启动日志,可以将它下载下来再看. 先尝试修改配置文 ...

  6. oracle 删除数据恢复

    select *  from taxi_comp_worksheet_ext   as of timestamp to_timestamp('2014-09-22 13:00:00', 'yyyy-m ...

  7. 第43天:事件对象event

    一.事件对象事件:onmouseover. onmouseout. onclickevent //事件的对象 兼容写法:var event = event || window.event; event ...

  8. 【】Python】异常处理try...except、raise

    一.try...except 有时候我们写程序的时候,会出现一些错误或异常,导致程序终止.例如,做除法时,除数为0,会引起一个ZeroDivisionError 例子: 1 2 3 4 a=10 b= ...

  9. SPD各模块总结

    一.用户角色绑定节点 1.库存操作员.库存主管.验货操作员:绑定任一节点 2.采购操作员.公药操作员:只能绑定药库节点 3.退库操作员.药品申领员:绑定药库以外的节点 二.采购计划模块 1.采购计划的 ...

  10. BZOJ4300 绝世好题(动态规划)

    设f[i][j]为前i个数中所选择的最后一个数在第j位上为1时的最长序列长度,转移显然. #include<iostream> #include<cstdio> #includ ...