zoukankan      html  css  js  c++  java
  • HDU 1069 Monkey and Banana (DP)

    题目

    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.

    思路

    这个题意是给你若干种类的长方体,然后你可以任意旋转他,然后我需要用这些积木搭一个最高的高度,问你可以搭多少。这个题目一看就需要排序对吧,这很显然,然后我就走向的错误的方向,我排序完了之后就开始各种乱七八糟转移,我本来是想线性去dp每次的取或不取的,但是在推式子的时候发现你当你前一个不取的时候你没有办法确定前面的状态,也就是说没办法确定该如何转移,所以这个方法去世了。那么现在这种情况我们需要改变一下思路,我们需要从宏观的想法去思考一下,我们发现一个性质这个高度是可以叠加的,意思就是我以这块为底块的最大高度可以由以倒数第二块为底块的加上h得到。那么问题就简单了,思路还是一样,我们需要先排序,关于枚举的顺序,我们需要先枚举小的块,因为这个想法整体的思路是从小状态推大状态,那么我们顺序枚举排序后的数组元素,然后通过枚举前面符合条件的倒数第二块来转移出我当前这块的最高高度。过程中更新答案即可。

    代码实现

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int maxn=1e6+10;
    const int inf=0x3f3f3f3f;
    void check_min (int &a,int b) {a=min (a,b);}
    void check_max (int &a,int b) {a=max (a,b);}
    struct node {
        int l,s,h;
        node (int l,int s,int h):l (l),s (s),h (h) {}
        bool operator < (const node &b) const {
            if (l==b.l) return s<b.h;
            else return l<b.l;
        }
    };
    vector <node> v;
    int dp[maxn];
    int n;
    
    int main () {
        int n,k=0;
        while (~scanf ("%d",&n)) {
            k++;
            if (n==0) break;
            v.clear ();
            for (int i=1,a,b,c;i<=n;i++) {
                scanf ("%d%d%d",&a,&b,&c);
                v.push_back ({a,b,c});
                v.push_back ({b,a,c});
                v.push_back ({c,b,a});
                v.push_back ({c,a,b});
                v.push_back ({a,c,b});
                v.push_back ({b,c,a});
            }
            sort (v.begin (),v.end ());
            memset (dp,0,sizeof (dp));
            int ans=0;
            for (int i=0;i<v.size ();i++) {
                int st=v[i].h;
                dp[i]=st;
                for (int j=i-1;j>=0;j--) {
                    if (v[j].l<v[i].l&&v[j].s<v[i].s&&dp[j]+st>dp[i]) dp[i]=dp[j]+st;
                }
                check_max (ans,dp[i]);
            }
            printf ("Case %d: maximum height = %d
    ",k,ans);
        }
    
        return 0;
    }
    
    
  • 相关阅读:
    jsp int转String or String转int 方法
    log4j详细使用教程
    SQL 查询当天,本月,本周的记录
    1012Linux流编程的一些知识点
    myeclipse过期以后提示过期以后怎么办?!
    mysql常用命令
    Myeclipse文件没出错,但是项目上显示有错的解决办法
    java的一些命名规范吧
    mysql 按照时间查询
    struts1.x和struts2.x之间的一些区别
  • 原文地址:https://www.cnblogs.com/hhlya/p/13910247.html
Copyright © 2011-2022 走看看