本文已参与「新人创作礼」活动,一起开启掘金创作之路。
原题
There are many pretty girls in Wuhan University, and as we know, every girl loves pretty clothes, so do they. One day some of them got a huge rectangular cloth and they want to cut it into small rectangular pieces to make scarves. But different girls like different style, and they voted each style a price wrote down on a list. They have a machine which can cut one cloth into exactly two smaller rectangular pieces horizontally or vertically, and ask you to use this machine to cut the original huge cloth into pieces appeared in the list. Girls wish to get the highest profit from the small pieces after cutting, so you need to find out a best cutting strategy. You are free to make as many scarves of a given style as you wish, or none if desired. Of course, the girls do not require you to use all the cloth.
大致题意
武汉大学有很多漂亮的女孩,正如我们所知,每个女孩都喜欢漂亮的衣服,她们也是。有一天,他们中的一些人得到了一块巨大的长方形布料,他们想把它剪成长方形的小块来做围巾。但是不同的女孩喜欢不同的款式,她们投票决定每种款式的价格。他们有一台机器,可以把一块布横着或竖着切成两个更小的矩形块,并要求你用这台机器把原来巨大的布切成列表中出现的块。女孩们希望从小块中获得最高的利润
换句话说:
一个大矩形,要你分割成小矩形(只能水平和竖直切割),每个小矩形都有一个价值,问如何分割能得到最大总价值。
思路
不重不漏的划分这些子集 f[i,j]表示的是当前长宽为i和j的矩形 对应的切割来说,x, y可以横着放,可以竖着放那么他们对应的方案应该相应为:
代码
#include "bits/stdc++.h"
const int N = 1111;
using namespace std;
int f[N][N];
int main() {
int t;
cin >> t;
while (t--) {
int n, X, Y;
cin >> n >> X >> Y;
memset(f, 0, sizeof f);
vector<int> dx(n + 1), dy(n + 1), val(n + 1);
for (int i = 1; i <= n; ++i)
cin >> dx[i] >> dy[i] >> val[i];
for (int i = 0; i <= X; ++i)
for (int j = 0; j <= Y; ++j)
for (int k = 1; k <= n; ++k) {
int x = dx[k], y = dy[k], v = val[k];
if (i >= x && j >= y) {
f[i][j] = max (f[i][j], f[i - x][j] + f[x][j - y] + v);
f[i][j] = max (f[i][j], f[i - x][y] + f[i][j - y] + v);
}
if (i >= y && j >= x) {
f[i][j] = max (f[i][j], f[i - y][j] + f[y][j - x] + v);
f[i][j] = max (f[i][j], f[i - y][x] + f[i][j - x] + v);
}
}
cout << f[X][Y] << endl;
}
return 0;
}