AcWing 487. 金明的预算方案

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef pair<int, int> PII;

#define x first
#define y second

const int M = 32010;
const int N = 70;

int f[M];
PII master[N];
vector<PII> servant[N];

int main() {
    int n, m;
    cin >> m >> n;
    for(int i = 1; i <= n; ++ i) {
        int v, p, q;
        cin >> v >> p >> q;
        if(q == 0) {
            master[i].x = v;
            master[i].y = v * p;
        } else {
            servant[q].push_back({v, v * p});
        }
    }
    for(int i = 1; i <= n; ++ i) {
        for(int j = m; j >= 0; -- j) {
            //二进制拆分,遍历每一种状态
            for(int k = 0; k < 1 << servant[i].size(); ++ k) {
                int v = master[i].x;
                int w = master[i].y;
                for(int c = 0; c < servant[i].size(); ++ c) {
                    //根据右移操作判断状态,决定当前是加还是不加
                    if(k >> c & 1) {
                        v += servant[i][c].x;
                        w += servant[i][c].y;
                    }
                }
                if(j >= v) f[j]=max(f[j],f[j - v] + w);
            }
        }
    }
    cout << f[m] << endl;
    return 0;
}
posted @ 2022-02-28 17:19  lemonsbiscuit  阅读(36)  评论(0编辑  收藏  举报