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 ...
随机推荐
- hdu 1850 Being a Good Boy in Spring Festival 博弈论
求可行的方案数!! 代码如下: #include<stdio.h> ]; int main(){ int n,m; while(scanf("%d",&n)&a ...
- H5 移动Web框架集合
http://frozenui.github.io/ 一个简洁的h5前端框架 http://weui.github.io/weui/ 腾讯出的WebUI 风格是基于微信
- 欧拉工程第70题:Totient permutation
题目链接 和上面几题差不多的 Euler's Totient function, φ(n) [sometimes called the phi function]:小于等于n的数并且和n是互质的数的个 ...
- avi 格式详解
http://blog.csdn.net/becomly/article/details/6283004 http://blog.csdn.net/easecom/article/details/45 ...
- ADO,OLEDB,ODBC,DAO的区别
ADO NET OLEDB ODBC连接数据库的区别 http://www.doc88.com/p-976312043296.html http://blog.csdn.net/ithomer/art ...
- 2014-9-17二班----8 web project
http://localhost:8080/rwkj1/indexServlet 地址请求后,,,,浏览器 地址栏没有变化 package cn.rwkj.servlet; import java ...
- 检测系统是X86系统,还是X64系统
function IsWin64: Boolean; var Kernel32Handle: THandle; IsWow64Process: function(Handle: Windows.THa ...
- iOS Objective-C对象模型及应用
前言 原创文章,转载请注明出自唐巧的技术博客. 本文主要介绍Objective-C对象模型的实现细节,以及Objective-C语言对象模型中对isa swizzling和method swizzli ...
- Android LayoutInflater.from(context).inflate
在应用中自定义一个view,需要获取这个view的布局,需要用到 (LinearLayout) LayoutInflater.from(context).inflate(R.layout.conten ...
- Buffer数据结构和new IO的Memory-mapped files
一.Buffer类 java.nio.Buffer这个类是用来干什么的?有怎样的结构? "Core Java"中是这样定义的“A buffer is array of values ...