题意:

有n本宽w高h的书,向三层书架上放,每层不能为空,求占用的整体的最小面积(总高度*三层中最宽的)

分析:

不太好想,dp[i][j]表示第一层宽度为i第二层为j放的最小高度

dp[i][j]=min(dp[i-w[i]][j],dp[i][j-w[i]])放在第一、二层取最小,当i,j放的是第一本书的时候要加上相应的高度,先按高度升序排列保证正确性。

最后遍历求最小面积

  1. #include <map>
  2. #include <set>
  3. #include <list>
  4. #include <cmath>
  5. #include <queue>
  6. #include <stack>
  7. #include <cstdio>
  8. #include <vector>
  9. #include <string>
  10. #include <cctype>
  11. #include <complex>
  12. #include <cassert>
  13. #include <utility>
  14. #include <cstring>
  15. #include <cstdlib>
  16. #include <iostream>
  17. #include <algorithm>
  18. using namespace std;
  19. typedef pair<int,int> PII;
  20. typedef long long ll;
  21. #define lson l,m,rt<<1
  22. #define pi acos(-1.0)
  23. #define rson m+1,r,rt<<11
  24. #define All 1,N,1
  25. #define read freopen("in.txt", "r", stdin)
  26. const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
  27. const int INF= 0x7ffffff;
  28. const int mod = ;
  29. struct book{
  30. int h,w;
  31. }b[];
  32. int dp[][],sum,n;
  33. bool cmp(book x,book y){
  34. if(x.h==y.h)return x.w<y.w;
  35. return x.h>y.h;
  36. }
  37. void solve(){
  38. for(int i=;i<=sum;++i)
  39. for(int j=;j<=sum;++j)
  40. dp[i][j]=INF;
  41. dp[][]=;
  42. int tw=;
  43. for(int i=;i<n;++i){
  44. for(int j=tw;j>=;--j)
  45. for(int k=tw;k>=;--k){
  46. if(dp[j][k]==INF||j+k>tw)continue;
  47. int tmp=;
  48. if(j==)
  49. tmp=b[i].h;
  50. dp[j+b[i].w][k]=min(dp[j+b[i].w][k],dp[j][k]+tmp);
  51. tmp=;
  52. if(k==)
  53. tmp=b[i].h;
  54. dp[j][k+b[i].w]=min(dp[j][k+b[i].w],dp[j][k]+tmp);
  55. }
  56. tw+=b[i].w;
  57. }
  58. int mina=INF;
  59. for(int i=;i<=sum;++i)
  60. for(int j=;j<=sum;++j){
  61. if(dp[i][j]==INF||i+j>=sum)continue;
  62. int k=sum-i-j;
  63. int mw=max(max(i,j),k);
  64. mina=min(mina,(dp[i][j]+b[].h)*mw);
  65. }
  66. printf("%d\n",mina);
  67. }
  68. int main()
  69. {
  70. int t;
  71. scanf("%d",&t);
  72. while(t--){
  73. scanf("%d",&n);
  74. sum=;
  75. for(int i=;i<n;++i){
  76. scanf("%d%d",&b[i].h,&b[i].w);
  77. sum+=b[i].w;
  78. }
  79. sort(b,b+n,cmp);
  80. solve();
  81. }
  82. return ;
  83. }

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 ...

  10. Codeforces-707D:Persistent Bookcase (离线处理特殊的可持久化问题&&Bitset)

    Recently in school Alina has learned what are the persistent data structures: they are data structur ...

随机推荐

  1. POJ2632Crashing Robots

    做模拟题做的我直接睡着了,题并不难,就是一个细心的问题,有一些细节问题注意了就差不多了,代码写的精美的一般找错误也好找一些,应该学着些好看的代码 #include<cstdio> #inc ...

  2. JAVA WEB中如何让数据库连接对开发人员完全透明?

    书上的技术,确实开了眼界. 列相关测试代码如下,慢慢体会开发的模式. PropsUtil package org.smart4j.chapter2.util; import java.io.FileN ...

  3. OpenStack重启之后,dashboard登录不上去

    ubuntu 12.04装好openstack之后,安装成功,终端打出如下信息: Horizon is now available at http://192.168.0.2/Keystone is ...

  4. JAVA多线程的问题以及处理【转】

    http://www.cnblogs.com/springcsc/archive/2009/12/03/1616394.html 12.4 多线程问题及处理          多线程编程为程序开发带来 ...

  5. cojs QAQ的矩阵 题解报告

    题目描述非常的清晰 首先我们考虑(A*B)^m的求法,这个部分可以参考BZOJ 杰杰的女性朋友 我们不难发现(A*B)^m=A*(B*A)^(m-1)*B A*B是n*n的矩阵,而B*A是k*k的矩阵 ...

  6. cojs 疯狂的粉刷匠 疯狂的斐波那契 题解报告

    疯狂的斐波那契 学习了一些奇怪的东西之后出的题目 最外层要模p是显然的,然而内层并不能模p 那么模什么呢,显然是模斐波那契的循环节 那么我们可以一层层的求出每层的斐波那契循环节 之后在从内向外用矩阵乘 ...

  7. 546C. Soldier and Cards

    题目链接 题意 两个人玩扑克,共n张牌,第一个人k1张,第二个人k2张 给定输入的牌的顺序就是出牌的顺序 每次分别比较两个人牌的第一张,牌上面数字大的赢,把这两张牌给赢的人,并且大的牌放在这个人的牌最 ...

  8. 【Linux高频命令专题(8)】五大查询命令

    find 格式 find 路径 -命令参数 [输出形式] 路径:告诉find在哪儿去找你要的东西 命令参数:参考下面 输出形式:输出形式很多,-print,-printf,-print,-exec,- ...

  9. python 获取当前调用函数名等log信息

    import sys funcName = sys._getframe().f_back.f_code.co_name #获取调用函数名 lineNumber = sys._getframe().f_ ...

  10. 2410中断中SRCPND和INTPND清零的疑问

    2410中断中SRCPND和INTPND清零的疑问SRCPND是中断源引脚寄存器,某个位被置1表示相应的中断被触发,但我们知道在同一时刻内系统可以触发若干个中断,只要中断被触发了,SRCPND的相应位 ...