GYM 101002C Greetings!(状压DP)

Your greeting card company makes unique greeting cards. The sizes of these greeting cards vary widely because of the whims of card designers. There are a lot of different types of cards, and each has a specific quantity that you need to manufacture.
Your job is to determine what envelopes to order for these greeting cards. You have a strict limit on the different number of different sizes of envelopes, which may be less than the number of distinct sizes of cards. You need to have envelopes so that every card fits in some envelope, possibly with room to spare, and the amount of waste paper is minimized. Measure the waste paper by the area of the envelope that is in excess of the area of the card, for each card. For example, a 10×410×4      10 \times 410×4 card in a 10×410×4      10 \times 410×4 envelope has no wasted paper, but a 10×410×4      10 \times 410×4 card in a 12×512×5      12 \times 512×5 envelope has waste of 20. You may not rotate the cards to fit them in the envelopes better.
Suppose that you have 5 types of cards: 10×1010×10      10 \times 1010×10 (5 of these), 9×89×8      9 \times 89×8 (10 of these), 4×124×12      4 \times 124×12 (20 of these), 12×412×4      12 \times 412×4 (8 of these), and 2×32×3      2 \times 32×3 (16 of these).
Now, suppose that you can only buy one type of envelope. Since all cards have to fit in that one envelope size, the smallest envelope size you can use is 12×1212×12      12 \times 1212×12, with an area of 144. The wastes by each type of card are 144−10⋅10=44144−10⋅10=44      144-10\cdot10 = 44144−10⋅10=44, 144−9⋅8=72144−9⋅8=72      144-9\cdot8 = 72144−9⋅8=72, 144−4⋅12=96144−4⋅12=96      144-4\cdot12 = 96144−4⋅12=96, 144−12⋅4=96144−12⋅4=96      144-12\cdot4 = 96144−12⋅4=96, and 144−2⋅3=138144−2⋅3=138      144-2\cdot3 = 138144−2⋅3=138, respectively. The total waste is 44⋅5+72⋅10+96⋅20+96⋅8+138⋅16=583644⋅5+72⋅10+96⋅20+96⋅8+138⋅16=5836      44\cdot5+72\cdot10+96\cdot20+96\cdot8+138\cdot16 = 583644⋅5+72⋅10+96⋅20+96⋅8+138⋅16=5836.
Suppose that you can buy 2 types of envelopes. The best you can do is to put the 10×1010×10      10 \times 1010×10, 9×89×8      9 \times 89×8 and 12×412×4      12 \times 412×4 cards in 12×1012×10      12 \times 1012×10 envelopes, and the 4×124×12      4 \times 124×12 and 2×32×3      2 \times 32×3 cards in 4×124×12      4 \times 124×12 envelopes. That adds up to waste of 1828.
If you can buy 5 types of envelopes, then you can match one envelope type to each card type, and there’s no waste!
Given a list of card types and the number of types of envelopes you can buy, what is the smallest amount of wasted paper you can achieve?
Input
Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. The first line of the input will consist of two space-separated integers n and k (1≤n,k≤15)(1≤n,k≤15)      (1 \le n, k \le 15)(1≤n,k≤15), where n is the number of different types of cards, and k is the maximum number of types of envelopes you can order. Each of the following n lines will consist of three integers, describing a type of card. The integers are w, h and q (1≤w,h,q≤10,000)(1≤w,h,q≤10,000)      (1 \le w, h, q \le 10,000)(1≤w,h,q≤10,000), where w is the width of the cards of this type, h is the height of the cards, and q is the quantity of cards of this type.
Output
Output a single integer, representing the smallest possible total amount of wasted paper.
样例输入1
5 1
10 10 5
9 8 10
4 12 20
12 4 8
2 3 16
样例输出1
5836
样例输入2
5 2
10 10 5
9 8 10
4 12 20
12 4 8
2 3 16
样例输出2
1828
样例输入3
5 5
10 10 5
9 8 10
4 12 20
12 4 8
2 3 16
样例输出3
0

 

题意:有n种尺寸的贺卡,分别给出它们的长宽和数量,可以定制k种尺寸的信封,每个信封装一个贺卡,问最少浪费多少面积。

思路

dp[i][j]表示用 i 种信封装 j 状态的贺卡的最小浪费

用二进制枚举所有状态,求出用一种信封装每种状态的贺卡浪费的面积,即dp[1][j];

然后由dp[i][j]=min(dp[i][j],dp[i-v][x]+dp[v][j^x])即可得出答案;

(其中x是j的子集,j^x得出的是x对j的补集)

#include <bits/stdc++.h>
#define int long long
#define inf 1e18

using namespace std;

int n,k;

int dp[20][1000005];
int x[20],y[20],num[20];

signed main()
{
    ios::sync_with_stdio(false);
    cin >> n >> k;
    for(int i=0; i<n; i++)
    {
        cin >> x[i] >> y[i] >> num[i];
    }
    for(int i=1; i<=k; i++)
    {
        for(int j=0; j<1<<n; j++)
        {
            dp[i][j]=inf;
        }
    }
    dp[0][0]=0;
    for(int i=0; i<(1<<n); i++)
    {
        int maxx=0,maxy=0,cnt=0,sum=0;
        for(int j=0; j<n; j++)
        {
            if((i>>j)&1)
            {
                maxx=max(maxx,x[j]);
                maxy=max(maxy,y[j]);
                sum+=x[j]*y[j]*num[j];
                cnt+=num[j];
            }
        }
        dp[1][i] = maxx*maxy*cnt-sum;
    }
    for(int i=2; i<=k; i++)
    {
        for(int j=0; j<(1<<n); j++)
        {
            for(int x=j; x; x =(x-1)&j)
            {
                for(int v=1; v<i; v++)
                {
                    dp[i][j]=min(dp[i][j],dp[i-v][x]+dp[v][j^x]);
                }
            }
        }
    }
    cout << dp[k][(1<<n)-1] << endl;
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Jupyter中安装gym库,您可以执行以下步骤: 1. 安装gym库: 在Jupyter中,您可以使用以下命令安装gym库: ``` !pip install -i https://pypi.tuna.tsinghua.edu.cn/simple gym ``` 这将从清华镜像源下载并安装gym库。 2. (可选) 安装其他依赖项: 如果您计划使用gym的atari模块,您可能还需要安装其他依赖项。在Jupyter中,您可以使用以下命令安装该模块: ``` !pip install -i https://pypi.tuna.tsinghua.edu.cn/simple gym[atari] ``` 这将从清华镜像源下载并安装gym的atari模块。 3. (可选) 安装其他相关库: 如果您计划在使用gym时使用特定版本或其他相关库,您可以使用以下命令在Jupyter中安装它们: ``` !pip install gym==0.22 !pip install matplotlib !pip install pygame !pip install imageio-ffmpeg ``` 这将分别安装gym的0.22版本、matplotlib、pygame和imageio-ffmpeg库。 请确保在执行这些命令之前,您已在Jupyter中正确设置了Python环境。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [jupyter notebook远程服务器使用gym](https://blog.csdn.net/irober/article/details/113390363)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [完美解决强化学习服务器Jupyter notebook gym 执行env.render报错问题](https://blog.csdn.net/qq_18256855/article/details/127137590)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值