100722E The Bookcase
题目大意
给你一些书的高度和宽度,有一个一列三行书柜,要求放进去书后,三行书柜的高的和乘以书柜的宽度最小。问这个值最小是多少。
分析
我们可以先将所有书按照高度降序排好,这样对于每一层只要放过书高度边不会改变。我们设第一本书放在第一层,用dp[i][j][k]表示考虑到第i个,第二层的厚度为j,第三层的厚度为k的情况时书架的最小总高度是多少,如果j或k是0则代表这一层以前没放过书,然后根据这个转移就行了。由于空间不够我们使用滚动数组。注意由于至少有三本书,所以每一层至少会放一个(这样可以使答案更优)。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define gmin(x,y) x=min(x,y)
const int inf = 0x3f3f3f3f;
struct node {
int h,w;
};
node d[];
int dp[][][],maxw[];
inline bool cmp(const node x,const node y){return x.h>y.h;}
int main(){
int n,m,i,j,k,t;
scanf("%d",&t);
while(t--){
memset(maxw,,sizeof(maxw));
scanf("%d",&n);
for(i=;i<=n;i++){
scanf("%d%d",&d[i].h,&d[i].w);
}
sort(d+,d+n+,cmp);
maxw[]=d[].w;
for(i=;i<=n;i++)
maxw[i]=maxw[i-]+d[i].w;
int now=;
memset(dp[now],0x3f,sizeof(dp[now]));
dp[now][][]=d[].h;
for(i=;i<=n;i++){
now^=;
memset(dp[now],0x3f,sizeof(dp[now]));
for(j=;j<=maxw[i-];j++)
for(k=;k+j<=maxw[i-];k++)
if(dp[now^][j][k]<inf){
gmin(dp[now][j][k],dp[now^][j][k]);
if(!j)gmin(dp[now][j+d[i].w][k],dp[now^][j][k]+d[i].h);
else gmin(dp[now][j+d[i].w][k],dp[now^][j][k]);
if(!k)gmin(dp[now][j][k+d[i].w],dp[now^][j][k]+d[i].h);
else gmin(dp[now][j][k+d[i].w],dp[now^][j][k]);
}
}
int ans=inf;
for(i=;i<=maxw[n];i++)
for(j=;j+i<=maxw[n];j++){
k=maxw[n]-i-j+d[].w;
if(dp[now][i][j]<inf)
gmin(ans,(dp[now][i][j])*max(i,max(j,k)));
}
printf("%d\n",ans);
}
return ;
}
100722E The Bookcase的更多相关文章
- CodeForces #368 div2 D Persistent Bookcase DFS
题目链接:D Persistent Bookcase 题意:有一个n*m的书架,开始是空的,现在有k种操作: 1 x y 这个位置如果没书,放书. 2 x y 这个位置如果有书,拿走. 3 x 反转这 ...
- 【Codeforces-707D】Persistent Bookcase DFS + 线段树
D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...
- Codeforces Round #368 (Div. 2) D. Persistent Bookcase
Persistent Bookcase Problem Description: Recently in school Alina has learned what are the persisten ...
- 【暑假】[深入动态规划]UVa 10618 The Bookcase
UVa 12099 The Bookcase 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42067 思路: ...
- Persistent Bookcase
Persistent Bookcase time limit per test 2 seconds memory limit per test 512 megabytes input standard ...
- 新概念英语(1-37)Making a bookcase
What is Susan's favourite color ? A:You're working hard, Georage. What are you doing? B:I am making ...
- Codeforces Round #368 (Div. 2) D. Persistent Bookcase 离线 暴力
D. Persistent Bookcase 题目连接: http://www.codeforces.com/contest/707/problem/D Description Recently in ...
- codeforces 707D:Persistent Bookcase
Description Recently in school Alina has learned what are the persistent data structures: they are d ...
- codeforces 707D D. Persistent Bookcase(dfs)
题目链接: D. Persistent Bookcase time limit per test 2 seconds memory limit per test 512 megabytes input ...
随机推荐
- java学习笔记 --- IO(2)
IO流的分类: 流向: 输入流 读取数据 输出流 写出数据 数据类型: 字节流 字节输入流 读取数据 InputStream 字节输出流 写出数据 OutputStream 字符流 字符 ...
- Senior Manufacturing Technical Manager
Job Description As a Manufacturing Technical Manager, you will be responsible for bringing new produ ...
- uva12563 Jin Ge Jin Qu hao(01背包)
这是一道不错的题.首先通过分析,贪心法不可取,可以转化为01背包问题.但是这过程中还要注意,本题中的01背包问题要求背包必须装满!这就需要在普通的01背包问题上改动两处,一个是初始化的问题:把dp[0 ...
- 在VS2013上配置OpenCV1.0
OpenCV1.0版(下载)基本上已经是老掉牙了,但是不想装新版,只是因为任性.所以就有了这样一个新老搭配.装完回想起来还是挺简单的,但是还是费了我一晚上,所以有必要记录一下. 先在电 ...
- tensorflow中使用tf.ConfigProto()配置Session运行参数&&GPU设备指定
tf.ConfigProto()函数用在创建session的时候,用来对session进行参数配置: config = tf.ConfigProto(allow_soft_placement=True ...
- THUPC2018 城市地铁规划
$n$ 个点,你可以随意连成一棵树,一个点的贡献为 $F(度数) \space mod \space 59393$ ,$F$ 为给定多项式函数,不超过 $10$ 次 求这 $n$ 个点的最大贡献,和最 ...
- freemarker实现第一个HelloWorld
第一步:引入freemarker jar包 第二步:创建templates下的test01.ftl 第三步:在web.xml下 第四步:编写后台代码 package com.wisezone.test ...
- Redis底层探秘(二):链表和跳跃表
链表简介 链表提供了高效的节点重排能力,以及顺序性的节点访问方式,并且可以通过增删节点来灵活地跳转链表的长度. 作为一种常用数据结构,链表内置在很多高级的编程语言里面,因为Redis使用C语言并没有内 ...
- div 遮罩层 弹窗
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 寻找php.ini之旅
/usr/local/php-fpm/lib/php.ini 可以通过phpinfo()来查看 https://www.cnblogs.com/ChineseMoonGod/p/6474772.htm ...