天天写算法之Monkey and Banana

本文介绍了一种使用动态规划解决严格递增序列最大和问题的方法。通过实例代码展示了如何通过比较不同状态转移来找到最优解的过程。适用于动态规划初学者理解和实践。

地址: 点击打开链接
emmmm,需要总结一波了,看到这个题第一眼的确有思路,但是码代码的时候还是犹豫了好一会。就是最佳状态的递推。

dy【这次状态】 = dy【上一状态】+data【跳到这一状态的增加值】
感觉和前面几篇加起来已经快把动态规划的简单问题都涵盖了吧。
这个是动态规划严格递增序列的最大和,与之前求最长之类的不同。因此也mark一下。总结整理一下。


#include <iostream>
#include <iomanip>
#include<queue>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<iomanip>
#include<string.h>
#include<sstream>
#include<string>
//¶¨Ò庯Êý¶Î
#define repf(i,a,b) for(int i =(a);i<(b);i++)
#define repfe(i,a,b) for(int i =(a);i<=(b);i++)
using namespace std;


struct Rec{
public :
    int width;
    int Length;
    int height;
}recs[91];
int dp[91] ;
bool cmp(Rec a,Rec b)
{
    if(a.Length== b.Length)
    {
        return a.width>b.width;
    }
    return a.Length>b.Length;
}

int main() {

    int num,a,b,c,index  ,times = 1;
    while(cin>>num&&num!=0)
    {
        index = 0 ;

        while(num--)
        {
            cin >> a>> b >>c ;
            recs[index].height = a;
            recs[index].Length = max(b,c);
            recs[index].width  = min(b,c);
            index++;
            recs[index].height = c;
            recs[index].Length = max(a,b);
            recs[index].width  = min(a,b);
            index++;
            recs[index].height = b;
            recs[index].Length = max(a,c);
            recs[index].width  = min(a,c);
            index++;
        }

    sort(recs,recs+index,cmp);
    memset(dp,0,sizeof(dp));
    int res = 0;
    for(int i = 0 ; i < index ; i ++)
    {
        dp[i]=recs[i].height;
        for(int j = 0 ; j <  i; j ++)
        {

          if(recs[j].Length>recs[i].Length&&recs[j].width>recs[i].width)
          {
              dp[i] = max(dp[i],recs[i].height+dp[j]);
          }


        }
          res= max(res,dp[i]);
    }
    cout <<"Case "<< times++<<": maximum height = "<<res<<endl;
    }
    return 0;
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值