Monkey and Banana
Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5395Accepted Submission(s): 2765
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
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
题目大意:
有一堆箱子,有长宽高,x,y,z。 规定:放在上面的箱子,无论长和宽都要比下面的箱子大,只有一边大是不行的。
箱子的方向可以任意放。 这样,x,y,z就有六种组合。其实也可以说是三种。
因为要和上面的箱子比较,这里为了计算方便,直接写成六种。
求的是最大高度。
考虑到必须的大箱子在下面。这个其实和最大递增子段和 问题一个性质。
不了解的朋友看这里:http://blog.csdn.net/gaotong2055/article/details/8985452
只不过这个情况,我们需要先排下序。把大的箱子放前面。有点牵强的说,也算是用了贪心的思想。
虽然我们不能确定一个x边最大的箱子不一定就能放在最下面,例如的它的y边很小,但是,它肯定也放不到其它箱子的上面,所以,把它放前面也是可以的。
因此可以按x边排序。不过在最后比较的时候,要严格遵循 x1 > x2 && y1 > y2。
下面的代码,没有对长 宽 高相等的情况做优化,毕竟数据量不算很大。
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
struct Node {
int x, y, z;
};
Node blocks[100];
int opt[100];
int a, b, c;
bool cmp(const Node & a, const Node & b) {
if (a.x == b.x)
return a.y > b.y;
else
return a.x > b.x;
}
int main() {
int n;
int cnt = 0;
while (cin >> n, n) {
cnt++;
for (int i = 0; i < 6* n; i += 6) {
cin >> a >> b >> c;
blocks[i].x = a;
blocks[i].y = b;
blocks[i].z = c;
blocks[i + 1].x = b;
blocks[i + 1].y = c;
blocks[i + 1].z = a;
blocks[i + 2].x = c;
blocks[i + 2].y = a;
blocks[i + 2].z = b;
blocks[i+3].x = a;
blocks[i+3].y = c;
blocks[i+3].z = b;
blocks[i + 4].x = b;
blocks[i + 4].y = a;
blocks[i + 4].z = c;
blocks[i + 5].x = c;
blocks[i + 5].y = b;
blocks[i + 5].z = a;
}
sort(blocks, blocks + 6 * n, cmp);
for (int i = 0; i < 6 * n; i++) {
opt[i] = blocks[i].z;
for (int j = 0; j < i; j++) {
if ( (blocks[i].x < blocks[j].x && blocks[i].y < blocks[j].y)) {
if (opt[i] < opt[j] + blocks[i].z) {
opt[i] = opt[j] + blocks[i].z;
}
}
}
}
int max = 0;
for (int i = 0; i < 6 * n; i++) {
if (opt[i] > max)
max = opt[i];
}
cout <<"Case " << cnt <<": maximum height = " << max << endl;
}
return 0;
}
分享到:
相关推荐
北大POJ3253-POJ3253-Fence Repair【STL优先队列】 解题报告+AC代码
西北工业大学-c语言-POJ-题目及答案-第七季.doc
整理的acm知识分类 ACM-POJ 算法训练指南
这是我个人写的POJ上2314题的Java实现,希望对喜欢ACM的人有帮助
动态规划算法poj1088滑雪实验报告 动态规划算法poj1088滑雪实验报告
北大POJ初级-动态规划 解题报告+AC代码
POJ上的一道题目,自己写的代码,因为想下载别人的, 所以就放上了。
初学者练题开始------在POJ上(注:是百练) 初学者练题开始------在POJ上(注:是百练) 初学者练题开始------在POJ上(注:是百练)
An solution of a problem using Splay Trees
acm的好资料
PKU Online Judge上面很全面的动态规划试题总结。动态规划是ACM考点中最重要的一大类算法之一,对于工作人员来说,动态规划也是实际开发中经常会遇到的算法。这是POJ上面很多DP题目的总结与深刻分析。利于算法学习,...
poj经典动态规划题目解题报告,包括经典的动态规划题目20多道,可以作为学习动态规划系统的资料,包括题目: Pku acm 1179 Polygon Pku acm 1125 Stockbroker Grapevine Pku acm 1160 post office Pku ...
水题, 爆搜const int maxn = 105;
北大POJ1015-Jury Compromise【动态规划DP】 解题报告+AC代码
北大POJ2002-Squares 解题报告+AC代码
twilight-poj-solutionPOJ () solution by twilight想当年要找一题的分析, 解答实在太难了现在都是开源的时代了, 干脆把Archive放到GitHub上, 供后来人参考.POJ ID: 部分题解: 转载请注明出处~
北大ACM-POJ1010 - STAMPS 原比赛(Pacific Northwest 1998)题目的测试数据
北大ACP-POJ 1035 - Spell checker 原比赛题目测试数据(问题G)
poj 1000 - 2000 部分题目 官方分类 poj 1000 - 2000 部分题目 官方分类
(poj1753,poj2965)(2)贪心(poj1328,poj2109,poj2586)(3)递归和分治法.(4)递推.(5)构造法.(poj3295)……中级有:(1)C++的标准模版库的应用. (poj3096,poj3007)(2)较为复杂的模拟题的训练(poj3393,poj1472,poj3371,...