Aggressive cows

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 18313 Accepted: 8716

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.


解题心得:

  1. 有n间小屋,n个小屋排在一列,每个小屋距离原点有一个距离,要将m头牛放在小屋中,要使两头牛之间的距离尽量的大,如果最大化牛直接的距离,那么最近两头牛之间的距离最大是多少。
  2. 看到这个最大化最小值那么首先想到的就是二分,最大之间求最小,最小之间求最大,平均之间求最大都是二分。每次二分最大距离,然后按照这个距离安排,看能否用n个小屋安排下,不能那么减少距离,如果可以那么增大距离。

#include <algorithm>
#include <cstring>
#include <stdio.h>
using namespace std;
const int maxn = 1e5+10;
int pos[maxn],n,c; void init() {
for(int i=0;i<n;i++)
scanf("%d",&pos[i]);
sort(pos,pos+n);
} bool checke(int len){
int cnt = 1,now = pos[0];
for(int i=1;i<n;i++) {
if (pos[i] - now >= len) {
cnt++;
now = pos[i];
}
}
return cnt >= c;
} int binary_search() {
int l = 0,r = 1e9+10;
while(r-l > 1) {
int mid = (l+r)/2;
if(checke(mid))
l = mid;
else
r = mid;
}
return l;
} int main() {
while(scanf("%d%d",&n,&c) != EOF) {
init();
int ans = binary_search();
printf("%d\n",ans);
}
return 0;
}

POJ:2456-Aggressive cows的更多相关文章

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

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

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

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

  3. 二分搜索 POJ 2456 Aggressive cows

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

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

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

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

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

  6. POJ 2456 Aggressive cows

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

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

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

  8. poj 2456 Aggressive cows 贪心+二分

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

  9. POJ 2456 Aggressive cows(贪心 + 二分)

    原题链接:Aggressive cows 题目大意:农夫 建造了一座很长的畜栏,它包括  个隔间,这些小隔间依次编号为. 但是, 的  头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争 ...

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

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

随机推荐

  1. Springboot开源项目实例整理

    https://www.imooc.com/article/67664 ---------------------------------------------------------------- ...

  2. python模块介绍和 import本质

    模块的定义: 用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质上就是.py结尾的python文件. 包的定义: 用来从逻辑上组织模块的,本质上就是一个目录.(必须有一个__ ...

  3. 带你了解强大的Cadence家族,你可能只用到了它1/10的工具

    [转载自 SI-list[中国]http://mp.weixin.qq.com/s/qsdfzQwIVjvwHXuCdvrPXA ] 本篇对2017年初版Cadence的全套所有EDA工具的技术特性特 ...

  4. Oracle三种循环例题:打印九九乘法表

    数据库SQL三种循环语句(For.While.Loop) --如果要将执行结果输出,需要先执行 setserveroutput on 命令,在窗口里显示服务器输出信息 set serveroutput ...

  5. day003-List类、Set类

    (一) 知识回顾1. day002总结 泛型没有多态.如果泛型能实现多态,那么数据类型就不安全了.违背了设计泛型的初衷. 1.1 伪泛型 泛型只存在编译器,编译生成的字节码中,不存在泛型变量的. 1. ...

  6. Markdown快速上手指南

    Markdown快速上手指南 1.Markdown介绍 markdown可以实现快速html文档编辑,格式优没,并且不需要使用html元素. markdown采用普通文本的形式,例如读书笔记等易于使用 ...

  7. [转载]互联网 免费的WebService接口

    股票行情数据 WEB 服务(支持香港.深圳.上海基金.债券和股票:支持多股票同时查询) Endpoint: http://webservice.webxml.com.cn/WebServices/St ...

  8. 使用命令创建jenkins的job,解决jenkinsapi.custom_exceptions.JenkinsAPIException错误

    如果你使用 Python 2.7.12,Jenkins版本为Jenkins ver. 2.22,你使用我上面一种提到的修改的以下代码可以进行Jenkins的job复制 http://www.cnblo ...

  9. python url库学习

    参考资料:http://cuiqingcai.com/947.html urllib,urlib2是python自带的库. urlopen(url,data,timeout),三个参数. 第一个是地址 ...

  10. DOM(三):querySelector和querySelectorAll

    querySelector()方法querySelector()方法接收一个css选择符,返回与该模式匹配的第一个元素,如果没有找到匹配的元素,返回null. //取得body元素 var body ...