qbxt上课讲5min就过了的题,$Herself32$做了15min+,说明Ta实在是菜死了。
题目描述
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?
输入输出格式
输入格式
Line 1: Two space-separated integers: N and C
Lines 2..N+1: Line i+1 contains an integer stall location, xi
输出格式
Line 1: One integer: the largest minimum distance
INPUT & OUTPUT’s examples
Input’s eg #1
1 | 5 3 |
Output’s eg #1
1 | 3 |
Hint
OUTPUT DETAILS:
FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3.
Huge input data,scanf is recommended.
分析
qbxt的ysq奆佬用了5min-就讲完了这题,还包括一个秀儿秀了半天。
。。。自闭了
比较裸的一个二分,但是我们要对于答案进行二分,也就是二分答案。
我们首先把第一头牛放进牛栏,并开一个front
变量表示当前被放入牛栏的牛的编号。
然后枚举剩下的牛栏,如果这些牛栏与front
之间的间距大于等于$k$,就是Judge
函数传进来的值。
当所有的牛都被放完后,返回true
。
然后我们对于答案进行二分,我们定义l
为最小距离,就是1,最大距离r
为牛栏编号最大的减去编号最小的.
然后进行二分,如果这些牛能够被照题意分进牛栏里,那么就把l=mid+1
,查找右区间。
如果不能,把r=mid
,查找左区间。
最后$r-1$即为答案。
代码
1 | /* Headers */ |