Hdu 1069 simple and Dynamic Planning

Source: Internet
Author: User

N types of blocks are provided. The maximum height of these blocks can be stacked. the length and width of the above blocks must be strictly lower than that of the bottom blocks. */[Cpp] # include <stdio. h> # include <stdlib. h> # define N 95 int f [N]; // f [N] records the maximum height after I block struct X {int x, y, z ;} block [N]; int cmp (const struct X * a, const struct X * B) {if (* ). x! = (* B ). x) return (* ). x-(* B ). x; else return (* ). y-(* B ). y;} int main () {int T, n, a, B, c, I, j, temp, tallest; T = 1; while (scanf ("% d ", & n), n) {for (I = 0, j = 0; j <n; j ++) {// each block can be scanf ("% d", & a, & B, & c); block [I]. x = a; block [I]. y = B; block [I]. z = c; block [I + 1]. x = a; block [I + 1]. y = c; block [I + 1]. z = B; block [I + 2]. x = c; block [I + 2]. y = B; block [I + 2]. z = a; I + = 3 ;}for (I = 0; I <n * 3; I ++) {// find the length and width of each block if (block [I]. x <block [I]. y) {Temp = block [I]. x; block [I]. x = block [I]. y; block [I]. y = temp ;}} qsort (block, n * 3, sizeof (block [0]), cmp); // sort by the "length" of the block in ascending order, for (I = 0, tallest = 0; I <3 * n; I ++) {// initialize f [I] to the height of I. When f [I] is calculated, traverse 0-> I to find the maximum height when I block is placed. F [I] = block [I]. z; for (j = 0; j <= I; j ++) {if (block [I]. x> block [j]. x & block [I]. y> block [j]. y) {f [I] = max (f [I], f [j] + block [I]. z);} tallest = max (tallest, f [I]);} printf ("Case % d: maximum height = % d \ n", T ++, tallest);} return 0 ;}

Related Article

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.