专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > 编程

POJ2456 Aggressive cows (2分)

发布时间:2011-06-30 11:30:45 文章来源:www.iduyao.cn 采编人员:星星草
POJ2456 Aggressive cows (二分)

题目链接:

http://poj.org/problem?id=2456


题意:

有n个牛舍,位置为xi,c头牛,把每头牛放在与其相邻牛的距离尽量远的牛舍,即最大化相邻两头牛之间的距离,求这个最大距离。


分析:

 二分答案,然后O(N)的复杂度判断符不符合。


代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn = 100010;

int n,c;
int x[maxn];

bool check(int dis)
{
    int pos=x[0],cnt=1;
    for(int i=1;i<n;i++){
        if(x[i]-pos>=dis){
            cnt++;
            pos=x[i];
        }
        if(cnt==c) return true;
    }
    return false;
}
int main()
{
    while(~scanf("%d%d",&n,&c)){
        for(int i=0;i<n;i++)
            scanf("%d",&x[i]);
        sort(x,x+n);
        int l=0,r=1000000000,mid;
        while(r-l>1){
            int mid=(l+r)/2;
            if(check(mid)) l=mid;
            else r=mid;
        }
        printf("%dn",l);
    }
    return 0;
}



友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: