洛谷 P3853 [TJOI2007]路标设置

时间:2022-04-22 10:28:29

路标设置

二分枚举”空旷指数“, 做法与跳石头类似。

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
//Mystery_Sky
//
#define M 10000100
#define INF 0x7f7f7f7f
#define ll long long
ll l, r, mid;
int L, n, k;
int a[M];
inline bool check(ll ans)
{
    int last = 0, cnt = 0;
    for(int i = 2; i <= n; i++) {
        while (a[i] - last > ans) {
            cnt++;
            last += ans;
        }
        last = a[i];
    }
    return cnt <= k;
}

int main() {
    scanf("%d%d%d", &L, &n, &k);
    a[0] = 0;
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    a[++n] = L;
    sort(a, a+n+1);
    l = 0, r = INF;
    while(l < r) {
        mid = l + (r - l) /2;
        if(check(mid)) r = mid;
        else l = mid + 1;
    }
    printf("%lld\n", l);
    return 0;
}