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;
}