hdu 1069 Monkey and Banana

题目

http://acm.hdu.edu.cn/showproblem.php?pid=1069

分析

和hdu 1025类似,区别在于本题需要能够识别出来每个矩形,相邻的三个面都要存储(这样才能找到所有合理的组合情况),所以数组中元素个数为矩形个数的3倍。
决策:
当处理第i个的时候,决策为前边i-1个,依次遍历找到最大值(即相容的个数中最大的)

复杂度

O(N*N)

涉及内容

算法:动态规划

感想

分析题目的时候,首先确定用什么方法,然后再确定怎么变化题目条件符合方法,可能会快速找到解题思路

代码

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef struct point
{
    point(){ v=x=y=0; }
    point(int a,int b,int c){ x=a;y=b;v=c; }
    int x,y;
    int v;
} Point;

bool SortPre(Point X,Point Y)
{
    if(X.x>Y.x) return true;
    else if(X.x==Y.x && X.y>Y.y) return true;
    return false;
}

class Mokey{
public:
    vector<Point> coll;
    vector<int> h;
    int num,height,most;//最大高度
    int cx,cy,tx,ty;//当前底面积
    int total;
    static int ci;
    int e[3];

    void init(int n)
    {
        most=height=0;
        cx=cy=65535;
        coll.clear();
        h.clear();
        num=n*3;
        while(n--)
        {
            cin>>e[0]>>e[1]>>e[2];
            sort(e,e+3);
            coll.push_back(Point(e[0],e[1],e[2]));
            coll.push_back(Point(e[0],e[2],e[1]));
            coll.push_back(Point(e[1],e[2],e[0]));
        }
        sort(coll.begin(),coll.end(),SortPre);
    }

    void LDA()
    {
        most=coll[0].v;
        for(int i=0;i<coll.size();++i)
        {
            h.push_back(coll[i].v);
            for(int j=0;j<i;++j)
            {
                if(coll[i].x<coll[j].x&&coll[i].y<coll[j].y&&h[i]<coll[i].v+h[j])
                {
                    h[i]=(coll[i].v+h[j]);
                }
            }
            most=most>h[i]?most:h[i];
        }
        
    }

    void print()
    {
        cout<<"Case "<<ci++<<": maximum height = "<<most<<endl;
    }

    void exec(int n)
    {
        init(n);
        LDA();
        print();
    }

};

int Mokey::ci=1;

int main()
{
    Mokey m;
    int n;
    while(1)
    {
        cin>>n;
        if(n == 0) break;
        m.exec(n);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值