题目

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

翻译

背景

一组研究人员设计了一个实验来测试智商的猴子
他们在屋顶挂一个香蕉,同时,提供给猴子一些方块
足够聪明的猴子,应当能通过堆方块,爬上屋顶拿到香蕉

研究人员有n种方块,每种有无限个
i 种方块是一个长方体,各边长度为(xi,yi,zi)
方块可以任意放置,也即可以用 xi , yi , zi 中任意一个当作高

确保能通过叠加方块可以到达屋顶
问题在于,在向上搭时,每个方块必须放在比它小的方块上面,这是为了确保猴子有空间来当作阶梯向上爬
这也就意味着,接触面大小相同的方块不能被摞在一起

你的工作是编写一个程序,计算根据所给的方块最高能够达到多高的高度

输入

输入包括多组数据
每组数据的第一行包括一个整数 n 表示方块的种类数( n <= 30 )
接下来 n 行每行有三个数 xi , yi , zi
n==0 时表示输入结束

输出

对于每组数据,在一行内输出 Case case: maximum height = height
其中 case1 开始, height 表示最大的高度

题解

先粗略排下序,采用最长上升子序列的思路

将每个方块看作3个面,分别以3条棱做高,来描述一个方块的底面
dp[i] 表示 i 作为最上面的方块的底面的情况下能达到最高的高度
对于每个方块,找到所有比它大的方块,试试放上去后能达到什么高度,存上最高的那个

要判断两个方块是否满足一个能放到另一个上面,要判断是否严格小于
由于位于结构体里,所以使用了静态函数关键词 static

static bool cmp(Node &a,Node &b) {
    return (a.x < b.x && a.y < b.y);
}

有一点很重要** sort 函数使用的小于不能这样写!! **
比如有 (5,10)(11,2)(3,4) 按照上面的定义前一个都“小于”后一个,但是第三个却“小于”第一个

要让前面的思路成立,我们要把数据排序成每一个可能比它小的都放在它前面
因此可以先按照两个数中较小的排序,再按照较大的那个排序
这样可以确保每个比当前数据小的数据都在当前数据前面

代码

/*
By:OhYee
Github:OhYee
Blog:http://www.oyohyee.com/
Email:oyohyee@oyohyee.com

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
using namespace std;

const int maxn = 35;

int kase = 0;
struct Node {
    int x,y,h;
    Node(int x = 0,int y = 0,int h = 0) {
        this->x = min(x,y);
        this->y = max(x,y);
        this->h = h;
    }
    bool operator < (const Node &rhs)const {
        if(x == rhs.x)
            return y < rhs.y;
        else
            return x < rhs.x;
    }
    static bool cmp(Node &a,Node &b) {
        return (a.x < b.x && a.y < b.y);
    }
};
Node S[3 * maxn];
int dp[3 * maxn];

bool  Do() {
    int n;
    scanf("%d",&n);
    if(n == 0)
        return false;

    printf("Case %d: maximum height = ",++kase);

    for(int i = 0;i < n;i++) {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        S[i * 3 + 1] = Node(x,y,z);
        S[i * 3 + 2] = Node(y,z,x);
        S[i * 3 + 3] = Node(z,x,y);
    }

    sort(S,S + 3 * n + 1);

    int Max = 0;

    for(int i = 1;i <= 3 * n;i++) {
        dp[i] = 0;
        for(int j = 0;j < i;j++) {
            if(Node::cmp(S[j],S[i])) {
                dp[i] = max(dp[i],dp[j] + S[i].h);
            }
        }
        Max = max(Max,dp[i]);
    }

    printf("%d\n",Max);
    return true;
}

int main() {
    while(Do());
    return 0;
}