zoukankan      html  css  js  c++  java
  • uva12099 The Bookcase

    这道题超经典。dp和优化都值得看一看。
    因为i+1只和i有关,用滚动数组节省空间
    暑假第一次做感觉很困难,现在看就清晰了很多

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    const int maxn = 70 + 5;
    const int maxw = 30;
    const int INF = 1000000000;
    
    struct Book {
      int h, w;
      bool operator < (const Book& rhs) const {
        return h > rhs.h || (h == rhs.h && w > rhs.w);
      }
    } books[maxn];
    // dp[i][j][k] is the minimal total heights of level 2 and 3 when we used i books, level 2 and 3's total widths are j and k,
    
    int dp[2][maxn*maxw][maxn*maxw];
    int sumw[maxn]; // sum[i] is the sum of widths of first i books. sum[0] = 0.
    
    // increased height if you place a book with height h to a level with width w
    // if w == 0, that means the level if empty, so height is increased by h
    // otherwise, the height is unchanged because we're adding books in decreasing order of height
    inline int f(int w, int h) {
      return w == 0 ? h : 0;
    }
    
    inline void update(int& newd, int d) {   //更新最低高度
      if(newd < 0 || d < newd) newd = d;
    }
    
    int main () {
      int T;
      scanf("%d", &T);
      while(T--) {
        int n;
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
          scanf("%d%d", &books[i].h, &books[i].w);
        sort(books, books+n);
    
        sumw[0] = 0;
        for(int i = 1; i <= n; i++)
          sumw[i] = sumw[i-1] + books[i-1].w;
    
        dp[0][0][0] = 0;
        int t = 0;                              //滚动数组,t表示当前状态
        for(int i = 0; i < n; i++) {
          // Don't use memset. It's too slow
          for(int j = 0; j <= sumw[i+1]; j++)
            for(int k = 0; k <= sumw[i+1]-j; k++) dp[t^1][j][k] = -1;//初始化下一个状态
    
          for(int j = 0; j <= sumw[i]; j++)
            for(int k = 0; k <= sumw[i]-j; k++)
                if(dp[t][j][k] >= 0) {
              update(dp[t^1][j][k], dp[t][j][k]); // level 1
              update(dp[t^1][j+books[i].w][k], dp[t][j][k] + f(j,books[i].h)); // level 2
              update(dp[t^1][j][k+books[i].w], dp[t][j][k] + f(k,books[i].h)); // level 3
            }
          t ^= 1;
        }
    
        int ans = INF;
        for(int j = 1; j <= sumw[n]; j++) // each level has at least one book
          for(int k = 1; k <= sumw[n]-j; k++) if(dp[t][j][k] >= 0) {
            int w = max(max(j, k), sumw[n]-j-k);
            int h = books[0].h + dp[t][j][k];
            ans = min(ans, w * h);
          }
        printf("%d
    ", ans);
      }
      return 0;
    }
  • 相关阅读:
    centos7 安装kafka Manager
    MySql Table错误:is marked as crashed and last (automatic?) 和 Error: Table "mysql"."innodb_table_stats" not found
    安装prometheus+grafana监控mysql redis kubernetes等
    centos7 安装kubernetes1.4
    linux ip 转发设置 ip_forward
    开启Tomcat远程调试(转)
    SSH自动断开连接的原因、配置(转)
    解决mysql启动时报The server quit without updating PID file 的错误(转)
    supervisor的集中化管理搭建
    supervisor安装配置
  • 原文地址:https://www.cnblogs.com/lqerio/p/9800724.html
Copyright © 2011-2022 走看看