C - Monkey and Banana HDU - 1069 动态规划

C - Monkey and Banana HDU - 1069
动态规划 O(n^2) dp[i] = max (0….dp[i]) + a

https://vjudge.net/contest/68966#problem/C

#include <string>
#include <iostream>
#include <vector>
#include <cstdio>
#include <queue>
#include <map>
#include <algorithm>
#include <stack>
#include <cstring>

using namespace std;
#define M(a, b) memset(a,b,sizeof(a))

// memset(a,0x3f,sizeof(a))
//memset(a,0xcf,sizeof(a))
typedef long long LL;

const int maxn = 0x3f3f3f3f;
struct Block {
    int x;
    int y;
    int h;
    int area; // 面积
    int total_height;

//    Block(int _x,int _y,int _h):x(_x),y(_y),h(_h){}
};

bool compare(const Block& l, const Block& r){
    // 先按面积从从大到小排序,再按x大小排序

    if(l.area != r.area){
        return l.area> r.area;
    }else {
        return l.x > r.x;
    }
}

void my_swap(int &x, int &y) {
    int t = x;
    x = y;
    y = t;
}
// 按  x>=y  >= z 排序
void swap_three(int &x, int &y, int &z) {
    if (x < y) {
        my_swap(x, y);
    }
    if (x < z) {
        my_swap(x, z);
    }
    if (y < z) {
        my_swap(y, z);
    }
}

int main() {
    int n;
    int tx, ty, th;
    int cake=1;
    while (cin >> n && n != 0) {
        int res=-1;

        Block blocks[3 * n + 1];
        int p=1;
        for (int i = 1; i <= n; i++) {
            cin >> tx >> ty >> th;
            swap_three(tx, ty, th);

            blocks[p].x = tx;
            blocks[p].y = ty;
            blocks[p].h = th;
            blocks[p].area = tx * ty;
            blocks[p].total_height = 0;

            p++;
            blocks[p].x = tx;
            blocks[p].y = th;
            blocks[p].h = ty;
            blocks[p].area = tx * th;
            blocks[p].total_height = 0;

            p++;
            blocks[p].x = ty;
            blocks[p].y = th;
            blocks[p].h = tx;
            blocks[p].area = ty * th;
            blocks[p].total_height = 0;
            p++;

        }
        // 先按面积从从大到小排序,再按x大小排序
        sort(blocks+1,blocks+3*n+1,compare);

        for(int i=1;i<=3*n;i++){

            for(int j=1;j<i;j++){
                if(blocks[j].x > blocks[i].x && blocks[j].y > blocks[i].y){
                    // 动态规划方程
                    blocks[i].total_height = max(blocks[i].total_height, blocks[j].total_height);
                }
            }

            blocks[i].total_height += blocks[i].h;
            res = max(res, blocks[i].total_height);
        }
        printf("Case %d: maximum height = %d\n",cake++,res);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值