您的位置:首页 > 其它

hdu1069 Monkey and Banana(二维LIS)

2016-10-26 14:52 387 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1069

题意:猴子想吃到上面的香蕉,工作人员给了猴子n种长方体,每种长方体有无限个。猴子要用这些长方体来垒起高塔才能爬上去吃到香蕉,但是必须保证下面长方体的长宽都大于上面长方体的长宽,这样才能给猴子落脚点从而往上爬。求高塔所能达到的最大高度。

思路:刚开始会错题意了,以为二级排序后直接对高进行LIS就行。后来发现,其实就是对x和y进行同步LIS啊。。一种长方体有三种摆法,那么我们索性变成三种长方体,然后对所有的长方体以长方体之间x和y的大小作为判断条件,求最长上升子序列。哎,结构体排序因为把n放在全局变量半天找不出错,题意搞错,做了一晚上,真失败啊= =

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <string.h>
using namespace std;

const int N = 5005;

int dp
;

struct node
{
int x, y, z;
}a
;

bool cmp(node p1, node p2)
{
if(p1.x != p2.x) return p1.x>p2.x;
else return p1.y>p2.y;
}

void init(int n)
{
int x, y, z, cnt = 0;
for(int i = 0; i < n; i++)
{
scanf("%d%d%d", &x, &y, &z);
a[cnt].x = max(x, y);
a[cnt].y = min(x, y);
a[cnt].z = z;
cnt++;
a[cnt].x = max(x, z);
a[cnt].y = min(x, z);
a[cnt].z = y;
cnt++;
a[cnt].x = max(y, z);
a[cnt].y = min(y, z);
a[cnt].z = x;
cnt++;
}
}

int main()
{
// freopen("in.txt", "r", stdin);
int n, Case = 1;
while(~scanf("%d", &n))
{
if(n == 0) break;
init(n);
sort(a, a+3*n, cmp);
memset(dp, 0, sizeof(dp));
for(int i = 0; i < 3*n; i ++)
{
int sum = 0;
for(int j = 0; j < i; j ++)
{
if(a[i].x<a[j].x && a[i].y<a[j].y)//注意下面的x、y都大于上面的
{
sum = max(sum, dp[j]);
}
}
dp[i] = sum+a[i].z;
}
int maxx = 0;
for(int i = 0; i < 3*n; i ++)
{
maxx = max(maxx, dp[i]);
}
printf("Case %d: maximum height = %d\n", Case++, maxx);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: