uva12099 Bookcase ACM NWERC

书上,作者分析很详细 

#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<sstream>
#include<cassert>
using namespace std;
const int maxn = 70 + 5;
const int maxw = 30;
const int INF = 1000000000;
struct Book {
	int h, w;
	bool operator<(const Book& rhs) {
		return h > rhs.h || (h == rhs.h&&w > rhs.w);
	}
}book[maxn];

int d[2][maxn*maxw][maxn*maxw];
int sumw[maxn];
inline int f(int w, int h) {
	return w == 0 ? h : 0;
}
inline void update(int &newd, int d) {
	if (newd < 0 || d < newd)newd = d;
}
int main() {
	int T;
	cin >> T;
	while (T--) {
		int n;
		cin >> n;
		for (int i = 0; i < n; i++) {
			cin >> book[i].h >> book[i].w;
		}
		sort(book, book + n);
		sumw[0] = 0;
		for (int i = 1; i <= n; i++) {
			sumw[i] = sumw[i - 1] + book[i - 1].w;
		}
		d[0][0][0] = 0;
		int t = 0;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j <= sumw[i + 1]; j++) {
				for (int k = 0; k < sumw[i + 1] - j; k++)
					d[t ^ 1][j][k] = -1;
			}

			for (int j = 0; j <=sumw[i]; j++) {
				for (int k = 0; k <= sumw[i] - j; k++)if (d[t][j][k] >= 0) {
					update(d[t ^ 1][j][k], d[t][j][k]);
					update(d[t ^ 1][j + book[i].w][k], d[t][j][k] + f(j, book[i].h));
					update(d[t ^ 1][j][book[i].w], d[t][j][k] + f(k, book[i].h));
				}
			}
			t ^= 1;
		}
		int ans = INF;
		for (int j = 1; j <= sumw[n]; j++) {
			for (int k = 1; k <= sumw[n] - j; k++)if (d[t][j][k] >= 0) {
				int w = max(max(j, k), sumw[n] - j - k);
				int h = book[0].h + d[t][j][k];
				ans = min(ans, w*h);
			}
		}
		cout << ans << endl;
	}
	
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值