标签:poj 2456 二分 c++ poj amp algorithm
// n点中选c点放下c头牛 是的n-1段距离中的最短距离最大 ,求这个最大的最短距离
//假设当前的最小值为x,如果判断出最小差值为x时可以放下C头牛,
//就先让x变大再判断;如果放不下,说明当前的x太大了,
//就先让x变小然后再进行判断。直到求出一个最大的x就是最终的答案。
# include <algorithm>
# include <string.h>
# include <stdio.h>
using namespace std;
int n,c,i,a[100010];
int judge(int x)
{
int cot=1;
int tmp=a[0];
for(i=1;i<n;i++)
{
if(a[i]-tmp>=x)//距离大于栏的间隙 可以放下
{
cot++;
tmp=a[i];
if(cot>=c)//可以排下c头牛
return true;
}
}
return false;
}
int slove()//二分查找
{
int l=0;//最小距离
int r=a[n-1]-a[0];//最大距离
while(l<=r)
{
int mid=(l+r)/2;
if(judge(mid))//可以排下c头牛
l=mid+1;
else
r=mid-1;
}
return l-1;
}
int main()
{
while(~scanf("%d%d",&n,&c))
{
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
printf("%d\n",slove());
}
return 0;
}poj 2456 Aggressive cows(二分),布布扣,bubuko.com
标签:poj 2456 二分 c++ poj amp algorithm
原文地址:http://blog.csdn.net/lp_opai/article/details/38235903