Aggressive cows

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

#include
#include
#include
#include
#include
using namespace std;
int arr[100010],n,c;
bool check(int x)
{
    int curr=1,sum=1;
    for(int i=2;i<=n;i++)
    {
        if(arr[i]-arr[curr]>=x)
        {
            curr=i;
            sum++;
            if(sum>=c) return true;
        }

    }
    return false;

}
int main(){

    while(scanf("%d%d",&n,&c)!=EOF)
    {
       for(int i=1;i<=n;i++)
       {
           scanf("%d",arr+i);
       }
       sort(arr+1,arr+1+n);
        int lef=1,rig=1000000000,mid;
        while(rig-lef>1)
        {
            mid=(lef+rig)/2;
            if(check(mid))
            {
                lef=mid;
            }
            else
            {
                rig=mid;
            }
        }
        printf("%d\n",lef);
   }

}

你可能感兴趣的:(Aggressive cows)