Problem E
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 60 Accepted Submission(s) : 31
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.<br><br>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. <br><br>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. <br><br>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.<br>
Input
The input file will contain one or more test cases. The first line of each test case contains an integer n,<br>representing the number of different blocks in the following data set. The maximum value for n is 30.<br>Each of the next n lines contains three integers
representing the values xi, yi and zi.<br>Input is terminated by a value of zero (0) for n.<br>
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".<br>
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
题目要求:给猴子几个长方体,要求猴子摞起来最高,但是底面箱子的底要大于上面箱子的底。
解题思路:求最大升序子序列的变形。首先要对一个箱子先进行一次降序,6种情况可简化为3种,必须记录3次,x,y,z, x,z,y, y,z,x进行排序,可以先按x降序,若相同则按y降序拍。最后按求最大子序列的方法求解即可。
解题代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
using namespace std;
struct fang
{
int l,m,h;
};
bool comp(fang a,fang b)
{
if(a.l!=b.l)return a.l>b.l;
return a.m>b.m;
}
fang a[100];int d[300];
int main()
{
int t,n,p[3],x,y,z,flag=0;
while(cin>>t&&t)
{
int i,j,k;
for(i=1;i<=t;i++){
cin>>p[0]>>p[1]>>p[2];
sort(p,p+3);
x=p[0];y=p[1];z=p[2];
int ans=(i-1)*3+1;
a[ans].l=x;
a[ans].m=y;
a[ans].h=z;
ans=(i-1)*3+2;
a[ans].l=y;
a[ans].m=z;
a[ans].h=x;
ans=(i-1)*3+3;
a[ans].l=x;
a[ans].m=z;
a[ans].h=y;
}
sort(a+1,a+3*t+1,comp);
memset(d,0,sizeof(d));
d[1]=a[1].h;int max=0;
for(i=2;i<=3*t;i++)
{
int ans=a[i].h;
for(j=1;j<i;j++)
if(a[j].m>a[i].m&&a[j].l>a[i].l&&ans<d[j]+a[i].h)
ans=d[j]+a[i].h;
d[i]=ans;if(d[i]>max)max=d[i];
}flag++;
cout<<"Case "<<flag<<":"<<" maximum height = "<<max<<endl;
}
}
本文介绍了一道经典的算法问题——猴子搭箱问题。通过给定不同尺寸的长方体块,要求搭建最高的塔,但需确保上方的箱子底面积小于下方的箱子。文章详细解释了解决方案,并提供了一个具体的实现代码示例。
1万+

被折叠的 条评论
为什么被折叠?



