例题 9-24 书架(Bookcase, ACM/ICPC NWERC 2006, UVa12099)

原题链接:https://vjudge.net/problem/UVA-12099
分类:动态规划
备注:状态优化,类似0-1背包

#include <bits/stdc++.h>
using namespace std;

const int N = 70 + 5;
const int INF = 0x3f3f3f3f;

int t, n, d[N * 30][N * 30], sum[N];
struct Book {
	int h, t;
	bool operator < (const Book& rhs) const {
		return h > rhs.h;
	}
}b[N];

int f(int a, int b) {
	return a == 0 ? b : 0;
}

int main(void) {
//	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) {
			scanf("%d%d", &b[i].h, &b[i].t);
			sum[i] =  sum[i - 1] + b[i].t;
		}
		int m = sum[n];
		sort(b + 1, b + 1 + n);
		int ans = INF;
		for (int j = 0; j <= m; j++)
			for (int k = 0; k + j <= m; k++)
				d[j][k] = INF;
		d[0][0] = 0;
		for (int i = 2; i <= n; i++) {
			for (int j = min(sum[i - 1] + 30, m); j >= 0; j--) {
				for (int k = min(m - j, j + 30); k >= 0; k--) {
					if ((j > b[i].t || (j == b[i].t && k == 0)) && d[j - b[i].t][k] != INF)
						d[j][k] = min(d[j][k], d[j - b[i].t][k] + f(j - b[i].t, b[i].h));
					if (j > 0 && k >= b[i].t && d[j][k - b[i].t] != INF)
						d[j][k] = min(d[j][k], d[j][k - b[i].t] + f(k - b[i].t, b[i].h));
				}
			}
		}
		for (int j = 1; j <= m; j++)
			for (int k = 1; k + j <= m; k++)
				if (d[j][k] != INF)
					ans = min(ans, max(m - j - k, max(j, k)) * (b[1].h + d[j][k]));
		printf("%d\n", ans);
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage=""%> <%@ page import="com.dao.BookDAO"%> <%@ page import="com.dao.BookTypeDAO"%> <%@ page import="com.actionForm.BookForm"%> <%@ page import="com.actionForm.BookTypeForm"%> <%@ page import="com.dao.BookCaseDAO"%> <%@ page import="com.actionForm.BookCaseForm"%> <%@ page import="com.dao.PublishingDAO"%> <%@ page import="com.actionForm.PublishingForm"%> <%@ page import="java.util.*"%> <html> <% String str = null; BookTypeDAO bookTypeDAO = new BookTypeDAO(); Collection coll_type = (Collection) bookTypeDAO.query(str); if (coll_type == null || coll_type.isEmpty()) { out.println("<script>alert('请先录入图书类型信息!');history.back(-1);</script>"); } else { Iterator it_type = coll_type.iterator(); int typeID = 0; String typename = ""; BookCaseDAO bookcaseDAO = new BookCaseDAO(); String str1 = null; Collection coll_bookcase = (Collection) bookcaseDAO.query(str1); if (coll_bookcase == null || coll_bookcase.isEmpty()) { out.println("<script>alert('请先录入书架信息!');history.back(-1);</script>"); } else { Iterator it_bookcase = coll_bookcase.iterator(); int bookcaseID = 0; String bookcasename = ""; PublishingDAO pubDAO = new PublishingDAO(); String str2 = null; Collection coll_pub = (Collection) pubDAO.query(str2); if (coll_pub == null || coll_pub.isEmpty()) { out.println("<script>alert('请先录入出版社信息!');history.back(-1);</script>"); } else { Iterator it_pub = coll_pub.iterator(); String isbn = ""; String pubname = ""; %> <script language="jscript"> function check(form){ if(form.barcode.value==""){ alert("请输入条形码!");form.barcode.focus();return false; } if(form.bookName.value==""){ alert("请输入图书名称!");form.bookName.focus();return false; } if(form.price.value==""){ alert("请输入图书定价!");form.price.focus();return false; } } </script>
最新发布
05-19

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JILIN.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值