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 ...
随机推荐
- Spring mvc服务端消息推送(SSE技术)
SSE技术是基于单工通信模式,只是单纯的客户端向服务端发送请求,服务端不会主动发送给客户端.服务端采取的策略是抓住这个请求不放,等数据更新的时候才返回给客户端,当客户端接收到消息后,再向服务端发送请求 ...
- hdu4451 Dressing(容斥原理)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...
- tensorflow中使用tf.ConfigProto()配置Session运行参数&&GPU设备指定
tf.ConfigProto()函数用在创建session的时候,用来对session进行参数配置: config = tf.ConfigProto(allow_soft_placement=True ...
- LeetCode Maximum Length of Pair Chain
原题链接在这里:https://leetcode.com/problems/maximum-length-of-pair-chain/description/ 题目: You are given n ...
- Mxgraph使用总结二
1 新建画板,画板相关操作 var container = document.getElementById("main"); //设置背景样式 container.style.ba ...
- BZOJ2054:疯狂的馒头
浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php? ...
- flask之基础概念
[应用]一个 Flask 应用是一个 Flask 类的实例.可以在一个被称为应用工厂的函数内部创建 Flask实例.所有应用相关的配置.注册和其他设置都会在函数内部完成,然后返回这个应用.__init ...
- vim编辑 小笔记
http://jingyan.baidu.com/article/495ba8410ff14d38b30ede01.html vim编辑器笔记 1.vi 文件名 打开文件 2.按 i 键 进入inse ...
- UVA548(二叉树遍历)
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- POJ3641(快速幂)
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8529 Accepted: 35 ...