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?

农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1,000,000,000). 但是,John的C (2 <= C <= N)头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争斗。为了不让牛互相伤害。John决定自己给牛分配隔间,使任意两头牛之间的最小距离尽可能的大,那么,这个最大的最小距离是什么呢

Input

* Line 1: Two space-separated integers: N and C * Lines 2..N+1: Line i+1 contains an integer stall location, xi

第一行:空格分隔的两个整数N和C

第二行---第N+1行:i+1行指出了xi的位置

Output

* Line 1: One integer: the largest minimum distance

第一行:一个整数,最大的最小值

Sample Input

5 3
1
2
8
4
9

Sample Output

3

把牛放在1,4,8这样最小距离是3

题意是给定数轴上n个点,要取出其中m个点使得相邻的点的最小距离最大。

看到最小值最大……呵呵……果断二分

每次枚举一个最小距离k,然后贪心验证。

首先第一个点是必取的,然后后面的点一旦超过k就取。因为如果超过k还不取就相当于寻找更“不优”的一组可行解,不取白不取

最后判断一下有没有取光m个点就行了

#include<cstdio>
#include<algorithm>
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,ans,l,r;
int a[];
inline bool jud(int x)
{
int tot=,now=,save=;
while (tot<=m)
{
while (a[now]-a[save]<x && now<=n) now++;
if (now>n && tot<m) return ;
tot++;save=now;
if (tot==m) return ;
}
return ;
}
int main()
{
n=read();
m=read();
for (int i=;i<=n;i++) a[i]=read();
sort(a+,a+n+);
l=;r=;
while (l<=r)
{
int mid=(l+r)>>;
if (jud(mid)){ans=mid;l=mid+;}
else r=mid-;
}
printf("%d",ans);
}

bzoj1734 愤怒的牛的更多相关文章

  1. bzoj1734 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案

    [Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 407  Solved: 325[S ...

  2. BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )

    最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #inc ...

  3. 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛

    1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 217  Solved: ...

  4. bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛

    1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Description Farmer John has built a new long barn, with N ...

  5. 题解 yzoj1663: 愤怒的牛(二分) yzoj1662: 曲线(三分)

    话说二分和三分的题还没有整理过,就趁这两题来整理下笔记 先讲讲关于二分,对于二分的具体边界长期以来对我来说都是个玄学问题,都是边调边拍改对的.思路大体是确定左边界l,和有边界r,判断满足条件缩小范围. ...

  6. bzoj1734 [Usaco2005 feb]Aggressive cows 愤怒的牛

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

  7. B1734 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案

    水题,20分钟AC,最大值最小,一看就是二分答案... 代码: Description Farmer John has built a <= N <= ,) stalls. The sta ...

  8. loj1011愤怒的牛

    题目描述 原题来自:USACO 2005 Feb. Gold 农夫约翰建造了一座有 n 间牛舍的小屋,牛舍排在一条直线上,第 i 间牛舍在 x_i 的位置,但是约翰的 m 头牛对小屋很不满意,因此经常 ...

  9. bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛【二分+贪心】

    二分答案,贪心判定 #include<iostream> #include<cstdio> #include<algorithm> using namespace ...

随机推荐

  1. Nodejs以后台服务启动

    1: 从网上查找  LINUX中我们可以使用这种简单的方式让node.js在后台运行: nohup node your_app.js & 经多次实验一直没有成功   2:使用 forever ...

  2. 连载《一个程序猿的生命周期》- 44.感谢,我从事了IT相关的工作

    感谢博客园一直以来的支持,写连载都是在这里首发,相比较CSDN和开源中国气氛要好的多. 节前,想以此篇文章结束<一个程序猿的生命周期>的<生存>篇,对过10的年做一个了断,准备 ...

  3. JavaScript 基础回顾——函数

    在JavaScript中,函数也是一种数据类型,属于 function 类型,所以使用Function关键字标识函数名.函数可以在大括号内编写代码并且被调用,作为其他函数的参数或者对象的属性值. 1. ...

  4. 《In Search of an Understandable Consensus Algorithm》翻译

    Abstract Raft是一种用于管理replicated log的consensus algorithm.它能和Paxos产生同样的结果,有着和Paxos同样的性能,但是结构却不同于Paxos:它 ...

  5. 我的CS考研路

    说在前面 从去年7月15号正式准备考研以来,直到今天,3月19号,一共经历8个多月,考研初步告捷,在此想跟大家分享一下自己的经验,希望能对接下来考研的学弟学妹们有所帮助. 首先介绍下我自己的情况,本科 ...

  6. POJ2104 K-th Number[主席树]【学习笔记】

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 51440   Accepted: 17594 Ca ...

  7. java内部类

    1.内部类 2.内部类的名字不会单独存在,根据外部类名的存在而存在.内部类的名字可以和外部其他类的名字一样. 3.这个this.num打印的是inner类里面的num 4.在内部类访问外部类成员变量方 ...

  8. .net Global.asax文件使用

    一.Application_start: 第一个访问网站的用户会触发该方法. 通常会在该方法里定义一些系统变量,如聊天室的在线总人数统计,历史访问人数统计的初始化等等均可在这里定义. Applicat ...

  9. selenium phantomjs 关闭问题

    一个获取供应商余额的项目中,使用了 selenium 来爬虫,原因是获取余额不用太频繁,对性能要求不高.第二是 selenium 更好应对各种页面. 项目中,selenium webdriver使用了 ...

  10. cf Round 607

    A.Chain Reaction(DP+二分) 题意:一排有n个灯塔,每个灯塔给出坐标xi和力量yi,每次从最右边依次点亮灯塔,每点亮一个灯塔,它左边的距离它yi范围内的灯塔将受到损坏.现在允许在最右 ...