码迷,mamicode.com
首页 > 其他好文 > 详细

Aggressive cows

时间:2020-03-15 15:04:13      阅读:48      评论:0      收藏:0      [点我收藏+]

标签:ati   res   out   arm   rate   event   long   oca   oid   

描述

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

标签:ati   res   out   arm   rate   event   long   oca   oid   

原文地址:https://www.cnblogs.com/orange-ga/p/12497437.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!