传送门

题目大意

给你一些书的高度和宽度,有一个一列三行书柜,要求放进去书后,三行书柜的高的和乘以书柜的宽度最小。问这个值最小是多少。

分析

我们可以先将所有书按照高度降序排好,这样对于每一层只要放过书高度边不会改变。我们设第一本书放在第一层,用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的更多相关文章

  1. CodeForces #368 div2 D Persistent Bookcase DFS

    题目链接:D Persistent Bookcase 题意:有一个n*m的书架,开始是空的,现在有k种操作: 1 x y 这个位置如果没书,放书. 2 x y 这个位置如果有书,拿走. 3 x 反转这 ...

  2. 【Codeforces-707D】Persistent Bookcase DFS + 线段树

    D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...

  3. Codeforces Round #368 (Div. 2) D. Persistent Bookcase

    Persistent Bookcase Problem Description: Recently in school Alina has learned what are the persisten ...

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

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

  5. Persistent Bookcase

    Persistent Bookcase time limit per test 2 seconds memory limit per test 512 megabytes input standard ...

  6. 新概念英语(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 ...

  7. Codeforces Round #368 (Div. 2) D. Persistent Bookcase 离线 暴力

    D. Persistent Bookcase 题目连接: http://www.codeforces.com/contest/707/problem/D Description Recently in ...

  8. codeforces 707D:Persistent Bookcase

    Description Recently in school Alina has learned what are the persistent data structures: they are d ...

  9. codeforces 707D D. Persistent Bookcase(dfs)

    题目链接: D. Persistent Bookcase time limit per test 2 seconds memory limit per test 512 megabytes input ...

随机推荐

  1. Spring mvc服务端消息推送(SSE技术)

    SSE技术是基于单工通信模式,只是单纯的客户端向服务端发送请求,服务端不会主动发送给客户端.服务端采取的策略是抓住这个请求不放,等数据更新的时候才返回给客户端,当客户端接收到消息后,再向服务端发送请求 ...

  2. hdu4451 Dressing(容斥原理)

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...

  3. tensorflow中使用tf.ConfigProto()配置Session运行参数&&GPU设备指定

    tf.ConfigProto()函数用在创建session的时候,用来对session进行参数配置: config = tf.ConfigProto(allow_soft_placement=True ...

  4. LeetCode Maximum Length of Pair Chain

    原题链接在这里:https://leetcode.com/problems/maximum-length-of-pair-chain/description/ 题目: You are given n  ...

  5. Mxgraph使用总结二

    1 新建画板,画板相关操作 var container = document.getElementById("main"); //设置背景样式 container.style.ba ...

  6. BZOJ2054:疯狂的馒头

    浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php? ...

  7. flask之基础概念

    [应用]一个 Flask 应用是一个 Flask 类的实例.可以在一个被称为应用工厂的函数内部创建 Flask实例.所有应用相关的配置.注册和其他设置都会在函数内部完成,然后返回这个应用.__init ...

  8. vim编辑 小笔记

    http://jingyan.baidu.com/article/495ba8410ff14d38b30ede01.html vim编辑器笔记 1.vi 文件名 打开文件 2.按 i 键 进入inse ...

  9. UVA548(二叉树遍历)

    You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...

  10. POJ3641(快速幂)

    Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8529   Accepted: 35 ...