POJ2456 Aggressive cows (二分)

题目链接:

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


题意:

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


分析:

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


代码如下:

#include 
#include 
#include 
#include 
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=dis){
            cnt++;
            pos=x[i];
        }
        if(cnt==c) return true;
    }
    return false;
}
int main()
{
    while(~scanf("%d%d",&n,&c)){
        for(int i=0;i1){
            int mid=(l+r)/2;
            if(check(mid)) l=mid;
            else r=mid;
        }
        printf("%d\n",l);
    }
    return 0;
}



你可能感兴趣的:(二分&&三分)