P1833 樱花

P1833 樱花

题意:
混合背包问题。
思路:
将时间点相减变成限制的一个时间段之后就是一个混合背包的模板。
https://www.cnblogs.com/zxr000/p/16999868.html
实现:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int f[N];
struct Good
{
    int wei, val;
} goods[105];

int main()
{
    int fir = 0, sec = 0;
    int h, m;
    scanf("%d:%d", &h, &m);
    fir = h * 60 + m;
    scanf("%d:%d", &h, &m);
    sec = h * 60 + m;
    m = sec - fir;
    int n;
    scanf("%d", &n);
    while (n--)
    {
        int w, v, s;
        scanf("%d%d%d", &w, &v, &s);
        if (s == 0)
        {
            for (int j = 0; j <= m; j++)
                if (j - w >= 0)
                    f[j] = max(f[j], f[j - w] + v);
        }
        else
        {
            int cnt = 1;
            for (int i = 1; i <= s; i *= 2)
            {
                goods[cnt].val = v * i;
                goods[cnt++].wei = w * i;
                s -= i;
            }
            if (s > 0)
            {
                goods[cnt].val = v * s;
                goods[cnt++].wei = w * s;
            }

            for (int j = 1; j < cnt; j++)
            {
                for (int h = m; h >= 0; h--)
                {
                    if (h - goods[j].wei >= 0)
                    {
                        f[h] = max(f[h], f[h - goods[j].wei] + goods[j].val);
                    }
                }
            }
        }
    }
    printf("%d\n", f[m]);
    return 0;
}
posted @ 2022-12-28 20:24  zxr000  阅读(30)  评论(0编辑  收藏  举报