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 ...
随机推荐
- POJ2632Crashing Robots
做模拟题做的我直接睡着了,题并不难,就是一个细心的问题,有一些细节问题注意了就差不多了,代码写的精美的一般找错误也好找一些,应该学着些好看的代码 #include<cstdio> #inc ...
- JAVA WEB中如何让数据库连接对开发人员完全透明?
书上的技术,确实开了眼界. 列相关测试代码如下,慢慢体会开发的模式. PropsUtil package org.smart4j.chapter2.util; import java.io.FileN ...
- OpenStack重启之后,dashboard登录不上去
ubuntu 12.04装好openstack之后,安装成功,终端打出如下信息: Horizon is now available at http://192.168.0.2/Keystone is ...
- JAVA多线程的问题以及处理【转】
http://www.cnblogs.com/springcsc/archive/2009/12/03/1616394.html 12.4 多线程问题及处理 多线程编程为程序开发带来 ...
- cojs QAQ的矩阵 题解报告
题目描述非常的清晰 首先我们考虑(A*B)^m的求法,这个部分可以参考BZOJ 杰杰的女性朋友 我们不难发现(A*B)^m=A*(B*A)^(m-1)*B A*B是n*n的矩阵,而B*A是k*k的矩阵 ...
- cojs 疯狂的粉刷匠 疯狂的斐波那契 题解报告
疯狂的斐波那契 学习了一些奇怪的东西之后出的题目 最外层要模p是显然的,然而内层并不能模p 那么模什么呢,显然是模斐波那契的循环节 那么我们可以一层层的求出每层的斐波那契循环节 之后在从内向外用矩阵乘 ...
- 546C. Soldier and Cards
题目链接 题意 两个人玩扑克,共n张牌,第一个人k1张,第二个人k2张 给定输入的牌的顺序就是出牌的顺序 每次分别比较两个人牌的第一张,牌上面数字大的赢,把这两张牌给赢的人,并且大的牌放在这个人的牌最 ...
- 【Linux高频命令专题(8)】五大查询命令
find 格式 find 路径 -命令参数 [输出形式] 路径:告诉find在哪儿去找你要的东西 命令参数:参考下面 输出形式:输出形式很多,-print,-printf,-print,-exec,- ...
- python 获取当前调用函数名等log信息
import sys funcName = sys._getframe().f_back.f_code.co_name #获取调用函数名 lineNumber = sys._getframe().f_ ...
- 2410中断中SRCPND和INTPND清零的疑问
2410中断中SRCPND和INTPND清零的疑问SRCPND是中断源引脚寄存器,某个位被置1表示相应的中断被触发,但我们知道在同一时刻内系统可以触发若干个中断,只要中断被触发了,SRCPND的相应位 ...