Aggressive cows(二分+贪心)

农夫John面临一个问题,他的C头牛不喜欢新长形谷仓的布局,为了避免它们互相攻击,他需要分配隔间以确保牛之间的最小距离最大化。给定N个隔间位置,问题在于找到能容纳所有牛的最大最小距离。输入包括N和C,以及每个隔间的位置。输出为最大最小距离。题目提供样例输入和输出,解决方案可通过二分查找和贪心策略实现。
摘要由CSDN通过智能技术生成

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?

Input

  • Line 1: Two space-separated integers: N and C
  • Lines 2…N+1: Line i+1 contains an integer stall location, xi

Output

  • Line 1: One integer: the largest minimum distance

Sample Input

5 3
1
2
8
4
9

Sample Output

3

题意:农夫john建立了许多隔间,并且知道各自的距离,同时,他还养了许多喜欢打架的牛,这些牛不能紧挨着,我们得把他们放到不同的隔间里,问牛牛们隔的最远的距离是多少。

思路:我们需要一个数,也就是牛牛们相隔的距离,并且这个数必须满足有隔间能放牛,我们可以从最小的隔间坐标到最大的隔间坐标形成的区间内去枚举试探,但是吧,数据量大的很,显然直接就超时,我们可以考虑用二分查找的方法去找这个最大的数,检测的时候我们可以看所有的隔间能不能放得下所有得牛。

代码

#include<iostrea
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值