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

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

USACO 2005 February Gold



题目链接:

id=2456">http://poj.org/problem?

id=2456



题目大意:一个数轴上n个点,每一个点一个整数值,有c个物品。要放在这些点的某几个上,求怎么放能够使随意两个物品间距离的最小值最大,求这个最大值



题目分析:最小值最大,典型二分题。二分距离的值推断

#include <cstdio>
#include <algorithm>
using namespace std;
int const INF = 0x3fffffff;
int const MAX = 1e5 + 5;
int d[MAX];
int n, c; int cal(int m)
{
int ans = 0, now = d[0];
for(int i = 1; i < n; )
{
while(d[i] < now + m)
i ++;
ans ++;
now = d[i];
}
return ans;
} int main()
{
scanf("%d %d", &n, &c);
for(int i = 0; i < n; i++)
scanf("%d", &d[i]);
sort(d, d + n);
int r = d[n - 1] - d[0], l = 0, ans = 0;
while(l <= r)
{
int m = (l + r) / 2;
int num = cal(m);
if(num >= c)
{
ans = m;
l = m + 1;
}
else
r = m - 1;
}
printf("%d\n", ans);
}

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

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

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

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

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

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

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

  4. [poj 2456] Aggressive cows 二分

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

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

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

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

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

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

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

  8. 二分搜索 POJ 2456 Aggressive cows

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

  9. POJ 2456 Agressive cows(二分)

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

随机推荐

  1. HDU 4779:Tower Defense

    Tower Defense Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)T ...

  2. t4 template multi file output

    1.Manager.ttinclude <#@ assembly name="System.Core"#> <#@ assembly name="Sys ...

  3. JS读取/创建本地文件及目录文件夹的方法

    原文链接:http://www.cnblogs.com/ayan/archive/2013/04/22/3036072.html 注:以下操作只在IE下有效! Javascript是网页制作中离不开的 ...

  4. MySql授权和撤销权限操作

    MySQL 赋予用户权限命令的简单格式可概括为: grant 权限 on 数据库对象 to 用户 用户后面可以加@'ip地址' identified by '密码' 例如: ' ' 上面的语句表示将 ...

  5. 分享C#识别图片上的数字

    通过Emgu实现对图片上的数字进行识别.前期步骤:1.下载Emgu安装文件,我的版本是2.4.2.1777.3.0版本则实现对中文的支持.2.安装后需填写环境变量,环境变量Path值后加入Emgu安装 ...

  6. window golang1.12.4 安装配置&IDE vscode

    step 1:下载golang golang官网地址是 https://golang.org/dl/ 如果没有墙的话,可以用这个地址 https://golang.google.cn/dl/ 如图: ...

  7. guacamole 基本学习使用

    基本介绍: Guacamole 是一个基于 HTML 5 和 JavaScript 的 VNC 查看器,服务端基于 Java 的 VNC-to-XML 代理开发.要求浏览器支持HTML5. Apach ...

  8. springBoot 读取配置文件yml中的信息

    yml中自定义一些变量 var: analyze_url: test ocr_url: test microsoft_key: test 映射到类变量中 @Getter @Component publ ...

  9. 长安大学第四届“迎新杯”程序设计竞赛 F 打铁的箱子【数学/进制思维/折半枚举】

    题目描述 作为彩虹岛上最擅长打铁的人,

  10. 灰度发布+A/B测试

    一起需要提的还有灰度发布. 在方法论上都属于试错法. A/B测试就是上两个方案,部署后看效果.根据效果和一些结果参数决定采用哪个方案.灰度发布是切一部分业务使用新方案,看效果如何,是否有bug,会遇到 ...