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

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

解题思路:

这道题虽然不长,可是读了好几遍才读懂。有n个牛舍,每个牛舍有自己的位置xi(可以理解为坐标),把c头牛放在这n个牛舍里面,要求最近的两头牛之间的最大距离。

比如测试数据 5个牛舍 3头牛 牛舍坐标1 2 8 4 9

先按贪心,第一头牛肯定被放在第一个牛舍里,排序 1 2 4 8 9

第二头牛可以放在2,也可以放在4,假设第二头牛放在2 ,与第一头牛的距离为1 ,假设第三头放在9,与第二头的距离为7,那么,最近的两头牛之间的最大距离为1

最有解为 第一头牛放在1  第二头牛放在4 ,第三头牛放在9 ,那么 4-1=3  9-4=5 ,最近的两头牛之间的最大距离为3

也就是求相邻两头牛之间距离的最小值(取最大)。这个距离采用二分的形式进行判断。

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn=100002;
const int inf=0x7fffffff;
int x[maxn];
int n,c; bool ok(int m)
{
int last=0;
for(int i=1;i<c;i++)
{
int cur=last+1;//cur指当前的牛舍位置
while(cur<n&&x[cur]-x[last]<m)
cur++;
if(cur==n)//只放下第一头牛,无法放下第二头牛,使二者的距离大于等于m
return false;
last =cur;//把第i头牛放在编号为cur的牛舍里,这里牛的头数和牛舍的编号都是从0开始的
}
return true;
}
int main()
{
cin>>n>>c;
for(int i=0;i<n;i++)
scanf("%d",&x[i]);
sort(x,x+n);
int l=0,r=inf;//这里r也可以写成题目里的最大距离1000000000
for(int i=0;i<100;i++)
{
int m=(l+r)/2;
if(ok(m))
l=m;
else r=m;
}
cout<<l<<endl;
return 0;
}

[ACM] poj 2456 Aggressive cows (二分查找)的更多相关文章

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

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

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

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

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

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

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

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

  5. poj 2456 Aggressive cows 二分 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2456 解法 使用二分逐个尝试间隔距离 能否满足要求 检验是否满足要求的函数 使用的思想是贪心 第一个点放一头牛 后面大于等于尝试的距离才放 ...

  6. [poj 2456] Aggressive cows 二分

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

  7. POJ 2456 Aggressive cows ( 二分 && 贪心 )

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

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

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

  9. 二分搜索 POJ 2456 Aggressive cows

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

随机推荐

  1. python __init__.py用途

    转自http://www.cnpythoner.com/post/2.html Python中的Module是比较重要的概念.常见的情况是,事先写好一个.py文 件,在另一个文件中需要import时, ...

  2. php接口post提交方法 (改良版)

    $postData = "reqType=2&caller=15088719619&called=15068722845"; $result = send_post ...

  3. Windows下Python工具pip的安装

    1.打开pip的文档官网 https://pip.pypa.io/en/stable/ ,进入installation.在installation里,我们需要的是get-pip.py这个脚本. 选中后 ...

  4. Reactjs 入门基础(一)

    实例中我们引入了三个库: react.min.js .react-dom.min.js 和 browser.min.js: 1,react.min.js -React 的 核心库 2,react-do ...

  5. JavaScript:JSON

      JSON是一种数据格式,它并不从属于JavaScript,很多语言都有JSON的解析器和序列化器. 语法 JSON可以表示三种类型: 简单值:使用与JavaScript相同的语法,可以在JSON中 ...

  6. 解决关于ArcGIS10.2服务手动启动的问题

    解决关于ArcGIS10.2服务手动启动的问题 C:\program files\arcgis\license10.2\bin> lmgrd -z -c service.txt新建一文本,将上面 ...

  7. a + b + c 求和

    #include <iostream> int main() { std::cout << "请输入三个数字,以空格分隔,按回车键结束:" << ...

  8. 《Linux内核设计与实现》读书笔记 第二章 从内核出发

    一.获取内核源码 1. Git git实际上是一种开源的分布式版本控制工具. Linux作为一个开源的内核,其源代码也可以用git下载和管理 - 获取最新提交到版本树的一个副本 - $ git clo ...

  9. java代码性能优化总结(转载)

    原文链接:http://developer.51cto.com/art/201511/496263.htm 前言 代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改 ...

  10. 比较两个Long对象值

    比较两个Long对象的值是否相等,不可以使用双等号进行比较,(long int Integer可以用双等号进行比较)可以采用如下方式: 1.使用equals方法进行比较 Long a=new Long ...