【HDU-1069】Monkey and Banana (转换研究对象)

该博客介绍了一种利用最长上升子序列和贪心策略解决长方体堆叠问题的方法。题目要求在确保长方体严格不重叠的情况下,找出能垒起的最大高度。通过将长方体的不同面存储并排序,然后使用线性动态规划找到最优解。代码示例中展示了如何实现这一过程,并给出了详细的解题思路。

题目链接:Monkey and Banana

题解:Monkey and Banana(最长上升子序列+贪心)

题意:有n种长方体,每种有无限块,求最多能垒多高,要求(长方体可以以任意面为底摆放;上面长方体的长和宽要严格小于下面长方体的长和宽

  • 观察题面,看起来每种有无限块,但是块面是严格小于的,即同一种面不能重叠。
  • 同理可推得一块长方体最多用3次。那么就可以把长方体的3个块面存起来排序,线性dp即可。

代码:

#include<iostream>
#include<map>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=100;

ll dp[N];

struct node{
	int a,b,c;
}blo[N];
int cnt;

bool cmp(node & x,node & y)
{
	if(x.a!=y.a) return x.a<y.a;
	return x.b<y.b;
}

int main()
{
	int n,pos=1;
	while(scanf("%d",&n) && n)
	{
		int a,b,c; cnt=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d %d %d",&a,&b,&c);
			if(a>b) swap(a,b);
			if(b>c) swap(b,c);
			if(a>b) swap(a,b);
			
			blo[cnt++]={a,b,c};
			blo[cnt++]={a,c,b};
			blo[cnt++]={b,c,a};
		}
		
		for(int i=0;i<N;i++) dp[i]=0;
		
		sort(blo,blo+cnt,cmp);
		
		ll res=0;
		for(int i=0;i<cnt;i++)
		{
			dp[i]=blo[i].c;
			for(int j=0;j<i;j++)
				if(blo[j].a<blo[i].a && blo[j].b<blo[i].b) dp[i]=max(dp[i],dp[j]+blo[i].c);
			res=max(res,dp[i]);
		}
		
		printf("Case %d: maximum height = %lld\n",pos,res);
		pos++;
	}
	
	return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值