[Usaco2005 feb]Aggressive cows 愤怒的牛

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 407  Solved: 325
[Submit][Status][Discuss]

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 建造了一座很长的畜栏,它包括NN (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

HINT

 

Source

 
题解:
  题意是给定数轴上n个点,要取出其中m个点使得相邻的点的最小距离最大。
  所以二分答案判断即可。
 #include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstdio> #define N 200007
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<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,m,ans,l,r;
int a[N];
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 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. [ACM] poj 2456 Aggressive cows (二分查找)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5436   Accepted: 2720 D ...

  9. BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛( floyd + 二分答案 + 最大流 )

    一道水题WA了这么多次真是.... 统考终于完 ( 挂 ) 了...可以好好写题了... 先floyd跑出各个点的最短路 , 然后二分答案 m , 再建图. 每个 farm 拆成一个 cow 点和一个 ...

随机推荐

  1. CentOS7.2+MySQL5.7_ yum源方式_ 安装配置教程

    1)访问mysql官方网站 #访问网站 https://dev.mysql.com/downloads/file/?id=470281 2)下载安装包到linux #进入文件存放路径 cd /usr/ ...

  2. CPP-基础:字节对齐

    一.   什么是字节对齐,为什么要对齐?      现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特定的 ...

  3. linx vim 文件操作 ubuntu server 软件源

    mv /etc/danted.conf /etc/danted.conf.bak sudo wget https://files.cnblogs.com/files/marklove/danted.t ...

  4. Bootstrap历练实例:表单帮助文件

    Bootstrap表单控件可以在输入框input上有一个块级帮助文本,为了添加一个占用整个宽度的内容块,请在input后添加help-block. 实例: <!DOCTYPE html>& ...

  5. https 调用验证失败 peer not authenticated

    https 调用验证失败 peer not authenticated 报错日志: Caused by: javax.net.ssl.SSLPeerUnverifiedException: peer ...

  6. 人脸识别中的检测(在Opencv中加入了QT)

    #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include & ...

  7. ios软件设计中注意点

    1.取消系统自带渲染效果 2.取消屏幕旋转 3.项目中搜索丢失文件

  8. 洛谷 P1214 等差数列

    https://www.luogu.org/problemnew/show/P1214 首先暴力枚举可以凑出来的数,对于每个数进行标记. 对于每一个等差数列,当我们知道前两个数后即可以得出整个序列,那 ...

  9. Wash!!(HDU_6000)

    传送门:Wash! 题意:有n台洗衣机,m台烘干机,给出了每台机器处理意见衣服的时间,而且没见机器同时只能处理一件衣服.问如何选择机器才能使洗完衣服的时间最短. 思路:建两个优先队列,一个表示洗衣机, ...

  10. UVa-156-反片语

    这题比较精妙的是,我们对于单词重排,实际上是进行了标准化的处理,即按照字典序排序. 这样的话,就很方便地处理了单词的重排问题,我们不需要使用全排列函数进行排列尝试,我们直接化简为一,然后进行比较就可以 ...