Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1069
Give you n blocks, tell you their length, width, and height. If you want to build a tower, the length and width of the tower must be smaller than the length and width of the following blocks, the maximum height of the tower.
Idea: Converting n blocks into 3 * n blocks is equivalent to the original status of each block and the status after turning. Then sort the 3n blocks by height, which becomes a problem similar to finding the longest descent subsequence.
# Include <iostream> # include <stdio. h> # include <stdlib. h> # include <string. h >#include <algorithm> # define MAXN 1000 using namespace std; struct Box {int x, y, z; // We define z as Box height} boxes [MAXN]; int cnt = 0; int f [MAXN]; // f [I] = maximum bool cmp (Box a, Box B) {if (. x! = B. x) return. x> B. x; else return. y> B. y;} int main () {int n, TestCase = 0; while (scanf ("% d", & n )! = EOF & n) {cnt = 0; memset (f, 0, sizeof (f); for (int I = 1; I <= n; I ++) {int p [4]; for (int I = 1; I <= 3; I ++) scanf ("% d", & p [I]); sort (p + 1, p + 4); boxes [++ cnt]. x = p [3], boxes [cnt]. y = p [2], boxes [cnt]. z = p [1]; boxes [++ cnt]. x = p [3], boxes [cnt]. y = p [1], boxes [cnt]. z = p [2]; boxes [++ cnt]. x = p [2], boxes [cnt]. y = p [1], boxes [cnt]. z = p [3];} sort (boxes + 1, boxes + cnt + 1, cmp); for (int I = 1; I <= cnt; I ++) f [I] = boxes [I]. z; for (int I = cnt-1; I> = 1; I --) for (int j = I + 1; j <= cnt; j ++) {if (boxes [I]. x> boxes [j]. x & boxes [I]. y> boxes [j]. y) f [I] = max (f [I], f [j] + boxes [I]. z);} int ans = f [1]; for (int I = 1; I <= cnt; I ++) if (f [I]> ans) ans = f [I]; printf ("Case % d: maximum height = % d \ n", ++ TestCase, ans);} return 0 ;}
[HDU 1069] Monkey and Banana (DP)