二分答案 + 贪心
先写个 check
函数,判断最短长度是否可行。
然后二分找最大的最短长度。
AC代码
#include <iostream>
#include <algorithm>
using namespace std;
using LL = long long;
LL D[50004];
LL sub[50004];
LL L, M, N;
bool check(LL l) {
LL m = 0;
for (LL i = 0; i <= N; ++i) {
if (sub[i] < l) {
LL s = sub[i];
while (s < l) {
s += sub[++i];
m++;
}
if (m > M) {
return false;
}
}
}
return true;
}
int main()
{
ios::sync_with_stdio(false);
cin >> L >> N >> M;
cin >> D[0];
sub[0] = D[0];
for (LL i = 1; i < N; ++i) {
cin >> D[i];
sub[i] = D[i] - D[i-1];
}
sub[N] = L - D[N-1];
LL l = 0;
LL r = L;
LL mid;
LL ans = -1;
while (l + 1 < r) {
mid = l + (r - l) / 2;
if (check(mid)) {
l = mid;
ans = max(mid, ans);
} else {
r = mid-1;
}
}
for (LL i = l; i <= r; ++i) {
if (check(i)) {
ans = max(i, ans);
}
}
cout << ans << endl;
}