[HDU 1069] Monkey and Banana (DP)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.