您的位置:首页 > 其它

POJ 2456 Aggressive cows 二分

2014-02-22 00:00 267 查看
http://poj.org/problem?id=2456

题目大意:

给你n个牛舍,把c只牛放在牛舍里,要求最近的两头牛之间距离最大。

思路:

直接二分,枚举答案ans,如果满足,则搜索更大的。。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN= 100000+10;
int a[MAXN],n,c;
bool ok(int dis)
{
int last=0;
for(int i=1;i<c;i++)
{
int next=last+1;
while(next < n && a[next]-a[last] < dis) next++;
if(next>=n)
return false;
last=next;
}
return true;
}
int main()
{
while(~scanf("%d%d",&n,&c))
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]);

sort(a,a+n);
int L=0,R=a[n-1]+1;
while(L<R-1)
{
int mid=L+((R-L)>>1);
if(ok(mid))
L=mid;
else
R=mid;
}
printf("%d\n",L);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: