题目传送门 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. 2018-5-28-win10-uwp-动态修改ListView元素布局

    title author date CreateTime categories win10 uwp 动态修改ListView元素布局 lindexi 2018-05-28 15:15:54 +0800 ...

  2. js获取dom节点

    var s= document.getElementById("test");del_ff(s); //清理空格var chils= s.childNodes; //得到s的全部子 ...

  3. SpringBoot 集成 Activiti 一路踩得坑

    由于项目需要,本人开始在项目Spring boot 中集成工作流引擎Activiti.由于第一次集成,一路上步步都是坑,怪我没有先去看官方文档.现将一路上遇到的问题一一记录. 一. 环境配置 1.项目 ...

  4. Laravel基本使用、生成Cookie、返回视图、JSON/JSONP、文件下载及重定向

    https://yq.aliyun.com/ziliao/23889 1.Response篇 1.1 基本响应 最基本的HTTP响应只需在路由闭包或控制器动作中返回一个简单字符串即可,但是具体业务逻辑 ...

  5. 2018-9-1-win2d-画出好看的图形

    title author date CreateTime categories win2d 画出好看的图形 lindexi 2018-09-01 16:25:40 +0800 2018-2-13 17 ...

  6. 中和IOS七层架构和TCP/IP四层架构的五层架构

    五层架构分别为应用层.运输层.网络层.数据链路层.物理层. IOS架构把应用层又细分为应用层.表示层.会话层 TCP/IP把网络层改名网际层,数据链路层和物理层结合成网络接口层 其实只要学习五层协议, ...

  7. cmd 如何跨驱动器移动文件夹

    如果在命令行或 cmd 批处理文件通过 move 移动文件夹的时候,移动的文件夹是跨驱动器的,那么将会显示拒绝访问 解决通过 move 移动文件夹到不同的驱动器需要通过先复制文件夹到另一个驱动器,然后 ...

  8. dotnet 如何在 Mock 模拟 Func 判断调用次数

    在 dotnet 程序有很好用的 Mock 框架,可以用来模拟各种接口和抽象类,可以用来测试某个注入接口的被调用次数和被调用时传入参数.本文告诉大家如何在 Mock 里面模拟一个 Func 同时模拟返 ...

  9. Windows Server Core Remote Manage Hyper-V

    原帖:https://serverfault.com/questions/852144/how-do-i-remotely-manage-hyper-v-2016-standalone-via-win ...

  10. There is already an open DataReader associated with this Connection which must be closed first EF

    废话不多说,直接 上图,及解决办法.不过问题还不是太清楚到底为什么会出现这个情况..........