標籤: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)