hdu1069 Monkey and Banana

本文通过一个具体的编程实例介绍了如何使用最长递增子序列算法解决特定问题,包括立方体堆叠最大高度的计算过程。代码示例展示了如何初始化状态、进行排序以及动态规划更新等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接点击打开链接

#include <cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
struct Cube{
    int x,y,z;
    Cube(){}
    Cube(int _x,int _y,int _z):x(_x),y(_y),z(_z){}
}dp[200];//总共有30*3*2个状态
bool cmp(const Cube& a,const Cube& b){
    if(a.x!=b.x) return a.x>b.x;
    return a.y>b.y;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif

    int n;
    int a,b,c;
    int k=1;
    while(cin>>n,n!=0){
        int cnt=0;

        while(n--){
            cin>>a>>b>>c;
            dp[cnt++]=Cube(a,b,c);
            dp[cnt++]=Cube(a,c,b);
            dp[cnt++]=Cube(b,a,c);
            dp[cnt++]=Cube(b,c,a);
            dp[cnt++]=Cube(c,a,b);
            dp[cnt++]=Cube(c,b,a);
        }
        sort(dp,dp+cnt,cmp);

        for(int i=1;i<cnt;++i){
            int tmp=0;
            for(int j=0;j<i;++j){
                //如有满足条件的,也就是说可以将第i个方块放入到集合中
                if(dp[j].x>dp[i].x&&dp[j].y>dp[i].y&&dp[j].z>tmp){
                    tmp=dp[j].z;
                }
            }
            dp[i].z+=tmp;
        }
         int height=0;
        for(int i=0;i<cnt;++i){
            if(dp[i].z>height)
            height=dp[i].z;
        }
        printf("Case %d: maximum height = %d\n",k++,height);
    }
    return 0;

}

ps:

本题是利用最长递增子序列的思路来保存的最大高度值,下面代码便于理解:

void proc(){
 int arr[]={1,-1,2,1,3,5,3};
 int len=sizeof(arr)/sizeof(int);
 int ans=1;
 int dp[100];
 for(int i=0;i<len;++i){
  dp[i]=1;
 }
 for(int i=1;i<len;++i){
   for(int j=0;j<=i;++j){
     if(arr[j]<arr[i]){
       dp[i]=max(dp[i],dp[j]+1);
     }
   }
 }
 for(int i=0;i<len;++i)
 cout<<dp[i]<<" ";
 cout<<endl;
}
int main(){
 proc();

return 0;
}
输出的结果为:1 1 2 2 3 4 3

那么也就是dp[i]=max(dp[i],dp[j]+1);也就是以i为结尾的最大递增子序列的长度。动态规划的思想

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值