HDU - 1069 Monkey and Banana(DAG)

题目大意:给你N种积木,每种的数量不限,现在要求你求出能用这些积木堆出的最大高度
堆的规则如下:在底下的积木的的长和宽要大于在其上面的所有积木的长和宽

解题思路:一种积木有6种摆放方式,所以先预处理出每种积木的所有摆放方式
因为长宽限制,所以每种积木的每种摆放方式最多只有一块
接着排个序,按照面积排序,接下来就是纯DAG问题了

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 210;

struct Block{
    int x, y, z;
}B[N];

int dp[N];
int n;

bool cmp(const Block &a, const Block &b) {
    return a.x * a.y > b.x * b.y;
}

void init() {

    //1 2 3
    for (int i = 0; i < n; i++) 
        scanf("%d%d%d", &B[i].x, &B[i].y, &B[i].z);

    int cnt = n;
    for (int i = 0; i < n; i++) {
        B[cnt].x = B[i].x; B[cnt].y = B[i].z; B[cnt++].z = B[i].y;  //1 3 2
        B[cnt].x = B[i].y; B[cnt].y = B[i].x; B[cnt++].z = B[i].z;  //2 1 3
        B[cnt].x = B[i].y; B[cnt].y = B[i].z; B[cnt++].z = B[i].x;  //2 3 1
        B[cnt].x = B[i].z; B[cnt].y = B[i].x; B[cnt++].z = B[i].y;  //3 1 2 
        B[cnt].x = B[i].z; B[cnt].y = B[i].y; B[cnt++].z = B[i].x;  //3 2 1
    }
    n = cnt;
}

int cas = 1;
void solve() {
    sort(B, B + n, cmp);
    for (int i = 0; i < n; i++)
        dp[i] = B[i].z;

    for (int i = 0; i < n; i++)
        for (int j = 0; j < i; j++) 
            if (B[i].x < B[j].x && B[i].y < B[j].y) 
                dp[i] = max(dp[i], dp[j] + B[i].z);
    int ans = 0;
    for (int i = 0; i < n; i++)
        ans = max(ans, dp[i]);
    printf("Case %d: maximum height = %d\n", cas++, ans);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值