1819: 【例】【基础】愤怒的奶牛

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:4 解决:3

题目描述

Farmer John 建造了一个有 lns="http://www.w3.org/1998/Math/MathML">N(lns="http://www.w3.org/1998/Math/MathML">2 lns="http://www.w3.org/1998/Math/MathML"> lns="http://www.w3.org/1998/Math/MathML">N lns="http://www.w3.org/1998/Math/MathML"> lns="http://www.w3.org/1998/Math/MathML">100000) 个隔间的牛棚,这些隔间分布在一条直线上,坐标是 lns="http://www.w3.org/1998/Math/MathML">x1 ,...,lns="http://www.w3.org/1998/Math/MathML">xN (0 lns="http://www.w3.org/1998/Math/MathML"> lns="http://www.w3.org/1998/Math/MathML">xi lns="http://www.w3.org/1998/Math/MathML"> lns="http://www.w3.org/1998/Math/MathML">1000000000)。

他的 lns="http://www.w3.org/1998/Math/MathML">C(lns="http://www.w3.org/1998/Math/MathML">2 lns="http://www.w3.org/1998/Math/MathML"> lns="http://www.w3.org/1998/Math/MathML">C lns="http://www.w3.org/1998/Math/MathML"> lns="http://www.w3.org/1998/Math/MathML">N) 头牛不满于隔间的位置分布,它们为牛棚里其他的牛的存在而愤怒。为了防止牛之间的互相打斗,Farmer John 想把这些牛安置在指定的隔间,所有牛中相邻两头的最近距离越大越好。那么,这个最大的最近距离是多少呢?



输入

第 lns="http://www.w3.org/1998/Math/MathML">1 行:两个用空格隔开的数字 lns="http://www.w3.org/1998/Math/MathML">N 和 lns="http://www.w3.org/1998/Math/MathML">C

第 lns="http://www.w3.org/1998/Math/MathML">2 ~ lns="http://www.w3.org/1998/Math/MathML">N+1 行:每行一个整数,表示每个隔间的坐标。

输出

输出只有一行,即相邻两头牛最大的最近距离。

样例输入 复制

5 3
1 
2 
8 
4 
9 

样例输出 复制

3

提示

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
int a[1000010];
int n, c, ans;
bool P(int d) {
    int k = 0, last = -INF;
    for (int i = 1; i <= n; i++)
        if (a[i] - last >= d)
            last = a[i], k++;
    return k >= c;
}
int main() {
    scanf("%d%d", & n, & c);
    for (int i = 1; i <= n; i++)
        scanf("%d", & a[i]);
    sort(a + 1, a + n + 1);
    int L = 1;
    int R = a[n] - a[1];
    while (L <= R) {
        int mid = L + R >> 1;
        if (P(mid)) ans = mid, L = mid + 1;
        else R = mid - 1;
    }
    printf("%d", ans);
    return 0;
}