简单题。
我们令 为从第 分钟开始到营业结束的最大兴奋值之和,显然有:
倒着做答案就是 。
接下来考虑优化,我们枚举一下时间 ,对于某个 ,显然在第 分钟包含 ,在 时开始不包含 ,使用 multiset 或优先队列插入/删除一下就做完了。
时间复杂度 。
代码如下:
struct ds {
max_heap<int> pq, d;
void insert(int x) {
pq.emplace(x);
}
void erase(int x) {
d.emplace(x);
while (!pq.empty() && !d.empty() && pq.top() == d.top())
pq.pop(), d.pop();
}
int get() {
return pq.empty() ? 0 : pq.top();
}
};
int32_t main() {
#if MULTI_TEST == 1
#else
int n, m, k; std::cin >> n >> m >> k;
vi l(n), r(n), w(n);
vvi a(m + 2), b(m + 2); // 加入,删除
auto work = [&](int l, int r, int w) -> void {
if (l > r) return;
a[l].emplace_back(w);
b[r + 1].emplace_back(w);
};
For(i, 0, n - 1) {
std::cin >> l[i] >> r[i] >> w[i];
work(l[i], r[i] - k + 1, w[i]);
}
ds DS; vi mx(m + 1, 0);
For(i, 1, m) {
for (auto& w : a[i]) DS.insert(w);
for (auto& w : b[i]) DS.erase(w);
mx[i] = DS.get();
}
vi dp(m + k + 2, 0);
Rof(i, m, 1)
chkmax(dp[i], dp[i + 1]),
chkmax(dp[i], dp[i + k] + mx[i]);
std::cout << dp[1] << "\n";
#endif
}