给你 n 种箱子, 每种箱子无数个,长宽高一直,现在要一个一个垒起来,并且要求上边的必须长宽都小于下边的才可以,
求最大高度。
我们可以发现一个箱子有6个方法叠起来,所以加入一种箱子我们要记录六种情况,
同时,我们发现如果对 长度 L 进行排序,那么我们只需要去判断每个的宽度是否比上一个小就好了,
同时开数组记录, dp[ i ] 代表 使用第 i 个的最高高度。
之后跑一边 n^2 就好了,非常暴力,总复杂度越在 36*n^2 超不掉。
以下是 AC 代码(看了下题解,其实只有三种情况。。只需要对长度一些情况筛选一下会更好。。当然这种能过)
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int n;
int dp[maxn],tot;
struct node
{
int l,w,h;
}nd[maxn];
bool cmp(node a,node b)
{
if(a.l != b.l)
return a.l>b.l;
return a.w<b.w;
}
int main()
{
int cas=1;
while(~scanf("%d",&n)&&n)
{
int l,w,h;
tot = 0;
memset(dp, 0, sizeof dp);
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&l,&w,&h);
nd[++tot].l=l, nd[tot].w=w, nd[tot].h=h;
nd[++tot].l=l, nd[tot].w=h, nd[tot].h=w;
nd[++tot].l=w, nd[tot].w=l, nd[tot].h=h;
nd[++tot].l=w, nd[tot].w=h, nd[tot].h=l;
nd[++tot].l=h, nd[tot].w=l, nd[tot].h=w;
nd[++tot].l=h, nd[tot].w=w, nd[tot].h=l;
}
sort(nd+1, nd+1+tot, cmp);
dp[1]=nd[1].h;
int mx;
for(int i=2;i<=tot;i++)
{
mx = 0;
for(int j=1;j<i;j++)
{
if(nd[j].w>nd[i].w)
{
mx = max(mx, dp[j]);
}
}
dp[i] = nd[i].h + mx;
}
int ans = dp[1];
for(int i=2;i<=tot;i++)
ans = max(dp[i], ans);
printf("Case %d: maximum height = %d\n",cas++,ans);
}
return 0;
}