UVA - 12099 The Bookcase
No wonder the old bookcase caved under the massive piles of books Tom had stacked on it. He had better build a new one, this time large enough to hold all of his books. Tom finds it practical to have the books close at hand when he works at his desk. Therefore, he is imagining a compact solution with the bookcase standing on the back of the desk. Obviously, this would put some restrictions on the size of the bookcase, it should preferably be as small as possible. In addition, Tom would like the bookcase to have exactly three shelves for aesthetical reasons. Wondering how small his bookcase could be, he models the problem as follows. He measures the height hi and thickness ti of each book i and he seeks a partition of the books in three non-empty sets S1, S2, S3 such that (∑3 j=1 maxi∈Sj hi) × (max3 j=1 ∑ i∈Sj ti) is minimized, i.e. the area of the bookcase as seen when standing in front of it (the depth needed is obviously the largest width of all his books, regardless of the partition). Note that this formula does not give the exact area of the bookcase, since the actual shelves cause a small additional height, and the sides cause a small additional width. For simplicity, we will ignore this small discrepancy. Thinking a moment on the problem, Tom realizes he will need a computer program to do the job
Input
The input begins with a positive number on a line of its own telling the number of test cases (at most 20). For each test case there is one line containing a single positive integer N, 3 ≤ N ≤ 70 giving the number of books. Then N lines follow each containing two positive integers hi , ti , satisfying 150 ≤ hi ≤ 300 and 5 ≤ ti ≤ 30, the height and thickness of book i respectively, in millimeters.
Output
For each test case, output one line containing the minimum area (height times width) of a three-shelf bookcase capable of holding all the books, expressed in square millimeters.
Sample Input
2
4
220 29
195 20
200 9
180 30
6
256 20
255 30
254 15
253 20
252 15
251 9
Sample Output
18000 29796
这个题目在状态的构造上,十分用心,如果我们考虑暴力DP那么显然要记下6个东西,就是每行的长和高,当然首先空间上过不去,其次,转移也十分复杂,那么考虑优化我们的状态。
首先只要记下两个宽和一个高就可以了,因为如果我们知道两个宽,就可以用总和减去两个宽得到下一个宽,然后有一个高是必定不用记下的,因为他一定是最高的那本书,然后第一维记处理到那本书,可以滚动,数组里存剩下的一个高,但即使优化到这个地步还是会暴空间!怎么办?
我们把那两个高之和的最小值存在数组了,这样就只有三维(相当于两维),即:设dp[i][j][k],表示dp到第i本书,第二列的宽度是j,第三列的宽度是k的二三列的最小高度和,这个为什么可以转移呢?我们考虑将每本书按照高度排序!那么第一本(最高的一本)我们强制放在第一列,而且只要第二列,第三列有书,因为我们是从高到低放的所以一定更新不了最大当列的最高值,进而更新不了他们的高度和!
转移就是枚举本书分放到第1,2,3,层进行转移。
代码:
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#define MAXN 2150
#define ll long long
using namespace std;
struct book{
int h,w;
}a[];
int f[MAXN][MAXN],dp[][MAXN][MAXN],sum[];
int n,inf; bool cmp(book x,book y){
return x.h>y.h;
} int main(){
int t;cin>>t;
while(t--){
memset(f,,sizeof(f));
memset(dp,,sizeof(dp));inf=dp[][][];
memset(a,,sizeof(a));
memset(sum,,sizeof(sum));
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d%d",&a[i].h,&a[i].w);
sort(a+,a+n+,cmp);
for(int i=;i<=n;i++) sum[i]=sum[i-]+a[i].w;
for(int i=;i<=n;i++){
f[][a[i].h]=a[i].h;
}
dp[][][]=;
int now=,last=;
for(int i=;i<n;i++){
now^=,last^=;
memset(dp[now],,sizeof(dp[now]));
for(int j=;j<=sum[i];j++)
for(int k=;k<=sum[i];k++){
if(dp[last][j][k]==inf) continue;
if(j+k>sum[i]) break;
dp[now][j+a[i+].w][k]=min(dp[now][j+a[i+].w][k],dp[last][j][k]+f[j][a[i+].h]);
dp[now][j][k+a[i+].w]=min(dp[now][j][k+a[i+].w],dp[last][j][k]+f[k][a[i+].h]);
dp[now][j][k]=min(dp[now][j][k],dp[last][j][k]);
}
}
int ans=inf;
for(int i=;i<=sum[n];i++)
for(int j=;j<=sum[n];j++){
if(i+j>sum[n]) break;
if(dp[now][i][j]==inf) continue;
if(i == || j == ) continue;
ans = min(ans, (dp[now][i][j] + a[].h) * max(j, max(i, sum[n]-j-i)));
}
printf("%d\n",ans);
}
}
UVA - 12099 The Bookcase的更多相关文章
- UVa 12099 The Bookcase - 动态规划
题目大意 给定一些书,每个书有一个高度和宽度,然后将它们放到一个三层的书架里(要求每一层都不为空).定义书架的大小为每层最大的高度和 乘 每层宽度和的最大值.求最小的书架大小. 显然动态规划(直觉,没 ...
- UVa 12099 The Bookcase (DP)
题意:有 n 本书,每本书有一个高度和宽度,然后让你制作一个3层的书架,可以放下所有的书,并且要高*宽尽量小. 析:先把所有的书按高度进行排序,然后dp[i][j][k] 表示 前 i 本书,第二 层 ...
- 【暑假】[深入动态规划]UVa 10618 The Bookcase
UVa 12099 The Bookcase 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42067 思路: ...
- [SinGuLaRiTy] 动态规划题目复习
[SinGuLaRiTy-1026] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [UVA 1025] A Spy in the Metr ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- UVA&&POJ离散概率与数学期望入门练习[4]
POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...
- UVA计数方法练习[3]
UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...
随机推荐
- 一次使用scrapy的问题记录
前景描述: 需要获取某APP的全国订单量,及抢单量.由于没有全国的选项所以只能分别对每一个城市进行订单的遍历.爬虫每天运行一次,一次获取48小时内的订单,从数据库中取出昨天的数据进行对比,有订单被抢则 ...
- BigDecimal转String
代码: public static void main(String[] args) { // 浮点数的打印 System.out.println(new BigDecimal("10000 ...
- [淘宝客技术篇005]如何取站点id和推广位id
我们知道,生成一个用于推广的淘客链接,是需要指定对应的站点id和推广位id的,也就是siteid和adzoneid. 今天,火星来客跟大家分享两个不同的方法获取站点id和推广位id. 方法一:直接获取 ...
- 彻底解决android拍照后无法显示的问题
这是对上篇"android 图片拍照,相册选图,剪切并显示"的文章之后的 改进 上一篇文章虽然能解决图片的拍照剪切以及显示,但是发现他有一个缺点, 如果该程序单独运行,貌似没有任何 ...
- Linux常用命令 —— 进程类
service (CentOs6) 1.service 服务名 start -------------- 启动 2.service 服务名 stop ...
- airflow + CeleryExecutor 环境搭建
airflow整合环境搭建 1. 整体结构 mysql -> 后端数据库 redis -> 用于broker CeleryExecutor -> 执行器 2. 环境安装 2.1,安装 ...
- JavaEE就业学习路线(给初学者以及自学者一个学习方向)
大家按这个路线学完后基本可以找工作了 第一节java入门 1-Java 背景介绍 2-Java 入门程序的编写 3-环境配置 4-基本概念介绍 5-类型转换 6-开发工具使用 第二节java基础 1- ...
- HTML连载38-内边距属性、外边距属性
一.内边距属性 1.定义:边框和内容之间的距离就是内边距 2.分开写 padding-top:数字px: padding-left:数字px: padding-bottom:数字px: padding ...
- CSS3-边框 border
一.圆角效果 border-radius 使用方法: border-radius:10px; /* 所有角都使用半径为10px的圆角 */ border-radius: 5px 4px 3px 2px ...
- 脱离脚手架来配置、学习 webpack4.x (二)基础搭建loader 配置 css、scss
序 上一篇已经把基本架子搭起来了,现在来增加css.scss.自动生成html.css 提前等方面的打包.webpack 默认只能处理js模块,所以其他文件类型需要做下转换,而loader 恰恰是做这 ...