https://www.luogu.com.cn/problem/P1824
题目描述
Farmer John建造了一个有N(2<=N<=100,000)个隔间的牛棚,这些隔间分布在一条直线上,坐标是x1,…,xN (0<=xi<=1,000,000,000)。
他的C(2<=C<=N)头牛不满于隔间的位置分布,它们为牛棚里其他的牛的存在而愤怒。为了防止牛之间的互相打斗,Farmer John想把这些牛安置在指定的隔间,所有牛中相邻两头的最近距离越大越好。那么,这个最大的最近距离是多少呢?
输入格式
第1行:两个用空格隔开的数字N和C。
第2~N+1行:每行一个整数,表示每个隔间的坐标。
输出格式
输出只有一行,即相邻两头牛最大的最近距离。
输入输出样例
输入 #1复制
5 3 1 2 8 4 9
输出 #1复制
3
最小值最大化,第二个板子
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
typedef long long LL;
LL n,c;
LL a[maxn];
LL check(LL mid)
{
LL sum=1;LL start=a[1];
for(LL i=2;i<=n;i++)
{
if(a[i]-start>=mid)
{
sum++;start=a[i];
}
}
if(sum>=c) return 1;
else return 0;
}
LL bsearch(LL l,LL r)
{
while(l<r)
{
LL mid=(l+r+1)>>1;
if(check(mid)) l=mid;
else r=mid-1;
}
return l;
}
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
cin>>n>>c;
for(LL i=1;i<=n;i++) cin>>a[i];
sort(a+1,a+1+n);
//枚举最近距离
LL t=bsearch(0,a[n]);
cout<<t<<endl;
return 0;
}