杭电动态规划题

来源:互联网 发布:nginx lvs 负载均衡 编辑:程序博客网 时间:2024/05/18 02:09

Monkey and Banana

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14738    Accepted Submission(s): 7755


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
110 20 3026 8 105 5 571 1 12 2 23 3 34 4 45 5 56 6 67 7 7531 41 5926 53 5897 93 2384 62 6433 83 270
 

Sample Output
Case 1: maximum height = 40Case 2: maximum height = 21Case 3: maximum height = 28Case 4: maximum height = 342
 翻译:
问题描述
一组研究人员正在设计一个测试猴子智商的实验。他们会在建筑物的屋顶上挂一根香蕉,同时给猴子一些块。如果猴子够聪明,就可以通过在顶部放一个块来建立一个塔,爬上去获得最喜欢的食物,到达香蕉。

研究人员有n种类型的块,并且每种类型的块都是无限制的。每个i型块是具有线性尺寸(xi,yi,zi)的矩形固体。块可以重新定向,使得其三维中的任何两个尺寸确定基部的尺寸,而另一个尺寸确定为高度。

他们想确保最高的塔可能通过堆叠块可以到达屋顶。问题在于,在建造塔架时,只能将一个块体放置在另一个块体的顶部,只要上部块体的两个基础尺寸都严格小于下部块体的对应基础尺寸,因为必须有一些空间可供猴子踩踏。这意味着,例如,面向具有相等尺寸的基座的块不能被堆叠。你的工作是编写一个程序,用来确定猴子可以用一组给定的块建立的最高塔的高度。

输入
输入文件将包含一个或多个测试用例。每个测试用例的第一行包含整数n,
表示以下数据集中不同块的数量。 n的最大值为30。
接下来的n行中的每一行包含表示值xi,yi和zi的三个整数。
n的值为零(0)终止输入。

思路:
因为一个方块,一共有三种不同的摆放方法,可以把一个方块看成三个,n个方块就看成3*n个,然后把它们按从大到小排序,找到一个使高度最高的递减子序列。(动态规划)
 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
struct node
{
    int l, w, h;
}a[1047];

bool cmp(node a, node b)
{
    if(a.l == b.l)
    {
        return a.w > b.w;
    }
    return a.l > b.l;
}

int MAX(int a, int b)
{
    if(a > b)
        return a;
    return b;
}

int dp[1047];//dp[i]:以第i块积木为顶的最大高度

int main()
{
    int n;
    int cas = 0;
    while(scanf("%d",&n) && n)
    {
        int tt[3];
        int k = 0;
        ///一个块当成三个,
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d%d",&tt[0],&tt[1],&tt[2]);
            
            sort(tt,tt+3);
            a[k].l = tt[0];
            a[k].w = tt[1];
            a[k].h = tt[2];
            k++;
            
            a[k].l = tt[1];
            a[k].w = tt[2];
            a[k].h = tt[0];
            k++;
            
            a[k].l = tt[0];
            a[k].w = tt[2];
            a[k].h = tt[1];
            k++;
            
        }
        
        sort(a,a+k,cmp);///
        
        int maxx = 0;
        
        for(int i = 0; i < k; i++)
        {
            dp[i] = a[i].h;
            for(int j = i-1; j >= 0; j--)
            {
                if(a[j].l>a[i].l && a[j].w>a[i].w)
                {
                    dp[i] = MAX(dp[i], dp[j]+a[i].h);
                }
            }
            
            if(dp[i] > maxx)
            {
                maxx = dp[i];
            }
        }
        
        printf("Case %d: maximum height = %d\n",++cas,maxx);
        
    }
    return 0;
}