您的位置:首页 > 其它

HDU 1069 动态规划(DP) Monkey and Banana

2013-05-04 20:16 429 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069

题意:有n(n<=30)种不同的立方体(每种个数不限), 求能够堆多高.

分析:

(1) 对于每一种立方体, 假设长,宽,高互不相等, 则它放置方法有6种不同的情况(长,宽,高全排列).

(2)那么,实际上可以看成是6*n种不同的立方体.

(3)对这6*n种立方体的长(如果长相等则以宽)小到大排序.

(4)这里就等效于有很多的箱子排成了一列, 看怎么才能将它堆得最高,

(5)从小的一边开始, 如果后面的箱子上面能放下前面较小的, 就放一个那种小的到那些大箱子上.

(6)然后将这一堆看成一个箱子,只是这个箱子比以前加高了,但底面还是不变.

(7)重复(6)的操作. 直到最后没有任何位置的箱子可以放到其位置的箱子上面时, 找到最高的那个就是答案!

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=10000;

struct node
{
int x,y;
int h;
} dp[maxn];

bool cmp(node A, node B)
{
if(A.x==B.x) return A.y<B.y;
return A.x<B.x;
}

int main()
{
int n,cas=1;
while(cin>>n&&n)
{
int N=0;
for(int i=0; i<n; ++i)
{
int a,b,c;
cin>>a>>b>>c;
dp
.x=a, dp
.y=b, dp
.h=c, ++N;
dp
.x=b, dp
.y=a, dp
.h=c, ++N;
dp
.x=c, dp
.y=b, dp
.h=a, ++N;
dp
.x=b, dp
.y=c, dp
.h=a, ++N;
dp
.x=a, dp
.y=c, dp
.h=b, ++N;
dp
.x=c, dp
.y=a, dp
.h=b, ++N;
}
sort(dp,dp+N,cmp);
int ans=dp[0].h;
for(int i=1; i<N; ++i)
{
int temp=0;
for(int j=0; j<i; ++j)
if( dp[i].x>dp[j].x && dp[i].y>dp[j].y )
temp=max(temp,dp[j].h);
dp[i].h += temp;
ans = max( ans, dp[i].h );
}
printf("Case %d: maximum height = %d\n",cas++,ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: