您的位置:首页 > 其它

练习三 1005

2016-05-28 15:50 183 查看
概述:猴子要吃香蕉,它有很多木箱子,每个箱子有长宽高,且每种箱子没有个数限制,放在上面的箱子的长和宽要严格小于下面的箱子的长和宽。

思路;首先需要排序,将箱子长宽按从大到小排列,假设dp[i]是将第i个箱子放在最上面所能到达最大高度,dp[i]=max(dp[i],dp[j]+q[i].h),还有,一个箱子可以分为6种,不过可按排序简化了三种。

感想:自己一直没想出来,只好在网上看的别人的代码,吸取经验。

<span style="font-size:14px;">#include <iostream>
#include <algorithm>
#include <string.h>
#define INF 10000000000;

using namespace std;
int maxn;
int ans;
int dp[1000000];
struct chang
{
int x,y,z;
}q[10000];
bool cmp(const chang &a,const chang &b)
{
if(a.x!=b.x)
return a.x>b.x;
else return a.y>b.y;
}

int main()
{
int n,x[5];
int cnt;
int test=0;
while(cin>>n&&n!=0)
{
memset(dp,0,sizeof(dp));
cnt=0;
q[0].x=q[0].y=INF;
for(int i=1;i<=n;i++)
{
cin>>x[0]>>x[1]>>x[2];
sort(x,x+3);
cnt++;
q[cnt].x=x[0];
q[cnt].y=x[1];
q[cnt].z=x[2];
cnt++;
q[cnt].x=x[0];
q[cnt].y=x[2];
q[cnt].z=x[1];
cnt++;
q[cnt].x=x[1];
q[cnt].y=x[2];
q[cnt].z=x[0];
}
sort(q+1,q+cnt+1,cmp);
for(int i=1;i<=cnt;i++)
{
for(int j=0;j<i;j++)
{
if(q[i].x<q[j].x&&q[i].y<q[j].y)
{
dp[i]=max(dp[i],dp[j]+q[i].z);
}
}
}
ans=0;
for(int i=1;i<=cnt;i++)
{
ans=max(ans,dp[i]);
}
test++;
cout<<"Case "<<test<<": maximum height = "<<ans<<endl;
}
return 0;
}</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: