题目:The IQ of a monkey
题意:给定数目的木块,木块可以叠加,但是相同底面积的的木块不能放在一起,因为猴子需要一定的面积从一个木快到另一个木块,问在给定的木块中可以确 定的最大的塔高,木块的数量无限。
思路:给定的一组长宽高(x,y,z)中,高度可以是x,y,z中的任意一个,所以一种木块其实是三种木块。
先按照长相等宽的放在前面,如果不相等,长的在前面
感想:贪心不可以
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
struct node
{
int l, w, h;
} a[1047];
int dp[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)
{
return a>b?a:b;
}
int main()
{
int n,i;
int c = 0;
while(scanf("%d",&n)&&n)
{
int tt[3];
int k = 0;
for(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(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",++c,maxx);
}
return 0;
}