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. MYSQL常用命令——【转】

    MYSQL常用命令 1.导出整个数据库mysqldump -u 用户名 -p --default-character-set=latin1 数据库名 > 导出的文件名(数据库默认编码是latin ...

  2. 【BZOJ4481&JSOI2015】非诚勿扰(数学期望)

    听说JSOI有版权问题就不放图了 如果前面的文章里的图需要删掉请通知我 题意:有一些女的要挑一些男的,挑中的几率均为p.一个男的可以无限次被挑中.若女a选中男b,女c选中男d,a<c,b> ...

  3. 写一个Foreach帮助类,在razor中使用

    原文发布时间为:2011-05-05 -- 来源于本人的百度文章 [由搬家工具导入] A Better Razor Foreach Loop(razor delegate extension) htt ...

  4. .net开发工具集合

    原文发布时间为:2010-10-24 -- 来源于本人的百度文章 [由搬家工具导入] 原文出处:.NET Tools:Ten Must-Have Tools Every Developer Shoul ...

  5. jQuery插件(右下角弹出窗口)

    原文发布时间为:2010-03-07 -- 来源于本人的百度文章 [由搬家工具导入] jQuery插件(右下角弹出窗口) 收藏 源码下载 http://download.csdn.net/source ...

  6. VS2008 没办法太强大了

    原文发布时间为:2009-06-13 -- 来源于本人的百度文章 [由搬家工具导入] VS2008 太强大了。。。继续学习。。。。现在微软的某些强大功能也是针对 VS2008了。。。所以。。必须得装上 ...

  7. ef core 使用include进行外键连接查询

    在entity framework core中,如果两个实体涉及到外键连接,查询的时候默认是只查自身而不会去查询外键表的.如果想要让查询结果包含外键实体,则需要使用include方法来让查询结果包含外 ...

  8. 牛客网 牛客练习赛13 B.幸运数字Ⅱ-数组 or DFS

    B.幸运数字Ⅱ 链接:https://www.nowcoder.com/acm/contest/70/B来源:牛客网     这个题就是找出来数据范围内的所有的幸运数,然后直接区间累加起来就可以了. ...

  9. Codeforces 761D Dasha and Very Difficult Problem(贪心)

    题目链接 Dasha and Very Difficult Problem 求出ci的取值范围,按ci排名从小到大贪心即可. 需要注意的是,当当前的ci不满足在这个取值范围内的时候,判为无解. #in ...

  10. 伪全栈工程师做的有点简陋的ui设计

    站酷:http://www.zcool.com.cn/work/ZMjEwMDIxMDA=.html 这个app 叫自我时间管理  是一个 工具  管理自己开会 购物 健身 记账等 的提醒与管理,还可 ...