zoukankan      html  css  js  c++  java
  • kuangbin专题十二 HDU1069 Monkey and Banana (dp)

    Monkey and Banana

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 20094    Accepted Submission(s): 10725


    Problem Description
    A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a banana at the roof of a building, and at the mean time, provide the monkey with some blocks. If the monkey is clever enough, it shall be able to reach the banana by placing one block on the top another to build a tower and climb up to get its favorite food.

    The researchers have n types of blocks, and an unlimited supply of blocks of each type. Each type-i block was a rectangular solid with linear dimensions (xi, yi, zi). A block could be reoriented so that any two of its three dimensions determined the dimensions of the base and the other dimension was the height.

    They want to make sure that the tallest tower possible by stacking blocks can reach the roof. The problem is that, in building a tower, one block could only be placed on top of another block as long as the two base dimensions of the upper block were both strictly smaller than the corresponding base dimensions of the lower block because there has to be some space for the monkey to step on. This meant, for example, that blocks oriented to have equal-sized bases couldn't be stacked.

    Your job is to write a program that determines the height of the tallest tower the monkey can build with a given set of blocks.
     
    Input
    The input file will contain one or more test cases. The first line of each test case contains an integer n,
    representing the number of different blocks in the following data set. The maximum value for n is 30.
    Each of the next n lines contains three integers representing the values xi, yi and zi.
    Input is terminated by a value of zero (0) for n.
     
    Output
    For each test case, print one line containing the case number (they are numbered sequentially starting from 1) and the height of the tallest possible tower in the format "Case case: maximum height = height".
     
    Sample Input
    1 10 20 30 2 6 8 10 5 5 5 7 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 5 31 41 59 26 53 58 97 93 23 84 62 64 33 83 27 0
     
    Sample Output
    Case 1: maximum height = 40 Case 2: maximum height = 21 Case 3: maximum height = 28 Case 4: maximum height = 342
     

     

    题目大意:给出n种长方体的x,y,z(任意个),然后堆起来(要求严格小于自己下面的长方体),求能达到的最大高度。

    经典的矩形嵌套:把每种长方体的6种方法都存储起来,然后排序,然后再像上升子序列dp一样。见注释

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <math.h>
     4 #include <string.h>
     5 #include <stdlib.h>
     6 #include <string>
     7 #include <vector>
     8 #include <set>
     9 #include <map>
    10 #include <queue>
    11 #include <algorithm>
    12 #include <sstream>
    13 #include <stack>
    14 using namespace std;
    15 #define mem(a,b) memset((a),(b),sizeof(a))
    16 #define mp make_pair
    17 #define pb push_back
    18 #define fi first
    19 #define se second
    20 #define sz(x) (int)x.size()
    21 #define all(x) x.begin(),x.end()
    22 #define forn(i, x, n) for(int i = (x); i < n; i++)
    23 #define nfor(i, x, n) for(int i = n-1; i >= x; i--)
    24 typedef long long ll;
    25 const int inf = 0x3f3f3f3f;
    26 const ll INF =0x3f3f3f3f3f3f3f3f;
    27 const double pi = acos(-1.0);
    28 const double eps = 1e-5;
    29 const ll mod = 1e9+7;
    30 
    31 struct node{
    32     int l, w, h;
    33 }stu[200];
    34 int dp[200];//dp[i] 表示 i能达到的最高高度 
    35 
    36 bool cmp(node a, node b) {//先x 后 y 
    37     if(a.l == b.l)
    38         return a.w < b.w;
    39     return a.l < b.l;
    40 }
    41 
    42 int main() {
    43     int n, x, y, z, cur;
    44     int icase = 1;
    45     while(~scanf("%d", &n),n) {
    46         cur = 0;
    47         while(n--) {
    48             scanf("%d%d%d", &x, &y, &z);//存储6种状态 
    49             stu[cur].l = x; stu[cur].w = y; stu[cur++].h = z; 
    50             stu[cur].l = x; stu[cur].w = z; stu[cur++].h = y;
    51             stu[cur].l = y; stu[cur].w = z; stu[cur++].h = x;
    52             stu[cur].l = y; stu[cur].w = x; stu[cur++].h = z;
    53             stu[cur].l = z; stu[cur].w = x; stu[cur++].h = y;
    54             stu[cur].l = z; stu[cur].w = y; stu[cur++].h = x;
    55         }
    56         sort(stu, stu+cur, cmp);//排序 
    57         mem(dp, 0);
    58         int maxx = -inf;
    59         forn(i, 0, cur) {
    60             dp[i] = stu[i].h;//初始化 
    61             forn(j, 0, i) {//找到使自己最高的 
    62                 if(stu[j].l < stu[i].l && stu[j].w < stu[i].w) {
    63                     dp[i] = max(dp[j] + stu[i].h, dp[i]);
    64                 }
    65             }
    66             maxx = max(maxx, dp[i]);//更新最高高度 
    67         }
    68         maxx = max(0, maxx);
    69         printf("Case %d: maximum height = %d
    ", icase++, maxx);
    70     }
    71 }
  • 相关阅读:
    Centos7LDAP LDAPadmin的完整部署记录(改良版,其它文档太多坑)
    linux weblogic11g 部署
    redis离线集群安装
    Weblogic11g 10.3部署
    jdk安装部署
    tar.xz文件如何解压
    linux-Centos7安装python3并与python2共存
    ssh免密码登录配置方法
    Docker容器安装weblogic详细教程
    linux命令分块总结---多操作才是真理
  • 原文地址:https://www.cnblogs.com/ACMerszl/p/9572935.html
Copyright © 2011-2022 走看看