POJ2456【二分】

题意:
n个位置,m个帅气的窝的化身,然后窝要去这些位置,问一个最小距离的最大。
思路:
就是二分最小距离,然后判断一下该最小距离x 下,是不是存在>=m个窝的化身之间的距离>=x就好了;
二分模型是:11111111111000000000 满足条件的最右;

贴一发挫code…….

//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

const int N=1e5+10;

int a[N];
int n,c;

bool Judge(int s)
{
    int cnt=1;
    int cur=a[1];
    for(int i=2;i<=n;i++)
    {
        if(a[i]-cur>=s)
        {
            cnt++;
            cur=a[i];
            if(cnt>=c)
                return true;
        }
    }
    return false;
}


int main()
{
    scanf("%d%d",&n,&c);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    sort(a+1,a+n+1);

    int s=0;
    int t=a[n]-a[1];
    while(s<t)
    {
        int mid=s+(t-s+1)/2;
        if(Judge(mid))
            s=mid;
        else
            t=mid-1;
    }
    printf("%d\n",s);
    return 0;
}
posted @ 2016-08-14 23:28  see_you_later  阅读(147)  评论(0编辑  收藏  举报