The Bookcase
题意:
有n本宽w高h的书,向三层书架上放,每层不能为空,求占用的整体的最小面积(总高度*三层中最宽的)
分析:
不太好想,dp[i][j]表示第一层宽度为i第二层为j放的最小高度
dp[i][j]=min(dp[i-w[i]][j],dp[i][j-w[i]])放在第一、二层取最小,当i,j放的是第一本书的时候要加上相应的高度,先按高度升序排列保证正确性。
最后遍历求最小面积
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
struct book{
int h,w;
}b[];
int dp[][],sum,n;
bool cmp(book x,book y){
if(x.h==y.h)return x.w<y.w;
return x.h>y.h;
}
void solve(){
for(int i=;i<=sum;++i)
for(int j=;j<=sum;++j)
dp[i][j]=INF;
dp[][]=;
int tw=;
for(int i=;i<n;++i){
for(int j=tw;j>=;--j)
for(int k=tw;k>=;--k){
if(dp[j][k]==INF||j+k>tw)continue;
int tmp=;
if(j==)
tmp=b[i].h;
dp[j+b[i].w][k]=min(dp[j+b[i].w][k],dp[j][k]+tmp);
tmp=;
if(k==)
tmp=b[i].h;
dp[j][k+b[i].w]=min(dp[j][k+b[i].w],dp[j][k]+tmp);
}
tw+=b[i].w;
}
int mina=INF;
for(int i=;i<=sum;++i)
for(int j=;j<=sum;++j){
if(dp[i][j]==INF||i+j>=sum)continue;
int k=sum-i-j;
int mw=max(max(i,j),k);
mina=min(mina,(dp[i][j]+b[].h)*mw);
}
printf("%d\n",mina);
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
sum=;
for(int i=;i<n;++i){
scanf("%d%d",&b[i].h,&b[i].w);
sum+=b[i].w;
}
sort(b,b+n,cmp);
solve();
}
return ;
}
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 ...
- Codeforces-707D:Persistent Bookcase (离线处理特殊的可持久化问题&&Bitset)
Recently in school Alina has learned what are the persistent data structures: they are data structur ...
随机推荐
- mac 安装mysql 报错“ERROR 2002 (HY000): Can not connect to local MySQL server through socket '/tmp/mysql.sock' (2)” 解决办法
首先安装 homebrew 再 brew install mysql 之后连接 mysql 无论是登录还是修改初始密码都会报如下的错误 ERROR 2002 (HY000): Can not conn ...
- Project Euler 99:Largest exponential 最大的幂
Largest exponential Comparing two numbers written in index form like 211 and 37 is not difficult, as ...
- cv论文(SPARSE REPRESENTATION相关)
上个博文我讲了一些CNN相关的论文,比较浅显都是入门知识,这节课来总结一些稀疏表示方面的文章.至于上个博文说到的要讲的sparse coding的知识,我将会放在Deep Learning的专题里面讲 ...
- 网上图书商城项目学习笔记-011Book模块查询(分页)
一.流程分析 1.图书模块 2.分布分析 二.代码 1.view层 1)list.jsp <%@ page language="java" import="java ...
- Java基础:三步学会Java Socket编程
Java基础:三步学会Java Socket编程 http://tech.163.com 2006-04-10 09:17:18 来源: java-cn 网友评论11 条 论坛 第一步 ...
- java:synchronized
synchronized:利用上锁实现数据同步,避免多线程操作的情况下,数据出现异常. 另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块. 一个线程获得了一个对象的同步锁,那这个对象上所 ...
- Tomcat目录介绍以及运行时寻找class的顺序
来自:http://blog.csdn.net/lihai211/article/details/6651977 Tomcat下的文件目录 /bin:存放启动和关闭tomcat的脚本文件: /conf ...
- 10个用于Web开发的最好 Python 框架
Python 是一门动态.面向对象语言.其最初就是作为一门面向对象语言设计的,并且在后期又加入了一些更高级的特性.除了语言本身的设计目的之外,Python标准 库也是值得大家称赞的,Python甚至还 ...
- c#关键字详解
c#关键字 关键字是对编译器有特殊意义的预定义的保留标识符.它们不能在程序中用作普通标识符,除非在它们前面加上@前缀. 第一部分 一.访问关键字:base,this base:访问基类的成员. 用 ...
- Oracle过程包加密
Oracle加绕功能可以将PL/SQL代码实现部分隐藏,如存储过程.函数.包体等均可使用加绕功能,下面以一个存储过程实现部分加绕来展示Oracle加绕功能的使用. 加绕方法一: 1.编写如下存储过程 ...