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. Flask文件目录----- db文件

    import sqlite3 import click from flask import current_app, g from flask.cli import with_appcontext d ...

  2. css的三个特性 背景透明设置

    关于行内元素(补充一点) 行内元素只能容纳文本或其他行内元素.(a特殊a里面可以放块级元素) 例子: 关于行高tip: 选择器的嵌套层级不应大于3级,位置靠后的限定条件应尽可能的精确. 属性定义必须另 ...

  3. 认识less和webstrom的less配置

    认识less和webstrom的less配置 今天完成的事情: 首先第一件事情是,整理一下常用的颜色摄取 #F1F1F1 google的设置页面的body的背景颜色 #FFF 为google的内容块的 ...

  4. Mahara-16.10 (Ubuntu 16.04)

    平台: Ubuntu 类型: 虚拟机镜像 软件包: mahara-16.10 commercial education elearning mahara open source 服务优惠价: 按服务商 ...

  5. java:错误Error,异常Excepition

    java中throw异常后代码还会继续执行吗 今天遇到一个问题,在下面的代码中,当抛出运行时异常后,后面的代码还会执行吗,是否需要在异常后面加上return语句呢? public void add(i ...

  6. word文档快速转换为PPT演示文稿

    方法一: 访问http://t.im/pdftoppt,点击继续浏览(会跳转至:https://smallpdf.com/cn/pdf-to-ppt): 打开word文档,设置为“横向”,输出为PDF ...

  7. Python-Django框架学习笔记——第二课:Django的搭建

    Django 环境搭建 一. 版本选择 Django 1.5.x 支持 Python 2.6.5 Python 2.7, Python 3.2 和 3.3. Django 1.6.x 支持 Pytho ...

  8. selenium安装及官方文档

    selenium-python官方文档: https://selenium-python.readthedocs.io/ python3.5已安装的情况下,安装示意图如下 命令行输入 pip3 ins ...

  9. Poj(2421),Prim最小生成树

    题目链接:http://poj.org/problem?id=2421 最小生成树的变形,有的村庄已经连接了,就直接把他们的权值赋为0,一样的做最小生成树,Prim算法. #include <s ...

  10. ROS机器人程序设计

      在<ROS机器人程序设计>中,在第二章创建节点时给出一个接收和发送的例子,但是按照书中步骤编译时,遇到按个三个问题,现在罗列出来解决方案供参考. 建议在工作空间直接输入 catkin_ ...