题目传送门 POJ 2456

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.

 
题目大意:   
  有N个隔间和C头牛 , 给出n个隔间的位置 ,把每头牛分到一个隔间 ,问怎么分才能使任意两头牛之间的最小距离尽可能的大,求这个最大的 最小距离。
 
解题思路:
  显然,这是一个最小值最大的问题 ,先考虑二分是否可行 ,如果可行首选二分。
  隔间的位置是无序的 ,我们先把隔间按位置从小到大进行排序,显然任意两头牛之间距离最小为0,最大为a[n-1]-a[0],我们在这个区间内二分求值。枚举每个mid,通过判断以这个值为最小距离是否能放下C头牛,然后不断缩小区间范围求得最优解。
 
#include<stdio.h>
#include<algorithm>
using namespace std;
const int N = ;
int a[N],n,m;
int _is(int x)
{
int cnt=,y=a[];
for (int i=; i<n; i++)
if (a[i]-y>=x)
{
cnt++;
y=a[i];
if (cnt+==m)
return ;
}
return ;
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=; i<n; i++)
scanf("%d",&a[i]);
sort(a,a+n);
int l=,r=a[n-]-a[];
while(l<=r)
{
int mid =l+(r-l)/;
if ( _is(mid) )
l=mid+;
else r=mid-;
}
if (!_is(l)) l--;
printf("%d\n",l);
return ;
}
 
 

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

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

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

  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: 18099   Accepted: 8619 ...

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

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

  5. [poj 2456] 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 (二分查找)

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

  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 ...

  10. POJ 2456 Agressive cows(二分)

    POJ 2456 Agressive cows 农夫 John 建造了一座很长的畜栏,它包括N (2≤N≤100,000)个隔间,这 些小隔间的位置为x0,...,xN-1 (0≤xi≤1,000,0 ...

随机推荐

  1. laravel使用加载进行优化

    两种方式: 1.使用:with $posts=Post::orderby('created_at','desc')->withCount(['comments','zans'])->wit ...

  2. const(每个对象中的常量), static const(类的编译时常量)

    1 每个对象中的常量 --- const数据成员 const限定,意味着“在该对象生命周期内,它是一个常量”. 关键字const 使被限定的量为常量 在该类的每个对象中,编译器都为其const数据成员 ...

  3. [转]Node.js中package.json中^和~的区别

    webpack 项目的package.json 文件列出了项目所依赖的插件和库,同时也给出了对应的版本说明,但是在版本说明前面还有个符号:'^'(插入符号)和'~'(波浪符号),总结了下他们之间的区别 ...

  4. window 系统下修改`CMD`的编码格式的方法,`CHCP` 的 使用

    CHCP的使用 CHCP是一个计算机指令,能够显示或设置活动代码页编号. 一般上是在命令提示框中使用,用来查询和修改命令提示框的编码格式 具体使用方法 查看活动代码页编号 方式1: >>& ...

  5. 【75.28%】【codeforces 764B】Decoding

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 测试驱动开发实践—从testList开始

    [内容指引]运行单元测试:装配一条数据:模拟更多数据测试列表:测试无搜索列表:测试标准查询:测试高级查询. 一.运行单元测试 我们以文档分类(Category)这个领域类为例,示范如何通过编写测试用例 ...

  7. 转 最近5年183个Java面试问题列表及答案[最全]

    Java 面试随着时间的改变而改变.在过去的日子里,当你知道 String 和 StringBuilder 的区别(String 类型和 StringBuffer 类型的主要性能区别其实在于 Stri ...

  8. eclipse要修改的配置

    1. 修改 html的字体大小 window->preferences->General--> appearance--> Colors and fonts-->basi ...

  9. Java面向对象程序设计第14章3-8和第15章6

    Java面向对象程序设计第14章3-8和第15章6 3.完成下面方法中的代码,要求建立一个缓冲区,将字节输入流中的内容转为字符串. import java.io.*; public class tes ...

  10. Web基础了解版10-Filter-Listener

    Filter 对于WEB应用来说,过滤器是一个驻留在服务器中的WEB组件,他可以截取客户端和WEB资源之间的请求和响应信息. 在一个WEB应用中可以部署多个过滤器,多个过滤器就组成了一个过滤器链,请求 ...