Monkey and Banana
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20094 Accepted Submission(s): 10725
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.
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.
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
Case 2: maximum height = 21
Case 3: maximum height = 28
Case 4: maximum height = 342
题目大意:给出n种长方体的x,y,z(任意个),然后堆起来(要求严格小于自己下面的长方体),求能达到的最大高度。
经典的矩形嵌套:把每种长方体的6种方法都存储起来,然后排序,然后再像上升子序列dp一样。见注释
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define mem(a,b) memset((a),(b),sizeof(a))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define forn(i, x, n) for(int i = (x); i < n; i++)
#define nfor(i, x, n) for(int i = n-1; i >= x; i--)
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF =0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-;
const ll mod = 1e9+; struct node{
int l, w, h;
}stu[];
int dp[];//dp[i] 表示 i能达到的最高高度 bool cmp(node a, node b) {//先x 后 y
if(a.l == b.l)
return a.w < b.w;
return a.l < b.l;
} int main() {
int n, x, y, z, cur;
int icase = ;
while(~scanf("%d", &n),n) {
cur = ;
while(n--) {
scanf("%d%d%d", &x, &y, &z);//存储6种状态
stu[cur].l = x; stu[cur].w = y; stu[cur++].h = z;
stu[cur].l = x; stu[cur].w = z; stu[cur++].h = y;
stu[cur].l = y; stu[cur].w = z; stu[cur++].h = x;
stu[cur].l = y; stu[cur].w = x; stu[cur++].h = z;
stu[cur].l = z; stu[cur].w = x; stu[cur++].h = y;
stu[cur].l = z; stu[cur].w = y; stu[cur++].h = x;
}
sort(stu, stu+cur, cmp);//排序
mem(dp, );
int maxx = -inf;
forn(i, , cur) {
dp[i] = stu[i].h;//初始化
forn(j, , i) {//找到使自己最高的
if(stu[j].l < stu[i].l && stu[j].w < stu[i].w) {
dp[i] = max(dp[j] + stu[i].h, dp[i]);
}
}
maxx = max(maxx, dp[i]);//更新最高高度
}
maxx = max(, maxx);
printf("Case %d: maximum height = %d\n", icase++, maxx);
}
}