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的更多相关文章

  1. UVa 12099 The Bookcase - 动态规划

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

  2. UVa 12099 The Bookcase (DP)

    题意:有 n 本书,每本书有一个高度和宽度,然后让你制作一个3层的书架,可以放下所有的书,并且要高*宽尽量小. 析:先把所有的书按高度进行排序,然后dp[i][j][k] 表示 前 i 本书,第二 层 ...

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

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

  4. [SinGuLaRiTy] 动态规划题目复习

    [SinGuLaRiTy-1026] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [UVA 1025] A Spy in the Metr ...

  5. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  6. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

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

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

  8. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  9. UVA计数方法练习[3]

    UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...

随机推荐

  1. linux部署html代码到linux服务器,并进行域名解析

    本博客主要是说一下,如何将本地写好的html代码部署到linux服务器,并进行解析.下一篇博客将写一下,如何将html代码部署到阿里云服务器,并进行域名解析,以及在部署过程中遇到的问题和解决方法. 1 ...

  2. 对JDBC的使用理解

    JDBC,即Java连接数据库,是java针对数据库操作的一套API,使用JDBC对数据库进行操作时分为以下几步: 1.加载数据库驱动类 Class.forName("com.mysql.j ...

  3. 实验吧CTF练习题---安全杂项---异性相吸解析

    ---恢复内容开始--- 实验吧安全杂项之异性相吸   地址:http://www.shiyanbar.com/ctf/1855 flag值:nctf{xor_xor_xor_biubiubiu}   ...

  4. 让Jenkins执行GitHub上的pipeline脚本

    本文是<Jenkins流水线(pipeline)实战>系列的第二篇,上一篇搭建好了Jenkins环境并执行了一个简单的pipeline任务,当时我们直接在Jenkins网页上编写pipel ...

  5. RatingBar星级拖动条

    RatingBar和SeekBar用法类似,他们都继承AbsSeekBar类; RatingBar的xml属性 android:numStars="5" 表示有5颗星  andro ...

  6. localStorage详细总结

    一.localStorage简介: 在HTML5中,新加入了一个localStorage特性,这个特性主要是用来作为本地存储来使用的,解决了cookie存储空间不足的问题(cookie中每条cooki ...

  7. Elastic Stack 笔记(六)Elasticsearch5.6 搜索详解

    博客地址:http://www.moonxy.com 一.前言 Elasticsearch 主要包含索引过程和搜索过程. 索引过程:一条文档被索引到 Elasticsearch 之后,默认情况下 ES ...

  8. 语音信号的梅尔频率倒谱系数(MFCC)的原理讲解及python实现

    梅尔倒谱系数(MFCC) 梅尔倒谱系数(Mel-scale FrequencyCepstral Coefficients,简称MFCC).依据人的听觉实验结果来分析语音的频谱, MFCC分析依据的听觉 ...

  9. Java程序连接数据库

    /** * 了解: 利用 Driver 接口的 connect 方法获取连接 */ // 第一种实现 /** * 了解: 利用 Driver 接口的 connect 方法获取连接 */ @Test p ...

  10. Mongodb关于查询返回指定字段的方法记录

    //通常指定字段由前端传入后台,例如params 前端以逗号分隔 //后端获取字段后操作如下: Query query = new Query(); if (params != null) { Str ...