传送门

题目大意

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

分析

我们可以先将所有书按照高度降序排好,这样对于每一层只要放过书高度边不会改变。我们设第一本书放在第一层,用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. 马拉车——模版+KMP——模版

    void Manacher(){ ;t[i];++i,len+=){ s[i<<]='#'; |]=t[i]-'A'+'a'; |]=t[i]; } s[len++]='#'; ,pos= ...

  2. /dev/root: No such file or directory

    /*************************************************************************** * /dev/root: No such fi ...

  3. 元素为指针的vector的使用说明

    该程序演示了vector中的元素为指针的时候的对对象的操作. /* 功能说明: 元素为指针的vector的使用说明 实现方式: 使用this成员来显示各个对象的地址. 限制条件或者存在的问题: 无 * ...

  4. poj2010

    大学招n(n为奇数)个牛 招第i个牛需要ai块钱 第i个牛高考si分 输入招的牛数n 总的牛数c 总的钱数f 以及ai si 问用这些钱招的n个牛高考分数的中位数最大是多少 如果钱不够输出-1 这题结 ...

  5. numpy、scipy、pandas

    以下分别是numpy.Scipy.pandas的简介.虽然这些包提供的一些结构比python自身的“更高级.更高效”,更高级是因为它们能完成更高级的任务,但是,学习的时候尽量不要和python割裂开认 ...

  6. BeetleX高性能通讯开源组件

    net core高性能通讯开源组件BeetleX https://www.cnblogs.com/smark/p/9617682.html BeetleX beetleX是基于dotnet core实 ...

  7. 「长乐集训 2017 Day1」区间 线段树

    题目 对于两个区间\((a,b),(c,d)\),若\(c < a < d\)或\(c < b < d\)则可以从\((a,b)\)走到\((c,d)\)去,现在有以下两种操作 ...

  8. 使用自定义端口连接SQL Server 2008的方法

    版权声明:本文为博主原创文章,未经博主允许不得转载. 使用过SQL Server的人大多都知道,SQL Server服务器默认监听的端口号是1433,但是我今天遇到的问题是我的机器上有三个数据库实例, ...

  9. angularJs 指令的封装

    首先 指令的应用场景: 1:使你的html更具语义化,不需要深入研究代码就可以知道页面的大概逻辑. 2:抽象出一个自定义组件,在其他的地方进行重用. 一:directive的定义及其使用方法: 下面是 ...

  10. emacs复制粘贴和查找撤销

    在emacs下复制粘贴是这样的.1.在任一行中按下ctrl+space键,最底行会显示 Mark set 表示已经开始标记了.2.移动鼠标或者选中文字然后,可以有两种方式:Alt+w (复制) 或者  ...