1734: [Usaco2005 feb]Aggressive cows 愤怒的牛
1734: [Usaco2005 feb]Aggressive cows 愤怒的牛
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 217 Solved: 175
[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
1
2
8
4
9
Sample Output
把牛放在1,4,8这样最小距离是3
HINT
Source
题解:一道经典的二分答案题,对于可行的解的可能范围进行二分求最大合法值,然后再check函数里面采取的是 \( O\left(N \right) \) 的判断法,最坏情况下将会将整个数列跑一遍,这样一来复杂度为 \( O\left(N\log \frac{X_N-X_1}{C-1} \right) \) ,于是这样子就A掉了
实际上更好的办法是在check里面再套一个二分查找数字,实际效果将强于直接跑,复杂度 \( O\left(C\log N \log \frac{X_N-X_1}{C-1} \right) \)
/**************************************************************
Problem:
User: HansBug
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ var
i,j,k,l,m,n,r:longint;
a:array[..] of longint;
procedure sort(l,r:longint);
var i,j,x,y:longint;
begin
i:=l;j:=r;x:=a[(l+r) div ];
repeat
while a[i]<x do inc(i);
while a[j]>x do dec(j);
if i<=j then
begin
y:=a[i];a[i]:=a[j];a[j]:=y;
inc(i);dec(j);
end;
until i>j;
if i<r then sort(i,r);
if l<j then sort(l,j);
end;
function check(x:longint):boolean;
var i,j,k,l:longint;
begin
a[]:=-x-;l:=;j:=;
for i:= to n do
begin
if (a[i]-a[l])>=x then
begin
inc(j);
if j>=m then exit(true);
l:=i;
end;
end;
exit(false);
end;
begin
readln(n,m);
for i:= to n do readln(a[i]);
sort(,n);
l:=;r:=(a[n]-a[]) div (m-);
while l<r do
begin
k:=(l+r+) div ;
if check(k) then
l:=k
else
r:=k-;
end;
writeln(l);
readln;
end.
1734: [Usaco2005 feb]Aggressive cows 愤怒的牛的更多相关文章
- BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )
最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #inc ...
- bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛
1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Description Farmer John has built a new long barn, with N ...
- bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛【二分+贪心】
二分答案,贪心判定 #include<iostream> #include<cstdio> #include<algorithm> using namespace ...
- bzoj1734 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案
[Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 407 Solved: 325[S ...
- 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 ...
- [ACM] poj 2456 Aggressive cows (二分查找)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5436 Accepted: 2720 D ...
- BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛( floyd + 二分答案 + 最大流 )
一道水题WA了这么多次真是.... 统考终于完 ( 挂 ) 了...可以好好写题了... 先floyd跑出各个点的最短路 , 然后二分答案 m , 再建图. 每个 farm 拆成一个 cow 点和一个 ...
- 疯牛-- Aggressive cows (二分)
疯牛 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小 ...
随机推荐
- jQuery 对象与Dom 对象互转
jQuery 对象与Dom 对象互转: $obj --[i],get(i)-->obj --$(obj)-->$obj; obj--$($(obj))-->$obj,多包装了也是$o ...
- VoiceEngine获取硬件信息
#include "webrtc\voice_engine\include\voe_base.h" #include "webrtc\voice_engine\inclu ...
- doubango地址配置
转自:http://wiki.sip2sip.info/projects/sip2sip/wiki/SipDeviceConfiguration SIP Device Configuration Th ...
- 关于多字节字符入库失败处理(所谓的Emji),该处理是舍弃特殊字符
具体处理方法及样例如下: /** * 屏蔽超过三个字节以上的字符 * @param strByte * @return */ public static String filterUtf8(byte[ ...
- Kafka 0.10 SocketServer源代码分析
1概要设计 Kafka SocketServer是基于Java NIO来开发的,采用了Reactor的模式,其中包含了1个Acceptor负责接受客户端请求,N个Processor负责读写数据,M个H ...
- [CSS3] 学习笔记-CSS3选择器详解(一)
1.属性选择器 在CSS3中,追加了3个属性选择器,分别为:[att*=val].[att^=val]和[att$=val],使得属性选择器有了通配符的概念. <!doctype html> ...
- 游戏音频技术备忘 (四) Wwise Unreal Engine 集成代码浅析 (一)
在Engine\Plugins\Wwise\Source下为主要Wwise相关代码,AkAudio文件夹下为运行时相关代码,AudiokineticTools下为编辑器工具相关代码,Audiokine ...
- 从你的全世界切过(胡说八道支持向量机SVM小故事)
背景 据说很久很久以前, 澳门有一家"胡说八道大赌场", 专门提供各种奇奇怪怪的玩法. 其中有一个赌博叫"从你的全世界切过"(连名字也这么奇怪). 玩法是在一张 ...
- 使用python制作ArcGIS插件(4)界面交互
使用python制作ArcGIS插件(4)界面交互 by 李远祥 插件界面部分,除了一开始在设计器中设计的这些界面元素之外,还可以与操作系统进行一些输入输出的交互,这部分的实现全部在pythonadd ...
- 全球分布式数据库:Google Spanner(论文翻译)
本文由厦门大学计算机系教师林子雨翻译,翻译质量很高,本人只对极少数翻译得不太恰当的地方进行了修改. [摘要]:Spanner 是谷歌公司研发的.可扩展的.多版本.全球分布式.同步复制数据库.它是第一个 ...