您的位置:首页 > 其它

HDOJ 1069 Monkey and Banana

2012-08-26 21:50 288 查看
~~~题目链接~~~

题目大意:有个个猴子,现在给它n种不同类型的方块,每种方块不限个数, 要求让它把这些方块堆积起来, 在上面的方块的长和宽要小于下面方块的长和宽,要求求出能堆的最大高度.

思路:对矩形进行统一的摆放,长的边为长, 短的边为宽。也就是说x>=y。 然后按矩形的长宽进行排序(也可以是用面积),就变成了子序列最大和问题了,high[i]表示以第i个矩形为最上面的矩形时的最大高度, high[i] = max(high[i], high[j]+zi) j<i。

code:

#include <iostream>
#include <functional>
#include <algorithm>
#include <string.h>
using namespace std;
class node
{
public:
int x, y, z;
bool operator >(const node& b) const
{
//           return x*y>b.x*b.y;
if(x >b.x)  return true;
else if(x == b.x && y>b.y)return true;
else if(x == b.x && y==b.y && z>b.z) return true;
return false;
}
void make(int x, int y, int z)
{
this->x = x;
this->y = y;
this->z = z;
}
};
node blocks[1002];
int main()
{
int i = 0, j = 0, n = 0, cnt = 0, t = 0, ans = 0, a[3], high[1002];
while(cin>>n, n)
{
cnt = 0;
for(i = 0; i<n; i++)
{
cin>>a[0]>>a[1]>>a[2];
sort(a, a+3, greater<int>());
blocks[cnt++].make(a[0], a[1], a[2]);
blocks[cnt++].make(a[0], a[2], a[1]);
blocks[cnt++].make(a[1], a[2], a[0]);
}
sort(blocks, blocks+cnt, greater<node>());
memset(high, 0, sizeof(high));
high[0] = blocks[0].z;
for(i = 1; i<cnt; i++)
{
for(high[i] = blocks[i].z , j = 0; j<i; j++)//注意对high[i]的初始化的值,开始没注意,1 1 1 0 这组数据wa了
{
if(blocks[j].x>blocks[i].x && blocks[j].y >blocks[i].y && high[j]+blocks[i].z>high[i])
high[i] = high[j]+blocks[i].z;
}
}
ans = 0;
for(i = 0; i<cnt; i++)
ans = max(ans, high[i]);
cout<<"Case "<<++t<<": maximum height = "<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: