HDU 1069 Monkey and Banana 解题心得

原题:

Description

一组研究人员正在设计一项实验,以测试猴子的智商。他们将挂香蕉在建筑物的屋顶,同时,提供一些砖块给这些猴子。如果猴子足够聪明,它应当能够通过合理的放置一些砖块建立一个塔,并爬上去吃他们最喜欢的香蕉。

研究人员有n种类型的砖块,每种类型的砖块都有无限个。第i块砖块的长宽高分别用xi,yi,zi来表示。 同时,由于砖块是可以旋转的,每个砖块的3条边可以组成6种不同的长宽高。

在构建塔时,当且仅当A砖块的长和宽都分别小于B砖块的长和宽时,A砖块才能放到B砖块的上面,因为必须留有一些空间让猴子来踩。

你的任务是编写一个程序,计算猴子们最高可以堆出的砖块们的高度。

Input

输入文件包含多组测试数据。

每个测试用例的第一行包含一个整数n,代表不同种类的砖块数目。n<=30.

接下来n行,每行3个数,分别表示砖块的长宽高。

当n= 0的时候,无需输出任何答案,测试结束。

Output

对于每组测试数据,输出最大高度。格式:Case 第几组数据: maximum height = 最大高度

Sample Input

1

10 20 30 

6 8 10 
5 5 5 

1 1 1 
2 2 2 
3 3 3 
4 4 4 
5 5 5 
6 6 6 
7 7 7 

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

分析:dp[i]=max( for(j=i-1;j>=0;j--)    dp[j]+block[i].h  ,dp[i] )

输入一组数组要存入6个数据,分别对应6中不同的摆放方式,(其实也可以只要存三组同时保证宽小于长)->排序->再用状态转移方程:

dp[i]  表示以第i个砖作为底部可以累加的最大高度

dp[i]=max( for(j=i-1;j>=0;j--)    dp[j]+block[i].h  ,dp[i] )

 

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;

//   状态转移方程:   dp[i]=max(        for(j=i-1;j>=0;j--)    dp[j]+block[i].h  ,dp[i] )
//  复杂度  n^2

struct blocks
{
    int a, b, h;
    bool operator <(const blocks &x)const
    {
        if (a == x.a){
            return b < x.b;
        }
        else{
            return a < x.a;
        }
    }
}block[200];
int dp[200];

bool check(const blocks &x, const blocks &y)
{
    if (x.a < y.a&&x.b < y.b)
        return 1;
    return 0;
}

//                    dp[i]  表示以第i个砖作为底部可以累加的最大高度
int main()
{
    int kase;
    int k = 1;
    while (cin >> kase&&kase != 0)
    {
        int x, y, z;
        int n = kase * 6;
        for (int i = 0; i < n;)
        {
            scanf("%d%d%d", &x, &y, &z);
            block[i].a = x, block[i].b = y, block[i].h = z, i++;
            block[i].a = x, block[i].b = z, block[i].h = y, i++;
            block[i].a = y, block[i].b = x, block[i].h = z, i++;
            block[i].a = y, block[i].b = z, block[i].h = x, i++;
            block[i].a = z, block[i].b = x, block[i].h = y, i++;
            block[i].a = z, block[i].b = y, block[i].h = x, i++;
        }
        sort(block, block + n);
        int Max = 0;
        for (int i = 0; i < n; i++)
        {
            dp[i] = block[i].h;
            for (int j = i - 1; j >= 0; j--)
            {
                if (check(block[j], block[i]) && dp[i] < dp[j] + block[i].h)
                    dp[i] = dp[j] + block[i].h;
            }
            if (Max < dp[i])
                Max = dp[i];

        }
        printf("Case %d: maximum height = %d\n", k++, Max);
    }

    return 0;
}
时间: 2024-08-18 02:26:21

HDU 1069 Monkey and Banana 解题心得的相关文章

HDU 1069 Monkey and Banana dp 题解

HDU 1069 Monkey and Banana 题解 纵有疾风起 题目大意 一堆科学家研究猩猩的智商,给他M种长方体,每种N个.然后,将一个香蕉挂在屋顶,让猩猩通过 叠长方体来够到香蕉. 现在给你M种长方体,计算,最高能堆多高.要求位于上面的长方体的长要大于(注意不是大于等于)下面长方体的长,上面长方体的宽大于下面长方体的宽. 输入输出 开始一个数n,表示有多少种木块,木块的数量无限,然后接下来的n行,每行3个数,是木块的长宽高三个参量 输出使用这些在满足条件的情况下能够摆放的最大高度 解

[2016-03-30][HDU][1069][Monkey and Banana]

时间:2016-03-27 15:19:40 星期日 题目编号:[2016-03-30][HDU][1069][Monkey and Banana] 题目大意:给定n种积木无限个,问这些积木最大能叠多高,上面的积木长宽必须严格小于下面的积木 分析: dp[i]表示第i个积木在顶部时候的最大高度,那么dp[i] = max(dp[i],dp[j] + h[i]);?ji能放在j上面?ji能放在j上面 初始条件就是长宽最大的高度是它自己, #include <algorithm> #include

HDU 1069 Monkey and Banana(DP 长方体堆放问题)

Monkey and Banana 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

HDU 1069 Monkey and Banana (动规)

Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7248    Accepted Submission(s): 3730 Problem Description A group of researchers are designing an experiment to test the IQ of a

HDU 1069 Monkey and Banana(二维偏序LIS的应用)

---恢复内容开始--- Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13003    Accepted Submission(s): 6843 Problem Description A group of researchers are designing an experiment to te

HDU 1069 Monkey and Banana 基础DP

题目链接:Monkey and Banana 大意:给出n种箱子的长宽高.每种不限个数.可以堆叠.询问可以达到的最高高度是多少. 要求两个箱子堆叠的时候叠加的面.上面的面的两维长度都严格小于下面的. 简单的DP,依然有很多地发给当时没想到.比如优先级,比如这么简单粗暴的选择. 1 /* 2 大意是.给出n种箱子的长宽高.每种不限个数.可以堆叠.询问可以达到的最高高度是多少. 3 要求两个箱子堆叠的时候叠加的面.上面的面的两维长度都严格小于下面的. 4 5 样例: 6 10 20 30 7 10

HDU 1069 Monkey and Banana LCS变形

点击打开链接题目链接 Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7617    Accepted Submission(s): 3919 Problem Description A group of researchers are designing an experiment to test

hdu 1069 Monkey and Banana (结构体排序,也属于简单的dp)

Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7770    Accepted Submission(s): 4003 Problem Description A group of researchers are designing an experiment to test the IQ of a

DP [HDU 1069] Monkey and Banana

Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7854    Accepted Submission(s): 4051 Problem Description A group of researchers are designing an experiment to test the IQ of a