1. 程式人生 > >Aggressive cows POJ

Aggressive cows POJ

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

const int maxn=111111;

int a[maxn];
int n,c;
bool judge(double mid)
{
    int cont=0;
    int st=a[0];
    for(int i=1;i<n;i++)
    {
        if(a[i]-st<mid)
        {
            cont++;
        }
        else
            st=a[i];
    }
    if(cont>(n-c))
        return false;
    else
        return true;
}
int main()
{
    cin>>n>>c;
    int max1=-1;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        max1=max(max1,a[i]);
    }
    sort(a,a+n);
    int l=0;
    int r=max1;
    int ans;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(judge(mid))
        {
            l=mid+1;
            ans=mid;
        }
        else
        {
            r=mid-1;
        }
    }
    printf("%d\n",ans);
    return 0;
}