poj 2456 二分 Aggressive cows

来源:互联网 发布:java数组转json字符串 编辑:程序博客网 时间:2024/06/05 03:30

题目链接:http://poj.org/problem?id=2456


Aggressive cows
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8880 Accepted: 4408

Description

Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000). 

His C (2 <= C <= N) cows don't like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ want to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?

Input

* Line 1: Two space-separated integers: N and C 

* Lines 2..N+1: Line i+1 contains an integer stall location, xi

Output

* Line 1: One integer: the largest minimum distance

Sample Input

5 312849

Sample Output

3

Hint

OUTPUT DETAILS: 

FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3. 

Huge input data,scanf is recommended.

Source

USACO 2005 February Gold



代码:

#include <iostream>#include <stdio.h>#include <math.h>#include <stdlib.h>#include <string>#include <string.h>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <stack>#include <iomanip>using namespace std;typedef long long LL;const int INF=0x7fffffff;int N,C;int home[100009];bool c(int d){    int last=0;    int cur=last+1;    int ct=0;    for(int i=0;i<N;i++){        if(home[cur]-home[last]>=d){            last=cur;            cur=last+1;            ct++;            if(ct==C-1)return 1;        }        else{            cur++;        }    }    return 0;}int main(){    cin>>N>>C;    for(int i=0;i<N;i++){        scanf("%d",&home[i]);    }    sort(home,home+N);    int l=0,r=1000000001;    int mid;    while(r-l>1){        mid=(l+r)/2;        if(c(mid)){            l=mid;        }        else r=mid;    }    cout<<l<<endl;    return 0;}


0 0