首页 > 其他 > 详细

洛谷P1776 宝物筛选

时间:2020-02-09 21:13:39      阅读:87      评论:0      收藏:0      [点我收藏+]

分组背包,如果分成01背包有可能会超限,使用二进制分组的方法

比如18=1+2+4+8+3

分解成二进制的大物品

#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 n, m;
    cin >> n >> m;
    int cost[100001] = { 0 };
    int value[100001] = { 0 };
    int ans = 0;
    for (int i = 0; i < n; i++){
        int a, b, c;
        cin >> b >> a >> c;
        int t = 1;
        while (c - t>0){
            c -= t;
            cost[ans] = t*a;
            value[ans++] = t*b;
            t *= 2;
        }
        cost[ans] = c*a;
        value[ans++] = c*b;
    }


    int pos[40001] = { 0 };
    for (int i = 0; i < ans; i++){
        for (int j = m; j >= cost[i]; j--){
            pos[j] = max(pos[j], pos[j - cost[i]] + value[i]);
        }
    }
    cout << pos[m] << endl;



    return 0;
}

 

洛谷P1776 宝物筛选

原文:https://www.cnblogs.com/Vetsama/p/12288484.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!