POJ_2456

来源:互联网 发布:王菲歌词 知乎 编辑:程序博客网 时间:2024/05/24 05:42
Aggressive cows
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 11623 Accepted: 5693

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.


#include <stdio.h>#include <iostream>#include <algorithm>#include <math.h>using namespace std;const int  maxN = 100005;int N;int M;int cable[maxN];int sucess(int x){    int pre = 0;    for (int i = 1; i < M; i++)    {        int current = pre + 1;        while (current < N && ((cable[current] - cable[pre]) < x))        {            current++;        }        if (current == N)        {            return 0;        }        pre = current;    }    return 1;}void slove(){    int low = 0;    int high = cable[N-1]+1;    while (high - low > 1)    {        int mid = (low + high) / 2;        if (sucess(mid))        {            low = mid;        }        else        {            high = mid;        }    }    printf("%d\n", low);}int main(){    scanf("%d%d", &N, &M);    for (int i = 0; i < N; i++)    {        cin >> cable[i];    }    sort(cable, cable+N);    /*    for (int i = 0; i < N; i++)    {        cout << cable[i] << endl;    }    */    slove();    return 0;}


0 0
原创粉丝点击