Description
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
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
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
题目:给出一些长方体,然后让你把他堆成塔,
要求下面的塔的要比上面的塔大(长和宽),
而且每一种长方体的数量都是无限的。
思路:
因为每块积木最多有3个不同的底面和高度,因此先把每块积木看成三种不同的积木,每种积木只有一个底面和一个高度。n中类型的积木转化为3*n个不同的积木的叠加,对这3 * n个积木的长边从大到小排序;接下来的问题就是找到一个递减的子序列,使得子序列的高度和最大即可。
数组dp:dp[i]表示是以第i块积木为顶的塔的最大高度
因此可得状态转移方程:dp[i] = max(dp[i],dp[j] + r[i].z)(满足积木j的底面长和宽都大于积木i的长和宽)
总结:1,每个木块有无数个,每种情况不同,我们就要有把这个木块的不同情况想成不同木块的思想2,这个题要求长和宽都要大于上面的,那我们首先要保证长和宽是一致的,让结构体的x就代表长,y就代表宽,所以给结构体赋值的时候,要把作为底面的两条边的长的付给x,短的付给y,这一点很容易错,一开始就是没有注意这里,只是判断的时候两种情况都比比,可是这样排序的时候,就出错了
3,排序,为什么要排序呢,假设不排序,那样我们需要内层从头遍历数组,来找这个木块最高的位置在哪里,如果排序之后,那自然就好办,内循环跟一般LIS一样,循环到i就行,因为前面的长和宽都是按照从大到小的顺序来的,后面的一定比他小,所以只比较前面的就行。
4,初始化问题,看discuss许多人都是因为初始化没有吧dp赋值成每个木块的长度,这题应该那样赋值,愿意很简单,中间一个木块,他的长比别的小,但是他的宽却大于所有的木块,所以他哪里也不能放,只能自己,并且有可能这个木块是最高的木块,所以要这样初始化
5.dp还是一样,要找到状态转移方程,就要明白每个状态可以从哪些地方来,怎么来,这题在明白这两点之上,加了一个排序,这对dp新手是一个不错的锻炼机会
很经典的题目,如果没有深刻理解dp要很费力做出来
正确的代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 105;
struct node
{
int h, x, y;
}a[maxn];
int cmp (node a, node b)
{
if(a.x != b.x)
return a.x > b.x;
else
return a.y > b.y;
}
int dp[maxn];
int main()
{
int n, l = 0;
while(~scanf("%d",&n),n)
{
memset(dp,0,sizeof(dp));
l++;
int x,y,z, temp = 1;
while(n--)
{
scanf("%d%d%d",&x,&y,&z); //确保长和宽的一致性
a[temp].x = x > y ? x :y;
a[temp].y = x > y ? y : x ;
a[temp].h = z;
temp++;
a[temp].x = x > z ? x :z;
a[temp].y = x > z? z : x;
a[temp].h = y;
temp++;
a[temp].x = z > y ? z :y;
a[temp].y = z > y? y : z;
a[temp].h = x;
temp++;
}
sort(a+1,a+temp,cmp);
for(int i = 1; i < temp; i++)
dp[i] = a[i].h;
int ans = dp[1];
for(int i = 2; i < temp; i++)
{
for(int j = 1; j < i; j++)
{
if(a[j].x > a[i].x && a[j].y > a[i].y )
dp[i] = max(dp[i], dp[j] + a[i].h);
ans = max(ans, dp[i]);
}
}
printf("Case %d: maximum height = %d\n",l,ans);
}
return 0;
}
下面的是自己写的时候,错误的代码,没有确保长和宽的一致性,引以为戒吧
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 105;
struct node
{
int h, x, y;
}a[maxn];
int cmp (node a, node b)
{
if(a.x != b.x)
return a.x > b.x;
else
return a.y > b.y;
}
int dp[maxn];
int main()
{
int n, l = 0;
while(~scanf("%d",&n),n)
{
memset(dp,0,sizeof(dp));
l++;
int x,y,z, temp = 1;
while(n--)
{
scanf("%d%d%d",&x,&y,&z);
a[temp].x = x;
a[temp].y = y;
a[temp].h = z;
temp++;
a[temp].x = x;
a[temp].y = z;
a[temp].h = y;
temp++;
a[temp].x = z;
a[temp].y = y;
a[temp].h = x;
temp++;
}
sort(a+1,a+temp,cmp);
int ans = 0;
dp[1] = a[1].h;
for(int i = 2; i < temp; i++)
{
for(int j = 1; j < i; j++)
{
if((a[j].x > a[i].x && a[j].y > a[i].y) || (a[j].x > a[i].y && a[j].y > a[i].x)) //自以为这样就可以了,但是前面的排序已经把长宽的位置能乱
dp[i] = max(dp[i], dp[j] + a[i].h);
}
ans = max(ans, dp[i]);
}
printf("Case %d: maximum height = %d\n",l,ans);
}
return 0;
}