题解:遗迹 (ruins) 正解详解
1. 核心思路:公式变形与枚举
题目要求最大化净收益。我们可以令背包的剩余空间为 (R = V - \sum s_i)。由于总占用空间不超过 (V),所以剩余空间 (R) 一定满足 (0 \le R \le V)。
将 (R) 代入原公式中,利用乘法分配律展开进行推导:
这个变形是解题的关键:当我们固定了一个剩余空间 (R) 时,每件宝物的实际价值就变成了 (h_i - R \cdot d_i)。
此时,问题就转化为了一个经典的 0-1 背包问题:在容量为 (W = V - R) 的背包中,选择物品使得总占用体积恰好为 (W),且总实际价值最大。因为 (V) 较小((V \le 500)),我们可以直接枚举 (R) 从 (0) 到 (V)。
2. 为什么朴素背包会 TLE?
如果对于每个 (R),我们都跑一遍 (O(nW)) 的普通 0-1 背包,总时间复杂度将达到 (O(nV^2))。在本题的数据范围((n \le 10^5, V \le 500))下,运算量高达 (2.5 \times 10^{10}),必定超时。
观察瓶颈所在,我们需要在固定 (R) 后,用更高效的方法求解这个背包问题。
3. 优化一:同体积物品只保留价值最大的若干件
对于背包容量 (W),如果有一大批体积均为 (s) 的物品,我们最多只能选 (\lfloor W/s \rfloor) 件。
假设我们选了同体积中价值较小的物品,却未选价值更大的物品,直接替换不会改变总体积,只会让收益更大。因此,对于每个体积 (s),我们只需保留实际价值最大的前 (K = \min(\text{该体积物品数}, \lfloor W/s \rfloor)) 件。
具体实现上,按体积分组后,对于每组物品,我们可以用 nth_element 在 (O(\text{组大小})) 的时间内找出前 (K) 大,再将这 (K) 件降序排序。
4. 优化二:决策单调性分治优化转移
将每组物品按体积 (s) 分类处理后,设排序后的价值降序为 (b_1 \ge b_2 \ge \dots \ge b_K),前缀和为 (P_t = \sum_{i=1}^t b_i)。
同组物品体积均相同,转移不改变容量除以 (s) 的余数,因此可以按余数分类处理。设加入这组之前的状态为 (F),之后为 (G),则转移方程为:
由于前缀和 (P_t - P_{t-1} = b_t) 单调不增,(P) 是离散凹函数。令 (A(k, j) = F_j + P_{k-j}),在四项均有时:
这满足四边形不等式。因此,最优转移的来源下标 (j) 会随着状态 (k) 的增加而单调不减。利用这一性质,我们可以使用分治法 (Divide and Conquer) 优化转移。在 calc(l, r, L, R) 中每次枚举中点 mid 的最优来源,然后利用这个来源缩小左右两半的决策范围。
每组物品、每个余数类的转移可在 (O(L \log L)) 时间内完成((L) 为该余数类的状态数)。固定 (R) 时,所有体积组的转移总耗时为 (O(W^2))。结合外层枚举 (R),总时间复杂度降至 (O(nV + V^3)),空间复杂度为 (O(n + V)),轻松通过。
5. 参考代码(完美继承你的码风)
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
const int inf = 1e18;
struct node {
int s, h, d;
int x; // 记录在当前 R 下的实际价值
};
// V <= 500,按体积分组存物品
vector<node> group_list[505];
int dp[505], ndp[505], pref[505];
int volume, residue, limit_k;
// 决策单调性分治优化
// 计算区间 [l, r] 内的新状态,其最优转移下标在 [L, R] 范围内
void calc(int l, int r, int L, int R) {
if (l > r) return;
int mid = (l + r) >> 1;
int from = max(L, mid - limit_k);
int to = min(mid, R);
int best = -inf;
int where = from;
for (int j = from; j <= to; ++j) {
if (dp[residue + j * volume] == -inf) continue;
int val = dp[residue + j * volume] + pref[mid - j];
if (val > best) {
best = val;
where = j;
}
}
// 更新状态,不可达状态仍归为 -inf
ndp[residue + mid * volume] = max(-inf, best);
calc(l, mid - 1, L, where);
calc(mid + 1, r, where, R);
}
signed main() {
// 优化输入输出
cin.tie(0) -> ios::sync_with_stdio(0);
int n, V;
cin >> n >> V;
for (int i = 1; i <= n; ++i) {
int s, h, d;
cin >> s >> h >> d;
if (s <= V) {
group_list[s].push_back({s, h, d, 0});
}
}
int ans = 0;
// 枚举剩余空间 R
for (int R = 0; R <= V; ++R) {
int W = V - R;
// 正确初始化:求“恰好装满”的问题
for (int i = 0; i <= W; ++i) dp[i] = -inf;
dp[0] = 0;
// 分组背包
for (int s = 1; s <= W; ++s) {
if (group_list[s].empty()) continue;
// 计算当前 R 下的实际价值
for (auto &it : group_list[s]) {
it.x = it.h - R * it.d;
}
// 最多只能选 K 个
int K = min((int)group_list[s].size(), W / s);
auto cmp = [](const node& a, const node& b) {
return a.x > b.x;
};
// 筛选前 K 大并排序
if (K < (int)group_list[s].size()) {
nth_element(group_list[s].begin(), group_list[s].begin() + K, group_list[s].end(), cmp);
}
sort(group_list[s].begin(), group_list[s].begin() + K, cmp);
// 计算前缀和
pref[0] = 0;
for (int i = 0; i < K; ++i) {
pref[i + 1] = pref[i] + group_list[s][i].x;
}
// 分组转移时,ndp 负责记录新状态,dp 保持不变供同组其他余数使用
for (int i = 0; i <= W; ++i) ndp[i] = dp[i];
volume = s;
limit_k = K;
// 枚举余数
for (residue = 0; residue < s; ++residue) {
int len = (W - residue) / s;
if (len >= 0) {
calc(0, len, 0, len);
}
}
// 转移完毕,将新状态拷贝回 dp
for (int i = 0; i <= W; ++i) dp[i] = ndp[i];
}
// 如果恰好装满 W 是合法的,更新全局答案
if (dp[W] > -inf / 2) {
ans = max(ans, dp[W]);
}
}
cout << ans << "\n";
return 0;
}