先说说近况吧,本来觉得上个月会是最辛苦的一个月,毕竟要把许久没学过的东西全都拾起来,还要参加校赛和省赛选拔,算是很累吧,所幸,虽然我实力已大不如从前,还是勉强进了校队,并且获得了去参加省赛的资格,还组了队伍,有两个不错的队友。之后便是训练了,虽说进了校队,可训练基本还是以自学为主。五一集训了两天,考了两场,一场是11道0算法的水题,勉强做出来几道,还有一场是全模版,写的也是不好,两个队友算法学的也不算多。算是哈工程最弱的队了吧。
接下来是省赛,拿了个铁,离铜牌线差了三个队,很无语,但是不得不服气,还是技不如人甘拜下风。可我能怎么办啊,我不是计算机系的,自己系的课业多到炸,还要做科创项目,每天都是挤着时间学算法。既然这样,该如何寻找出路呢?我觉得还是得徐徐图之,急不得,就像看过的一本书上写的,怕的不是稀缺,而是有了稀缺心态。怕的不是我忙不过来,而是怕我自己都觉得自己忙不过来。做一件事的时候心里却在想另一件事,效率肯定高不了,所以干什么的时候就专心做,一定不能分心,否则就是恶性循环。
近期几乎鸽掉了所有的晚自习泡在机房,主要是把之前五一集训的题目在反工一波,学了两个算法:manacher和二分图匹配。过几天端午还要集训,最近还有两个科创项目在做,事儿挺多,只能徐徐图之了。
扯多了,下面正题。
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.
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.
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种型号的长方体长宽高为x,y,z,每种有无穷多个,现在要挑若干个长方体摞一块儿,要求下面的长方体长和宽严格大于上面的,求最高的高度,n<=30。
分析:每种无限多个,给了个完全背包的条件,然而并没有什么卵用,由于题目条件,每种长方体选不了几个,但是这给了我们一个方向,因为给的长方体可以以任意方式摆放(只要符合条件),我们可以将一种方块分成六个不同的方块,也就是取x,y,z全排列,每个排列代表一个方块的长宽高,然后得到的所有的6*n个方块按长从大到小排序(长相等则宽从大到小),然后做一个DP,要求长度宽度单调递减。
设f[i]取第i块做最上面的方块时得到的最大高度。状态转移方程即:
f[i]=max(f[i],f[j]+a[i].h) (j<i)。
结果是把f从1到6*n扫一遍取最大值。(不是f[6*n],想想最长上升子序列那道题。)
预处理:f初始化0;在方块的结构体里加一个地面方块长宽为无限大,高为0.
代码如下:
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define MAXN 32
using namespace std;
int T,N,f[6*MAXN],ans;
inline int max(int aa,int bb){ return aa>=bb?aa:bb; }
struct block
{
int l,w,h;
}a[6*MAXN];
bool mycmp(block aa,block bb)
{
if(aa.l!=bb.l) return aa.l>bb.l;
else return aa.w>bb.w;
}
inline void insert(int ll,int ww,int hh)
{
a[++N].l=ll;
a[N].w=ww,a[N].h=hh;
a[++N].l=ww;
a[N].w=ll,a[N].h=hh;
}
int main()
{
int k=0;
while(1)
{
scanf("%d",&T);
if(T==0) break;
k++;
N=ans=0;
memset(f,0,sizeof(f));
int ll,ww,hh,maxx;
for(int i=1;i<=T;i++)
{
scanf("%d%d%d",&ll,&ww,&hh);
insert(ll,ww,hh);
insert(ll,hh,ww);
insert(hh,ww,ll);
maxx=max(ll,max(ww,hh));
}
a[++N].l=maxx+1;
a[N].w=maxx+1,a[N].h=0;
sort(a+1,a+N+1,mycmp);
for(int i=1;i<=N;i++) f[i]=a[i].h;
for(int i=2;i<=N;i++)
{
for(int j=2;j<i;j++)
{
if(a[i].l<a[j].l&&a[i].w<a[j].w)
f[i]=max(f[i],f[j]+a[i].h);
}
}
for(int i=1;i<=N;i++) ans=max(ans,f[i]);
printf("Case %d: maximum height = %d\n",k,ans);
}
return 0;
}