题意:有 n 本书,每本书有一个高度和宽度,然后让你制作一个3层的书架,可以放下所有的书,并且要高*宽尽量小。

析:先把所有的书按高度进行排序,然后dp[i][j][k] 表示 前 i 本书,第二 层的宽度是 j,第三层的宽度是 k,第二层和第三层的高度最小,首先我们可以先最高的那本书放到第一层,那么这一层的高度就确定了,同理,在放第二层和第三层的时候,如果是第一本书,那么这一层的高度就已经确定了,为什么不表示第一层的宽度呢,因为总宽度是确定的,如果第二层的宽度和第三层的宽度知道了,那么第一层也就确定了。有了这些就可以进行状态转移了。注意不要转移无用的状态以降低时间复杂度。

有三种决策,1 放在第一层,dp[i+1][j][k] = min{ dp[i][j][k] }

2.放在第二层,那么又有两种情况,第一次放,还是不是第一次放

if(j == 0)  dp[i+1][j+w][k] = min{ dp[i][j][k] + h }

else  dp[i+1][j+w][k] = min{ dp[i][j][k] }

3.放在第三层,这一种和第二层一样。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 200000 + 10;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r > 0 && r <= n && c > 0 && c <= m;
} int dp[2][2105][2105];
struct Node{
int h, w;
bool operator < (const Node &p) const {
return h > p.h;
}
};
Node a[80];
int sum[maxn]; int main(){
int T; cin >> T;
while(T--){
scanf("%d", &n);
for(int i = 1; i <= n; ++i) scanf("%d %d", &a[i].h, &a[i].w);
sort(a + 1, a + n + 1);
for(int i = 2; i <= n; ++i) sum[i] = sum[i-1] + a[i].w;
int cnt = 0;
ms(dp[0], INF);
dp[0][0][0] = 0;
for(int i = 1; i < n; ++i, cnt ^= 1){
ms(dp[cnt^1], INF);
for(int j = 0; j <= sum[i]; ++j){
for(int k = 0; k <= sum[i]; ++k){
if(j + k > sum[i]) break;
if(dp[cnt][j][k] == INF) continue;
dp[cnt^1][j][k] = min(dp[cnt^1][j][k], dp[cnt][j][k]);
if(j == 0) dp[cnt^1][a[i+1].w][k] = min(dp[cnt^1][a[i+1].w][k], dp[cnt][j][k] + a[i+1].h);
else dp[cnt^1][j+a[i+1].w][k] = min(dp[cnt^1][j+a[i+1].w][k], dp[cnt][j][k]);
if(k == 0) dp[cnt^1][j][a[i+1].w] = min(dp[cnt^1][j][a[i+1].w], dp[cnt][j][k] + a[i+1].h);
else dp[cnt^1][j][k+a[i+1].w] = min(dp[cnt^1][j][k+a[i+1].w], dp[cnt][j][k]);
}
}
} int ans = INF;
for(int i = 0; i <= sum[n]; ++i)
for(int j = 0; j <= sum[n]; ++j){
if(i + j > sum[n]) break;
if(dp[cnt][i][j] == INF) continue;
if(i == 0 || j == 0) continue;
ans = min(ans, (dp[cnt][i][j] + a[1].h) * max(j, max(i, sum[n]-j-i+a[1].w)));
}
printf("%d\n", ans);
}
return 0;
}

  

UVa 12099 The Bookcase (DP)的更多相关文章

  1. UVa 12099 The Bookcase - 动态规划

    题目大意 给定一些书,每个书有一个高度和宽度,然后将它们放到一个三层的书架里(要求每一层都不为空).定义书架的大小为每层最大的高度和 乘 每层宽度和的最大值.求最小的书架大小. 显然动态规划(直觉,没 ...

  2. UVA - 12099 The Bookcase

    No wonder the old bookcase caved under the massive piles of books Tom had stacked on it. He had bett ...

  3. 【暑假】[深入动态规划]UVa 10618 The Bookcase

    UVa 12099  The Bookcase 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42067 思路:    ...

  4. UVA.674 Coin Change (DP 完全背包)

    UVA.674 Coin Change (DP) 题意分析 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 每种硬币的数量是无限的.典型完全背包. 状态 ...

  5. uva 10817(数位dp)

    uva 10817(数位dp) 某校有m个教师和n个求职者,需讲授s个课程(1<=s<=8, 1<=m<=20, 1<=n<=100).已知每人的工资c(10000 ...

  6. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  7. uva 10453 - Make Palindrome(dp)

    题目链接:10453 - Make Palindrome 题目大意:给出一个字符串,通过插入字符使得原字符串变成一个回文串,要求插入的字符个数最小,并且输出最后生成的回文串. 解题思路:和uva 10 ...

  8. uva 10671 - Grid Speed(dp)

    题目链接:uva 10671 - Grid Speed 题目大意:给出N,表示在一个N*N的网格中,每段路长L,如今给出h,v的限制速度,以及起始位置sx,sy,终止位置ex,ey,时间范围st,et ...

  9. uva 1331 - Minimax Triangulation(dp)

    option=com_onlinejudge&Itemid=8&page=show_problem&category=514&problem=4077&mosm ...

随机推荐

  1. ORA-01146: cannot start online backup - file 1 is already in backup ORA-01110: data file 1: 'C:\ORACLE\ORADATA\ORCL8\SYSTEM01.DBF'

    问题: Error: [1146] ORA-01146: cannot start online backup - file 1 is already in backup ORA-01110: dat ...

  2. Mac运维安装软件

    Maccrt软件 sudo spctl --master-disable 打开软件,复制到app,按照sn.txt输入即可 sudo spctl --master-enable crt快捷键crtl ...

  3. Android:不同drawable文件夹的区别

    4.0后,新建android工程,会自动生成drawable,drawalbe-ldpi,drawable-mdpi,drawable-hdpi,drawable-xhdpi,drawable-xxh ...

  4. canvas绘制路径

    canvas绘制路径 方法 beginPath() 创建一个新的路径 lineTo() 描绘路径 closePath() 沿着路径画直线,并且画点移动到路径开头 stroke() 绘制形状 fill( ...

  5. AJAX跨域调用ASP.NET MVC的问题及解决方案

    AJAX跨域调用ASP.NET MVC的问题及解决方案 问题描述: 解决方法: 只需要在web.config中添加如下标为红色的内容即可: <system.webServer> <h ...

  6. AO Identify地图交互

    转自supernever文章 Identify 1.框选要素高亮显示 private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS ...

  7. linux系统构架 - LB集群之LVS的NAT

    1.环境说明 三台服务器,一台叫dir,两台叫rs1和rs2 (director 和 real server) dir外网ip:192.168.192.129  内网ip:192.168.1.114 ...

  8. Python基础补充(二) 多核CPU上python多线程并行的一个假象【转】

    在python上开启多个线程,由于GIL的存在,每个单独线程都会在竞争到GIL后才运行,这样就干预OS内部的进程(线程)调度,结果在多核CPU上: python的多线程实际是串行执行的,并不会同一时间 ...

  9. js判断是否ie浏览器或者浏览器版本

    var Sys = {}; var ua = navigator.userAgent.toLowerCase(); var s; (s = ua.match(/rv:([\d.]+)\) like g ...

  10. pid文件的作用

    pid文件的作用 一.pid文件的作用 1.pid文件的内容用cat命令查看,可以看到内容只有一行,记录了该进程的ID 2.pid文件的作用防止启动多个进程副本 3.pid文件的原理进程运行后会给.p ...