您的位置:首页 > 其它

HDU1069——Monkey and Banana

2013-03-27 21:43 369 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069

        题目说长方形是无限的,但是上一层的长方形的长和宽必须严格的小于下一层的长方形的长和宽。因为每组数据有x,y,z,所以考虑分别用xy,xz,yz作为长方形的长和宽,这样每一组数据输入都对应有三种可能。把题目完全抽象出来就是,把每一组数据的三种可能都存放到一个数组里面,再按面积大小进行排序,然后求出最长的递增子序列的对应的高的和就可以了。

参考代码:

#include<stdio.h>
#include<stdlib.h>

struct tu
{
int x;
int y;
int z;
}s[105];

int compare1(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}

int compare2(const void *a, const void *b)
{
struct tu *t1 = (struct tu *)a;
struct tu *t2 = (struct tu*)b;
return (t1->x * t1->y) - (t2->x * t2->y);
}

inline int max(const int a,const int b)
{
return a > b ? a : b;
}

int dp[105];

void main()
{
int n;
int i,j;
int m;
int t = 1;
int Max;
int a[3];

while(scanf("%d",&n))
{
if(n == 0)
break;

j = 0;
while(n--)
{
for(i = 0; i < 3; ++i)
{
scanf("%d",&a[i]);
}
qsort(a,3,sizeof(int),compare1);
s[j].x = a[1]; s[j].y = a[2]; s[j++].z = a[0];
s[j].x = a[0]; s[j].y = a[2]; s[j++].z = a[1];
s[j].x = a[0]; s[j].y = a[1]; s[j++].z = a[2];
}

m = j;
qsort(s,m,sizeof(s[0]),compare2);

Max = 0;
for(i = 0; i < m; ++i)
{
dp[i] = s[i].z;
for(j = 0; j < i; ++j)
{
if(s[j].x < s[i].x && s[j].y < s[i].y)
dp[i] = max(dp[i],dp[j] + s[i].z);
}
Max = max(dp[i], Max);
}
printf("Case %d: maximum height = %d\n",t++, Max);
}
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: