洛谷P1833 樱花

01背包+完全背包+多重背包=?

大杂烩背包!

其实是叫混合背包

if else套对应的代码就行,多重背包转成01其实就是,但是我就不(滑稽)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;

int max(int a, int b){
    return a > b ? a : b;
}

int main()
{
    int a, b, c, d, n;
    scanf("%d:%d %d:%d %d", &a, &b, &c, &d, &n);
    int m = (60 * c + d) - (60 * a + b);
    int cost[10001] = { 0 };
    int value[10001] = { 0 };
    int x[10001] = { 0 };
    for (int i = 0; i < n; i++){
        scanf("%d%d%d", &cost[i], &value[i], &x[i]);
    }
    int pos[1001] = { 0 };
    for (int i = 0; i < n; i++){
        if (x[i] == 0){
            for (int j = cost[i]; j <= m; j++){
                pos[j] = max(pos[j], pos[j - cost[i]] + value[i]);
            }
        }
        else if (x[i] == 1){
            for (int j = m; j >= cost[i]; j--){
                pos[j] = max(pos[j], pos[j - cost[i]] + value[i]);
            }
        }
        else{
            int tcost[10001] = { 0 };
            int tvalue[10001] = { 0 };
            int ans = 0;
            int t = 1;
            while (x[i] > t){
                x[i] -= t;
                tcost[ans] = t*cost[i];
                tvalue[ans++] = t*value[i];
                t *= 2;
            }
            tcost[ans] = x[i]*cost[i];
            tvalue[ans++] = x[i]*value[i];
            for (int k = 0; k < ans; k++){
                for (int j = m; j >= tcost[k]; j--){
                    pos[j] = max(pos[j], pos[j - tcost[k]] + tvalue[k]);
                }
            }
        }
    }
    
    int max = 0;
    for (int i = 1; i <= m; i++){
        if (pos[i]>max){
            max = pos[i];
        }
    }
    cout << max << endl;



    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Vetsama/p/12288457.html
今日推荐