题解 CF106C【Buns】

· · 题解

显然(RXZ 老师上课讲了),做包子的顺序不会改变答案。

所以可以按顺序来,也就是先做没有馅的第 0 种包子,再做第 1 种,再做第 2 种,一直做到第 m 种。

显然第 i 种包子最多可以做 \lfloor\frac{a_i}{b_i}\rfloor 个,所以跑这么多遍 0-1 背包即可。

没有馅的第 0 种包子最多可以做 \lfloor\frac{n}{c_0}\rfloor 个,枚举上来即可。

还有我这里为了践行中央提出的“厉行节约,反对浪费”这一要求好写,dp_i 表示恰好使用 i 克面团最多赚的钱。

所以需要从头到尾再扫一遍。

时间复杂度 \Theta(n\Sigma_{i=1}^{m}\lfloor\frac{a_i}{b_i}\rfloor) 约为 \Theta(10^6) 即 \Theta(\texttt{能过})。

见短的不能再短的代码:

#include <bits/stdc++.h>
using namespace std;
int n, m, a, b, c, d, dp[1010], ans;
int main() {
    cin >> n >> m >> c >> d;
    for (int i = 1; i <= n/c; i++) dp[i*c] = i*d;
    while (m--) {
        cin >> a >> b >> c >> d;
        for (int t = 1; t <= a/b; t++) {
            for (int i = n; i >= c; i--) {
                dp[i] = max(dp[i], dp[i-c] + d);
            }
        }
    }
    for (int i = 1; i <= n; i++) ans = max(ans, dp[i]);
    cout << ans << endl;
    return 0;
}
\huge{\texttt{\color{white}{求过,求赞}}}