bzoj1734 愤怒的牛
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
1
2
8
4
9
Sample Output
把牛放在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 愤怒的牛的更多相关文章
- bzoj1734 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案
[Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 407 Solved: 325[S ...
- BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )
最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #inc ...
- 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛
1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 217 Solved: ...
- bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛
1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Description Farmer John has built a new long barn, with N ...
- 题解 yzoj1663: 愤怒的牛(二分) yzoj1662: 曲线(三分)
话说二分和三分的题还没有整理过,就趁这两题来整理下笔记 先讲讲关于二分,对于二分的具体边界长期以来对我来说都是个玄学问题,都是边调边拍改对的.思路大体是确定左边界l,和有边界r,判断满足条件缩小范围. ...
- bzoj1734 [Usaco2005 feb]Aggressive cows 愤怒的牛
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
- B1734 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案
水题,20分钟AC,最大值最小,一看就是二分答案... 代码: Description Farmer John has built a <= N <= ,) stalls. The sta ...
- loj1011愤怒的牛
题目描述 原题来自:USACO 2005 Feb. Gold 农夫约翰建造了一座有 n 间牛舍的小屋,牛舍排在一条直线上,第 i 间牛舍在 x_i 的位置,但是约翰的 m 头牛对小屋很不满意,因此经常 ...
- bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛【二分+贪心】
二分答案,贪心判定 #include<iostream> #include<cstdio> #include<algorithm> using namespace ...
随机推荐
- Linux 内核进程管理之进程ID
Linux 内核使用 task_struct 数据结构来关联所有与进程有关的数据和结构,Linux 内核所有涉及到进程和程序的所有算法都是围绕该数据结构建立的,是内核中最重要的数据结构之一.该数据结构 ...
- 【JavaScript Demo】回到顶部功能实现
随着网站的不断发展,需要展示的内容也越来越丰富,这导致网页上能展示的内容越来越多.当内容堆积影响了用户体验,就需考虑如何提升用户体验.在这一系列的改动中,“回到顶部”的功能成为了一个经典. 1.页面布 ...
- BZOJ 1070: [SCOI2007]修车 [最小费用最大流]
1070: [SCOI2007]修车 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 4936 Solved: 2032[Submit][Status] ...
- c#读写xml文件
using System; using System.Collections.Generic; using System.Xml; using System.Text.RegularExpressio ...
- CDN缓存机制
CDN也叫内容分发网络,是一个经策略性部署的整体系统,包括分布式储存.负载均衡.网络请求的重定向和内容管理4个要件.而其中内容管理和全局的网络流量管理是CDN的核心所在.通过用户就进行和服务器负载的判 ...
- PAT 1036. 跟奥巴马一起编程(15)
美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统.2014年底,为庆祝"计算机科学教育周"正式启动,奥巴马编写了很简单的计算机代 ...
- Android 图文数据JSON解析,金山词霸每日一句API的调用
金山词霸开发的免费API http://open.iciba.com/dsapi/ 数据格式为 {","name":"\u7535\u5f71\u7ecf\u5 ...
- 分享录制的正则表达式入门、高阶以及使用 .NET 实现网络爬虫视频教程
我发布的「正则表达式入门以及高阶教程」,欢迎学习. 课程简介 正则表达式是软件开发必须掌握的一门语言,掌握后才能很好地理解到它的威力: 课程采用概念和实验操作 4/6 分隔,帮助大家理解概念后再使用大 ...
- docfx预热中
奋战了几个月,docfx终于有些像样了. 预览文档: http://aspnet.github.io/docfx/ 源代码正在准备开源中 Nuget包很快会发布 FAQ: Q: docfx是什么? A ...
- 认真理解 图片 <img> background-image
<img src="" width="" height="" alt=""> 一:图片的宽度和高度的关系? ...