437 - The Tower of Babylon(动态规划)

这题感觉比较水了,只不过建立模型的时候需要想一下,给n个长方体,我们不妨给它长宽高固定的3个长方体。

之后根据长宽的大小排序。

dp[i]代表第i个长方体当顶面的时候的高度,所以初始的时候dp[i] = cub[i[.h, dp[i] = dp[j] + cub[i[.h(当j的长宽均严格小于i的时候成立)

13989891 437 The Tower of Babylon Accepted C++ 0.015 2014-08-04 13:43:47
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<string>
#include<sstream>
#include<ctime>
using namespace std;
#define _PI acos(-1.0)
#define INF 1 << 10
#define esp 1e-6
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> pill;
/*===========================================*/
#define MAXD 250 + 5
struct Cub{
    int x,y,h;
    friend bool operator < (Cub p ,Cub q){
        if(p.x != q.x){
            if(p.x < q.x)
                return true;
            else
                return false;
        }
        else {
            if(p.y < p.y)
                return true;
            else
                return false;
        }
    }
}cub[MAXD];
int n,size,Case = 1;
int dp[MAXD]; /*表示第i个木块,维度为j的时候当顶的最大高度*/
bool Input(){
    size = 0;
    scanf("%d",&n);
    if(!n) return false;
    for(int i = 0 ; i < n ; i++){
        int arr[3];
        scanf("%d%d%d",&arr[0],&arr[1],&arr[2]);
        sort(arr,arr + 3);
        cub[size].x = arr[0];
        cub[size].y = arr[1];
        cub[size].h = arr[2];
        size++;
        cub[size].x = arr[1];
        cub[size].y = arr[2];
        cub[size].h = arr[0];
        size++;
        cub[size].x = arr[0];
        cub[size].y = arr[2];
        cub[size].h = arr[1];
        size++;
    }
    sort(cub,cub + size);
    return true;
}
void init(){
    for(int i = 0 ; i < size ; i++)
        dp[i] = cub[i].h;
    return ;
}
void DP(){
    init();
    int ans = 0;
    for(int i = 1 ; i < size ; i++){
        int  x = cub[i].x , y = cub[i].y;
        int _h = cub[i].h;
        for(int j = 0 ; j < i ; j++){
            int _x = cub[j].x , _y = cub[j].y;
            if(_x < x && _y < y){
                dp[i] = max(dp[i],dp[j] + _h);
            }
        }
        ans = max(ans , dp[i]);
    }
    printf("Case %d: maximum height = %d\n",Case++,ans);
}
int main(){
    while(Input()){
        DP();
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值