Aggressive cows

Posted orange-ga

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Aggressive cows相关的知识,希望对你有一定的参考价值。

描述

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?

输入

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

* Lines 2..N+1: Line i+1 contains an integer stall location, xi输出* Line 1: One integer: the largest minimum distance

样例输入

5 3

1

2

8

4

9

样例输出

3


 

#include<stdio.h>
#include<stdlib.h>
int N, C;
long long int loca[100000];

int compare(const void* a, const void* b) {
    return *(long long int*)a - *(long long int*)b;
}
int cows(long long p) {
    int cnt = 1;
    long long last = loca[0];
    for (int i = 1; i < N; ++i) {
        if (loca[i] - last >= p) {
            ++cnt;
            last = loca[i];
        }
    }
    return cnt;
}
int main()
{
    freopen("E:\IDMdowanload\in.txt", "r", stdin);
    long long int maxdist = 0;
    scanf("%d %d", &N, &C);
    for (int i = 0; i < N; ++i) {
        scanf("%lld", &loca[i]);
        if (loca[i] > maxdist)maxdist = loca[i];
    }
    qsort(loca, N, sizeof(loca[0]), compare);
    long long int l = 0, r = maxdist;
    while (l < r) {
        long long int mid = (l + r) / 2;
        int p = cows(mid);
        if (p < C)r = mid;
        else l = mid + 1;
    }
    
    printf("%lld", l - 1);

    return 0;
}

 

以上是关于Aggressive cows的主要内容,如果未能解决你的问题,请参考以下文章

Aggressive cows

Aggressive cows

007:Aggressive cows

Aggressive cows 愤怒的牛

POJ - 2456 :Aggressive cows

POJ - 2456 Aggressive cows