[HDU 1069 Monkey and Banana]DP

来源:互联网 发布:mac序列号有什么用 编辑:程序博客网 时间:2024/06/05 07:04

题目链接  http://acm.hdu.edu.cn/showproblem.php?pid=1069

Monkey and Banana

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


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
110 20 3026 8 105 5 571 1 12 2 23 3 34 4 45 5 56 6 67 7 7531 41 5926 53 5897 93 2384 62 6433 83 270
 

Sample Output
Case 1: maximum height = 40Case 2: maximum height = 21Case 3: maximum height = 28Case 4: maximum height = 342
 

Source
University of Ulm Local Contest 1996
 
其实蛮简单的,思考一下就可以了。
题目意思是给你n个长方体的长宽高,长方体能用各种姿态摆,也就是长能做宽宽能做高高能做长,随意变化。。。
每一种长方体的个数无限,稍微想想就知道一个长方体能摆3个姿势,长宽高能定6种
然后让你把这些长方体像金字塔一样堆叠起来,上面的长方体要小于下面的长方体,宽和长都要小于下面长方体的宽和长,让你求出能摆出的最高高度。
其实就是最大子序列和,写出转移方程的条件就行。
我的做法是,每给你一个长方体的数据,就存6种长方体,然后按照长或者宽从小到大排序,用dp记录用当前长方体做低能堆叠出的最高高度。

代码如下
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;typedef struct node   //开个结构体数组,用来记录dp值和长宽高{    int x, y, z;    int dp;};int cmp(node a, node b){    return a.x < b.x;}node nod[185];int n;int main(){//    freopen("input.txt", "r", stdin);    int s[3];    int a, b, c;    int cnt;    int tt = 1;    while (scanf("%d", &n) != EOF&&n)    {        cnt = 1;        for (int i = 1; i <= n; i++)        {            scanf("%d%d%d", &s[0], &s[1], &s[2]);            sort(s, s + 3);            nod[cnt].x = s[0];    //冗长的储存。。。            nod[cnt].y = s[1];            nod[cnt].z = s[2];            nod[cnt++].dp = s[2];            nod[cnt].x = s[1];            nod[cnt].y = s[0];            nod[cnt].z = s[2];            nod[cnt++].dp = s[2];            nod[cnt].x = s[1];            nod[cnt].y = s[2];            nod[cnt].z = s[0];            nod[cnt++].dp = s[0];            nod[cnt].x = s[2];            nod[cnt].y = s[1];            nod[cnt].z = s[0];            nod[cnt++].dp = s[0];            nod[cnt].x = s[2];            nod[cnt].y = s[0];            nod[cnt].z = s[1];            nod[cnt++].dp = s[1];            nod[cnt].x = s[0];            nod[cnt].y = s[2];            nod[cnt].z = s[1];            nod[cnt++].dp = s[1];        }        sort(nod, nod + cnt, cmp);  //按照宽或者长从小到大排序        for (int i = 1; i < cnt; i++)        {            for (int j = 1; j < i; j++)            {                if (nod[i].x > nod[j].x && nod[i].y > nod[j].y)  //长/宽都要大于上面的长方体的长/宽                    nod[i].dp = max(nod[i].dp, nod[j].dp + nod[i].z);            }        }        int mx = -1;        for (int i = 1; i < cnt; i++)   //取最大值            mx = max(mx, nod[i].dp);        printf("Case %d: maximum height = %d\n", tt++, mx);    }    return 0;}



0 0
原创粉丝点击