[HDU 1069]Monkey and Banana(DP)

來源:互聯網
上載者:User

標籤:blog   http   io   os   for   sp   on   2014   問題   

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1069

題目大意:給你n個積木,告訴你它們的長寬高,要你搭出一個塔,這個塔上面的積木長和寬必須小於下面積木的長和寬,求這個塔的最大高度

思路:將n塊積木轉化成3*n塊積木,相當於每塊積木的原來狀態、翻轉後的狀態。然後對這3n個積木按高度排序,此題就變成了一個類似於求最長下降子序列的問題

#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <algorithm>#define MAXN 1000using namespace std;struct Box{    int x,y,z; //我們定義z為箱子高度}boxes[MAXN];int cnt=0;int f[MAXN]; //f[i]=前i個箱子得到的最大高度bool cmp(Box a,Box b){    if(a.x!=b.x) return a.x>b.x;    else return a.y>b.y;}int main(){    int n,TestCase=0;    while(scanf("%d",&n)!=EOF&&n)    {        cnt=0;        memset(f,0,sizeof(f));        for(int i=1;i<=n;i++)        {            int p[4];            for(int i=1;i<=3;i++)                scanf("%d",&p[i]);            sort(p+1,p+4);            boxes[++cnt].x=p[3],boxes[cnt].y=p[2],boxes[cnt].z=p[1];            boxes[++cnt].x=p[3],boxes[cnt].y=p[1],boxes[cnt].z=p[2];            boxes[++cnt].x=p[2],boxes[cnt].y=p[1],boxes[cnt].z=p[3];        }        sort(boxes+1,boxes+cnt+1,cmp);        for(int i=1;i<=cnt;i++) f[i]=boxes[i].z;        for(int i=cnt-1;i>=1;i--)            for(int j=i+1;j<=cnt;j++)            {                if(boxes[i].x>boxes[j].x&&boxes[i].y>boxes[j].y)                    f[i]=max(f[i],f[j]+boxes[i].z);            }        int ans=f[1];        for(int i=1;i<=cnt;i++)            if(f[i]>ans)                ans=f[i];        printf("Case %d: maximum height = %d\n",++TestCase,ans);    }    return 0;}




[HDU 1069]Monkey and Banana(DP)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.