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)个隔间,这些小 ...
随机推荐
- underscore 1.7.0 api
它是这个问题的答案:“如果我在一个空白的HTML页面前坐下, 并希望立即开始工作, 我需要什么?“ http://www.css88.com/doc/underscore/#
- Ubuntu14.04 安装Oracle JDK
在Ubuntu 14.04 中安装 Oracle JDK6,7 或 8. 准备:添加add-apt-repository工具 #apt-get install python-software-prop ...
- Redis系列二(yum切换为网易163)
这个可能和Redis没有直接的关系... 是我在yum install的时候发现centos的yum实在是太慢,上网查了下.网易163有个yum镜像,为了让CentOS6使用速度更快的YUM更新源,可 ...
- 开发团队在TFS中使用Git Repository (二)
使用Git作分支时,仅仅是对提交历史记录的一个引用,创建分支成本非常低,分支的切换快且简单.在分支管理方面,相对其他的版本管理工具,Git可谓是一骑绝尘. 开发过程中,我们可以针对任何的大小功能进行分 ...
- linux - tar命令简单使用
tar 新建一个tar文档 touch file1 touch file2 mkdir dir1 touch dir1/file3 # 普通tar文档 tar -cf tar-file.tar fil ...
- 使用VS Code从零开始开发调试.NET Core 1.1
使用VS Code 从零开始开发调试.NET Core 1.1.无需安装VS 2017 RC 即可开发.NET Core 1.1. .NET Core 1.1 发布也有一段时间了,最大的改动是从 pr ...
- CSS3知识点整理(一)----基本样式
(一) 在编写CSS3样式时,不同的浏览器可能需要不同的前缀.它表示该CSS属性或规则尚未成为W3C标准的一部分,是浏览器的私有属性,虽然目前较新版本的浏览器都是不需要前缀的,但为了更好的向前兼容前缀 ...
- Asp.net mvc 知多少(六)
本系列主要翻译自<ASP.NET MVC Interview Questions and Answers >- By Shailendra Chauhan,想看英文原版的可访问http:/ ...
- Android jni 编程1(对基本类型字符串的操作)
最近一直在学安卓的jni,主要参考的是黑马程序员的视频教程,讲的确实不错. 那就自己总结一下吧,算是对学习的复习. 这篇博客也主要参考了这位博主:http://www.cnblogs.com/acti ...
- 动态样式语言—LESS
博客原文地址:Claiyre的个人博客 https://claiyre.github.io/ 博客园地址:http://www.cnblogs.com/nuannuan7362/ 如需转载,请在文章开 ...