[kuangbin带你飞]专题1-23 Monkey and Banana

Posted waaaaaa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[kuangbin带你飞]专题1-23 Monkey and Banana相关的知识,希望对你有一定的参考价值。

题目链接:https://vjudge.net/problem/HDU-1069

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.

InputThe 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.

OutputFor 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

思路:一个盒子六种排布方式故可以直接当作是六个盒子进行存储
在堆放时从最小的盒子开始往大盒子遍历看那些盒子可以放在它上面 找高度最大的并更新
注意0的break!我看了半小时。。。。

AC代码
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 using namespace std;
 5 struct block
 6 {
 7     int x,y,z;
 8 }a[200005];
 9 int dp[200005];
10 /*
11 排序需注意应对长和宽做出严格的要求来避免后面比前面小的情况 
12 */ 
13 bool cmp(block a,block b) 
14 {
15     if(a.x==b.x) return a.y<b.y;
16     return a.x<b.x;
17 }
18 int main()
19 {
20     int n,num=1;
21     while(cin>>n)
22     {
23         if(n==0) break;
24         memset(dp,0,sizeof(dp));
25         int i,j,temp=0;
26         for(i=0;i<n;i++)
27         {
28             int a1,a2,a3;
29             cin>>a1>>a2>>a3;//按照不同的摆放方式一共有六种长宽高的模式 
30             a[temp].x=a1;a[temp].y=a2;a[temp++].z=a3;
31             a[temp].x=a1;a[temp].y=a3;a[temp++].z=a2;
32             a[temp].x=a2;a[temp].y=a1;a[temp++].z=a3;
33             a[temp].x=a2;a[temp].y=a3;a[temp++].z=a1;
34             a[temp].x=a3;a[temp].y=a1;a[temp++].z=a2;
35             a[temp].x=a3;a[temp].y=a2;a[temp++].z=a1;
36         }
37         sort(a,a+temp,cmp);//对所有的木块进行排序 
38         int ans=0;   
39         
40         for(i=0;i<temp;i++)//最少也是有一块的高度 
41         dp[i]=a[i].z;
42         
43         for(i=1;i<temp;i++)//从最小的开始模拟 
44         {
45             int index=0;
46             for(j=i-1;j>=0;j--)//找到可以放到最小的上面的高度最大的木块 
47             {
48                 if(a[i].x>a[j].x&&a[i].y>a[j].y)
49                 {
50                     if(dp[j]>index) index=dp[j];
51                 }
52             }
53             dp[i]=dp[i]+index;
54         }
55         for(i=0;i<temp;i++)//寻找最优解 
56         if(ans<dp[i]) ans=dp[i];
57         
58         cout<<"Case "<<num<<": maximum height = "<<ans<<endl;
59         num++;
60     }
61 }

 




以上是关于[kuangbin带你飞]专题1-23 Monkey and Banana的主要内容,如果未能解决你的问题,请参考以下文章

[kuangbin带你飞]专题1-23 Doing Homework HDU - 1074

[kuangbin带你飞]专题1-23 Doing Homework HDU - 1074

[kuangbin带你飞]专题1-23 免费馅饼 HDU - 1176

[kuangbin带你飞]专题1-23 免费馅饼 HDU - 1176

跟着chengyulala刷题之[kuangbin带你飞]之'并查集'专题/斜眼笑

kuangbin带你飞专题一 简单搜索 题解