BZOJ4525——[Usaco2016 Jan]Angry Cows

时间:2021-07-23 20:36:12

1、题意:给一堆可以的限制长度的区间。。。区间的长度是你控制的。。。但是只有一个长度。。。求最短长度覆盖所有的点

2、分析:发现可以二分。。。那二分吧。。。。。然后我们从头向后扫一遍直接判断能否直接覆盖。。。然后就可以AC了《大赛后一水系列》

#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
#define inf 2147483647
#define M 100010

inline int read(){
    char ch = getchar(); int x = 0, f = 1;
    while(ch < '0' || ch > '9'){
        if(ch == '-') f = -1;
        ch = getchar();
    }
    while('0' <= ch && ch <= '9'){
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
} 

int a[M];
int n, K;

inline bool check(int x){
    int l = 1, o = 0;
    for(int i = 1; i <= n; i ++){
        if(a[i] - a[l] > x * 2){
            o ++;
            l = i;
        }
    }
    o ++;
    return o <= K;
}

int main(){
    n = read(), K = read();
    for(int i = 1; i <= n; i ++) a[i] = read();
    sort(a + 1, a + n + 1);
    int l = 0, r = 1000000000, t = 1000000000;
    while(l <= r){
        int mid = (l + r) / 2;
        if(check(mid)) r = (t = mid) - 1;
        else l = mid + 1;
    }
    printf("%d\n", t);
    return 0;
}