ZOJ 1093 Monkey and Banana
Question: Give You n bricks of different specifications. Tell you the length, width, and height of the bricks. There are no number of bricks of each type, and there are bricks of different sizes (strictly less than, not equal) you can stack on long and wide bricks and ask how high you can stack.
Train of Thought: Tell you that a type of Brick actually gives you three types of bricks, because the bricks can be flipped, And the length, width, and height can be changed;
Take the length as the first variable and the width as the second variable, and sort the values from large to small. In this way, the Top n blocks can only be selected from the first n-1 blocks, select the maximum value, and accumulate the height.
The Code is as follows:
#include
#include
#include
#include 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, cnt, maxn); cnt ++; } return 0;}