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 ...
随机推荐
- Eclipse 修改项目名称
1:修改项目目录下:.project文件 <?xml version="1.0" encoding="UTF-8"?> ...
- cojs 榴莲 题解报告
首先这道题目是求第k大 求第k大我们有逐位确定,主席树,整体二分等等方法 首先我们考虑如何处理每个询问 我们可以二分答案k,之后扫一遍之前的操作 我们只需要知道有多少个权值>=k的操作经过当前点 ...
- eclipse中(装了插件m2eclipse后的)导入maven工程显示"感叹号"
有时候导入一些开源工程(maven结构的),在eclipse中(装了插件m2eclipse后的)该工程前面会有一个小的红色感叹号,但点开工程后没有发现有打小红叉的内容,这种情况其实大部分是jar包 ...
- Mysql 分页语句Limit用法
转载自:http://qimo601.iteye.com/blog/1634748 1.Mysql的limit用法 在我们使用查询语句的时候,经常要返回前几条或者中间某几行数据,这个时候怎么办呢?不用 ...
- UVa 11922 - Permutation Transformer 伸展树
第一棵伸展树,各种调试模板……TVT 对于 1 n 这种查询我处理的不太好,之前序列前后没有添加冗余节点,一直Runtime Error. 后来加上冗余节点之后又出了别的状况,因为多了 0 和 n+1 ...
- C# 控件双缓冲控制 ControlStyles 枚举详解
ControlStyles 枚举 .NET Framework 4 指定控件的样式和行为. 此枚举有一个 FlagsAttribute 特性,通过该特性可使其成员值按位组合. 命名空间: Sy ...
- SGU 441 Set Division(矩阵快速幂)
题目链接:http://acm.sgu.ru/status.php 题意:将n个有区别的球放到m个无区别的盒子里,盒子不能为空.不同的方案数. 思路:设f[i][j]表示将前i个球放到j个盒子里,那么 ...
- 【HDOJ】2774 Shuffle
1. 题目描述有长度为$n \in [1, 10^5]$的序列,表示一个打乱的循环排列,即每当$[1 \cdots n]$中的数字全部出现后,再重新产生一个随机的覆盖$[1 \cdots n]$的序列 ...
- Web内容管理系统 Magnolia 介绍-挖掘优良的架构(1)
Magnolia简介: Magnolia CMS是一家瑞士公司自2003年起发布的一个基于Java的开源内容管理系统.它适合且已被使用在以下领域:电子商务(例如:COOP.Migros.Rossman ...
- sdut 2351 In Danger (找规律)
题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2351 题意:xyez, xy表示一个十进 ...