zoukankan      html  css  js  c++  java
  • UVa 12099 The Bookcase (DP)

    题意:有 n 本书,每本书有一个高度和宽度,然后让你制作一个3层的书架,可以放下所有的书,并且要高*宽尽量小。

    析:先把所有的书按高度进行排序,然后dp[i][j][k] 表示 前 i 本书,第二 层的宽度是 j,第三层的宽度是 k,第二层和第三层的高度最小,首先我们可以先最高的那本书放到第一层,那么这一层的高度就确定了,同理,在放第二层和第三层的时候,如果是第一本书,那么这一层的高度就已经确定了,为什么不表示第一层的宽度呢,因为总宽度是确定的,如果第二层的宽度和第三层的宽度知道了,那么第一层也就确定了。有了这些就可以进行状态转移了。注意不要转移无用的状态以降低时间复杂度。

    有三种决策,1 放在第一层,dp[i+1][j][k] = min{ dp[i][j][k] }

    2.放在第二层,那么又有两种情况,第一次放,还是不是第一次放

    if(j == 0)  dp[i+1][j+w][k] = min{ dp[i][j][k] + h }

    else  dp[i+1][j+w][k] = min{ dp[i][j][k] }

    3.放在第三层,这一种和第二层一样。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <list>
    #include <assert.h>
    #include <bitset>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    #define sz size()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define all 1,n,1
    #define FOR(x,n)  for(int i = (x); i < (n); ++i)
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 200000 + 10;
    const LL mod = 1e9 + 7;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c) {
      return r > 0 && r <= n && c > 0 && c <= m;
    }
    
    int dp[2][2105][2105];
    struct Node{
      int h, w;
      bool operator < (const Node &p) const {
        return h > p.h;
      }
    };
    Node a[80];
    int sum[maxn];
    
    int main(){
      int T;  cin >> T;
      while(T--){
        scanf("%d", &n);
        for(int i = 1; i <= n; ++i)  scanf("%d %d", &a[i].h, &a[i].w);
        sort(a + 1, a + n + 1);
        for(int i = 2; i <= n; ++i)  sum[i] = sum[i-1] + a[i].w;
        int cnt = 0;
        ms(dp[0], INF);
        dp[0][0][0] = 0;
        for(int i = 1; i < n; ++i, cnt ^= 1){
          ms(dp[cnt^1], INF);
          for(int j = 0; j <= sum[i]; ++j){
            for(int k = 0; k <= sum[i]; ++k){
              if(j + k > sum[i])  break;
              if(dp[cnt][j][k] == INF)  continue;
              dp[cnt^1][j][k] = min(dp[cnt^1][j][k], dp[cnt][j][k]);
              if(j == 0)  dp[cnt^1][a[i+1].w][k] = min(dp[cnt^1][a[i+1].w][k], dp[cnt][j][k] + a[i+1].h);
              else dp[cnt^1][j+a[i+1].w][k] = min(dp[cnt^1][j+a[i+1].w][k], dp[cnt][j][k]);
              if(k == 0)  dp[cnt^1][j][a[i+1].w] = min(dp[cnt^1][j][a[i+1].w], dp[cnt][j][k] + a[i+1].h);
              else dp[cnt^1][j][k+a[i+1].w] = min(dp[cnt^1][j][k+a[i+1].w], dp[cnt][j][k]);
            }
          }
        }
    
        int ans = INF;
        for(int i = 0; i <= sum[n]; ++i)
          for(int j = 0; j <= sum[n]; ++j){
            if(i + j > sum[n])  break;
            if(dp[cnt][i][j] == INF)  continue;
            if(i == 0 || j == 0)  continue;
            ans = min(ans, (dp[cnt][i][j] + a[1].h) * max(j, max(i, sum[n]-j-i+a[1].w)));
          }
        printf("%d
    ", ans);
      }
      return 0;
    }
    

      

  • 相关阅读:
    HDU 5937 Equation(DFS+剪枝)
    HDU 5733 tetrahedron(计算几何)
    BZOJ2243 [SDOI2011]染色(树链剖分+线段树合并)
    计蒜客 微软大楼设计方案(RMQ)
    Codeforces 804D Expected diameter of a tree(树的直径 + 二分 + map查询)
    Codechef Black Nodes in Subgraphs(树型背包)
    2017年暑假集训前的反省
    Codeforces 599E Sandy and Nuts(状压DP)
    Codeforces 570D Tree Requests(树上启发式合并)
    搭建MHA
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7439790.html
Copyright © 2011-2022 走看看