ZOJ-1093

31 篇文章 0 订阅

一道基础DP。。但是我DP太菜了。。想了半天没思路,网上搜了搜大牛们的解法才顿悟,DP要加强啊,实在太弱了。。

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

struct Block
{
	int a, b, h;
};

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

static int min(int a, int b)
{
	return a < b ? a : b;
}

static int cmp(const void *p1, const void *p2)
{
	struct Block *b1 = (struct Block*) p1;
	struct Block *b2 = (struct Block*) p2;
	if (b1->a != b2->a)
		return b1->a - b2->a;
	else
		return b1->b - b2->b;
}

int main()
{
	int n, count = 0, dp[100];
	struct Block *array = malloc(100 * sizeof(struct Block));
	while (scanf("%d", &n), n)
	{
		int i, j, a, b, c;
		for (i = 0; i < n; i++)
		{
			scanf("%d %d %d", &a, &b, &c);
			array[i * 3].a = min(a, b);
			array[i * 3].b = max(a, b);
			array[i * 3].h = c;
			array[i * 3 + 1].a = min(a, c);
			array[i * 3 + 1].b = max(a, c);
			array[i * 3 + 1].h = b;
			array[i * 3 + 2].a = min(c, b);
			array[i * 3 + 2].b = max(c, b);
			array[i * 3 + 2].h = a;
		}
		int total = 3 * n;
		qsort(array, total, sizeof(struct Block), cmp);
		for (i = 0; i < total; i++)
			dp[i] = array[i].h;
		for (i = 1; i < total; i++)
			for (j = 0; j < i; j++)
				if (array[j].a < array[i].a && array[j].b < array[i].b)
					dp[i] = max(dp[i], dp[j] + array[i].h);
		int maxx = 0;
		for (i = 0; i < total; i++)
			if (dp[i] > maxx)
				maxx = dp[i];
		printf("Case %d: maximum height = %d\n", ++count, maxx);
	}
	free(array);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值