标签:
题意:给你n个规格的砖块,告诉你它的长、宽、高,每种规格的砖都有无数块,长宽小的砖块(严格小于,不能等于)可以叠在长宽大的砖块上,问你最多能叠多高。
思路:告诉你一种规格的砖其实给了你三种规格的砖,因为砖是可以翻转的,长宽高可以变化的;
以长为第一变量,宽为第二变量,从大到小排序,这样垫在第n块砖下面的只能从前n-1块选择,选择最大值,累加高度即可。
代码如下:
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> const int N = 35; using namespace std; int n; int dp[3*N]; struct node { int x, y, z; bool operator< (const node &rhs) const{ if(x != rhs.x) return x > rhs.x; return y > rhs.y; } }block[3*N]; int main() { int cnt = 1; while(~scanf("%d", &n)) { int a, b, c; if(n == 0) break; for(int i = 0, x, y, z; i < 3 * n; i ++) { scanf("%d%d%d", &x, &y, &z); a = max(max(x, y), z); c = min(min(x, y), z); b = (x + y + z) - (a + c); block[i].x = a, block[i].y = b, block[i].z = c; block[++i].x = a, block[i].y = c, block[i].z = b; block[++i].x = b, block[i].y = c, block[i].z = a; } sort(block, block + 3 * n); int maxn; for(int i = 0; i < 3*n; i++) { maxn = 0; for(int j = 0; j < i; j++) { if(block[j].x > block[i].x && block[j].y > block[i].y && block[j].z > maxn) { maxn = block[j].z; } } block[i].z += maxn; } maxn = 0; for(int i = 0; i < 3*n; i++) { if(maxn < block[i].z) { maxn = block[i].z; } } printf("Case %d: maximum height = %d\n", cnt, maxn); cnt ++; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/doris1104/article/details/47190441