HDU1069 Monkey and Banana【基础DP】

Monkey and Banana

题意: 有n种类型的砖块,每种类型的砖块都有无限个。第i块砖块的长宽高分别用xi,yi,zi来表示。 同时,由于砖块是可以旋转的,每个砖块的3条边可以组成6种不同的长宽高。在构建塔时,当且仅当A砖块的长和宽都分别小于B砖块的长和宽时,A砖块才能放到B砖块的上面,计算最高可以堆出的砖块们的高度。

题解: 因为一块砖有六个形态,直接将每个形态确定为一种砖块,然后排好序(随便以什么依据,但一定要排好序)。要明确一点,是不能用贪心做的,因为也许在A砖上放置了B,不如在A转上放置C再放置D来的更高。而某种排序上B在C和D前面,所以选择了B导致无法选择C和D,错失了更优答案。
但也不用想的过于复杂,不过两个for循环就能解决。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;

inline int read(){
   int s = 0, w = 1;
   char ch = getchar();
   while(ch < '0' || ch > '9'){if(ch == '-') w = -1; ch = getchar();}
   while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
   return s * w;
}

int n, k;
struct node{
	int l, w, h;
	node(int l, int w, int h): l(l), w(w), h(h) {}
	bool operator<(const node &a)const{
		if(a.l == l)	return a.w < w;
		return a.l < l;
	}
};
void run(){
	int ans = 0;
	int dp[210] = {0};
	vector<node> V;
	for(int i = 0; i < n; i++){
		int a = read(), b = read(), c = read();
		V.push_back(node(a, b, c));
		V.push_back(node(a, c, b));
		V.push_back(node(b, a, c));
		V.push_back(node(b, c, a));
		V.push_back(node(c, a, b));
		V.push_back(node(c, b, a));
	}
	sort(V.begin(), V.end());
	for(int i = 0; i < V.size(); i++){
		dp[i] = V[i].h;
		for(int j = i - 1; j >= 0; j--){
			if(V[j].l > V[i].l && V[j].w > V[i].w && dp[j] + V[i].h > dp[i])
				dp[i] = dp[j] + V[i].h;
		}
		ans = max(ans, dp[i]);
	}
	cout << "Case " << ++k << ": maximum height = " << ans << endl;
}	

int main(){
	while(~scanf("%d", &n) && n)	run();	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值