[题目链接]
[算法]
二分答案,时间复杂度O(NlogL)
[代码]
#include<bits/stdc++.h> using namespace std; #define MAXN 50010 typedef long long ll; ll i,L,N,M,l,r,mid,ans; ll dis[MAXN]; template <typename T> inline void read(T &x) { ll f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; } for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0'; x *= f; } inline bool check(ll d) { ll i,c = 0,cnt = 0; bool flag = false; for (i = 1; i <= N; i++) { if (dis[i] - dis[c] >= d) c = i; else cnt++; if (cnt > M) return false; } if (L - dis[c] < d) cnt++; if (cnt <= M) return true; return false; } int main() { read(L); read(N); read(M); dis[0] = 0; for (i = 1; i <= N; i++) read(dis[i]); l = 1; r = 1e15; while (l <= r) { mid = (l + r) >> 1; if (check(mid)) { l = mid + 1; ans = mid; } else r = mid - 1; } printf("%lld ",ans); return 0; }