Monkey and Banana(基础dp)

该博客介绍了一道编程题,题目的目标是计算在给定一系列三维砖块,每个砖块有长宽高三个维度,并且可以以不同方向放置多次的情况下,能够堆叠出的最大高度。博主通过分析得出每个砖块有三种放置方式,然后对砖块进行排序,使用动态规划求解最大高度。代码中使用了C++语言,并应用了优化技巧如排序和动态规划。
摘要由CSDN通过智能技术生成
题目传送门

Monkey and Banana

题目大意

给你n个砖块和每个砖块的三个数值,分别为长宽高并且可以任意选择,每个砖块可以使用任意次。
上面的砖块的长宽必须严格小于下面的砖块,求最高可以堆叠砖块到多高?

思路

每个砖块一共有 3 ∗ 2 ∗ 1 3*2*1 321种情况,限制长宽后即为3种情况,所以我们可以将三种情况全部存下(任意次数使用)
然后按照长宽由小到大排序
dp每层可以达到的最大高度(每i层之前的最大高度加上本层高度作为下次比较的高度)
注意点:因为存三次,所以数据范围实际为 0 < = n < = 90 0<=n<=90 0<=n<=90

AC Code

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<math.h>
using namespace std;
#define endl '\n'
#define INF 0x3f3f3f3f
#define int long long
#define debug(a) cout<<#a<<"="<<a<<endl;
// #define TDS_ACM_LOCAL
typedef long long ll;
const double PI=acos(-1.0);
const double e=exp(1.0);
const int M=1e9+7;
const int N=109;
inline int mymax(int x,int y){return x>y?x:y;}
inline int mymin(int x,int y){return x<y?x:y;}
inline int read(){
    register int x=0, f=0;
    register char ch=getchar();
    while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
    return f?-x:x;
}
inline void write(register int x)        {     
    if(x < 0) {putchar('-');x = -x;}        
    if(x > 9)  write(x/10);
    putchar(x % 10 + '0');
}
struct node
{
    int l, w, h;
}a[N];
int n, x, y, z, len, k;
int dp[N];
inline bool cmp(node a, node b){
    if(a.l==b.l)    return a.w<b.w;
    return a.l<b.l;
}

void solve(){
    while(~scanf("%d", &n) && n)
    {
        len=0;
        for(int i=1; i<=n; i++){
            x=read(); y=read(); z=read();
            a[++len].l=mymax(y,z); a[len].w=mymin(y,z); a[len].h=x;
            a[++len].l=mymax(x,z); a[len].w=mymin(x,z); a[len].h=y;
            a[++len].l=mymax(x,y); a[len].w=mymin(x,y); a[len].h=z;
        }
        sort(a+1, a+1+len, cmp);
        dp[1]=a[1].h;
        int mx=0, ans=dp[0];
        for(int i=2; i<=len; i++){
            mx=0;
            for(int j=1; j<i; j++)
                if(a[j].l<a[i].l && a[j].w<a[i].w)
                    mx=mymax(dp[j],mx);
            dp[i]=a[i].h+mx;
            ans=mymax(ans,dp[i]);
        }
        cout<<"Case "<<++k<<": maximum height = "<<ans<<endl;
    }
	return ;
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
#ifdef TDS_ACM_LOCAL
    freopen("D:\\VS code\\.vscode\\testall\\in.txt", "r", stdin);
    freopen("D:\\VS code\\.vscode\\testall\\out.txt", "w", stdout);
#endif
    solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值