您的位置:首页 > 其它

nyoj586(poj2456) 疯牛

2017-03-29 21:48 141 查看
原题链接:nyoj586 疯牛

北大oj:Aggressive cows

//贪心+二分 难点:理解题意
#include <stdio.h>
#include <algorithm>
using namespace std;
const int N = 100005;
int a
, n, c;

bool judge(int x)
{
int cnt = 1, tmp = a[0];
for(int i = 1; i < n; i++)
{
if(a[i] - tmp >= x)
{
cnt++;
tmp = a[i];
if(cnt >= c) //可以放下C头牛
return true;
}
}
return false;
}

int get_ans() //二分搜索最小值
{
int l = 0, r = a[n-1] - a[0];
while(l <= r)
{
int mid = (l + r) / 2;
if(judge(mid))
l = mid + 1;
else
r = mid - 1;
}
return l - 1;  //或r
}

int main()
{
while(~scanf("%d%d",&n,&c))
{
for(int i = 0; i < n; i++)
scanf("%d",&a[i]);
sort(a, a+n);
printf("%d\n",get_ans());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: