【DP】【排序】堆积木

D e s c r i p t i o n Description

Leo是一个快乐的火星人,总是能和地球上的OIers玩得很high。
2012到了,Leo又被召回火星了,在火星上没人陪他玩了,但是他有好多好多积木,于是他开始搭积木玩。
火星人能制造n种积木,积木能无限供应。每种积木都是长方体,第i种积木的长、宽、高分别为li、wi、hi。积木可以旋转,使得长宽高任意变换。Leo想要用这些积木搭一个最高的塔。问题是,如果要把一个积木放在另一个积木上面,必须保证上面积木的长和宽都严格小于下面积木的长和宽。这意味着,即使两块长宽相同的积木也不能堆起来。
火星上没有电脑,好心的你决定帮助Leo求出最高的塔的高度。

I n p u t Input

第一行,一个整数n,表示积木的种数
接下来n行,每行3个整数li,wi,hi,表示积木的长宽高

O u t p u t Output

一行一个整数,表示塔高的最大值

S a m p l e Sample I n p u t Input

Sample Input1:
1
10 20 30


Sample Input2:
2
6 8 10
5 5 5



Sample Input3:
5
31 41 59
26 53 58
97 93 23
84 62 64
33 83 27

S a m p l e Sample O u t p u t Output

	Sample Output1:
40


Sample Output2:
21


Sample Output3:
342

H i n t Hint

每种积木都可以拆分成高度分别为li、wi、hi的三种积木,另两边作为长和宽,保证长>=宽。

T r a i n Train o f of T h o u g h t Thought

对于一个积木而言,符合题意的有三种情况,三个数分别为高,剩下的,大的为长,小的为宽
然后以长为第一关键字,以宽为第二关键字从大到小排序
然后跑一遍最长单调下降序列就好了
动态转移方程:
f [ i ] = m a x ( f [ j ] + H [ i ] , f [ i ] ) f[i] = max(f[j] + H[i], f[i])
f [ i ] f[i] 为以第 i i 个为最高层的最大高度
H [ i ] H[i] 为第 i i 个矩形的高度

C o d e Code

#include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;

int n, ans[100005], num, Ans;

struct Brick
{
	int l, w, h;
}A[30002];

bool cmp(Brick i, Brick j)
{
	if (i.l != j.l) return i.l > j.l;
	if (i.w != j.w) return i.w > j.w; 
}//排序

int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i)
	{
		int x, y, z;
		scanf("%d%d%d", &x, &y, &z);
		
		A[++num].h = x;
		A[num].l = max(y, z);
		A[num].w = min(y, z);
		
		A[++num].h = y;
		A[num].l = max(x, z);
		A[num].w = min(x, z);
		
		A[++num].h = z;
		A[num].l = max(y, x);
		A[num].w = min(y, x);	
	}//存储矩形
	sort(A + 1, A + num + 1, cmp);
	for (int i = 1; i <= num; ++i) { 
	  for (int j = i - 1; j >= 1; --j)
	      if (A[j].l > A[i].l && A[j].w > A[i].w) 
	       ans[i] = max(ans[i], ans[j]);
		ans[i] += A[i].h;
		Ans = max(Ans, ans[i]); 
	} 
	printf("%d", Ans);
} 

猜你喜欢

转载自blog.csdn.net/LTH060226/article/details/100048689