You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 50010;
const int INF = 0x3f3f3f3f;
int n, m;
int w[N];
int f[N]; // 已经完成前i个并且第i个被选中 情况下抄袭时间最小值
// 暴力+DP 通过了 8/12个数据
bool check(int limit) {
// 注意这个初始化,我吃了这个亏~
memset(f, 0x3f, sizeof f);
f[0] = 0;
for (int i = 1; i <= n; i++) // 计算前i道题填表填数据
for (int j = max(0, i - limit - 1); j <= i - 1; j++) // 第i题肯定是选中的那么它前面的是哪个位置选中呢
f[i] = min(f[i], f[j] + w[i]);
// 最后[n-limit,n]之间必然得有一题答了吧,这些都是合法解
int res = INF;
for (int i = n - limit; i <= n; i++) res = min(res, f[i]);
// 最小值比m小的话那么就是有可行解
return res <= m;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> w[i];
// 最长的空题段∈[1,n]
// 二分(求最长的最小)
int l = 0, r = n; // 空段区间长度0~n都有可能,注意此处是带0的因为可能老师要求每题必答对吧你以为隔一题空一题老师能忍其实人家忍不了
// 事实也证明了这一点,我尝试修改为 int l=1,r=n;会挂掉一个测试点! 
/*
空白时段的长度越长,花费的时间越少
空白时段的长度越短,花费的时间越长
*/
while (l < r) {
int mid = (l + r) >> 1; // 一定要搞清楚mid的含义:模拟的最长空题段长度
if (check(mid)) // 如果这个空白长度可以使得整体时间<=m,那么空白长度可以再小一点,这是个弯,好好想想
r = mid;
else
l = mid + 1; // 如果现在的长度不能使得时间够用,只能是长度再长一点
}
printf("%d\n", l);
return 0;
}