"Algorithm Series learning" [Kuangbin you fly] topic 12 basic DP1 C-monkey and Banana

Source: Internet
Author: User

Https://vjudge.net/contest/68966#problem/C

"Reference" http://blog.csdn.net/qinmusiyan/article/details/7986263

"Test Instructions": Multiple sets of test data

Each set of test data gives the length, width and height of n bricks, each type of brick has an infinite number, there can be three different placement methods (XY;XZ;YZ), the brick below is larger than the length and width of the brick above, ask the maximum height is how much.

"Thinking": "greedy +dp" each brick has three kinds of placement method, all the state of all the bricks are pressed into the vector, first greedy, by the size of the order, so that its own as much as possible to meet the monotonically increasing; Dp[i] ln[i] This brick is the maximum height of the brick, then Dp[i]=max (dp[k ]+ln[i].h,ln[i].h), where 0<=k<=i-1 and Ln[i]>ln[k], it is important to pay attention to their overloaded operator is, can not be written Ln[k]<ln[i]....orz

Code

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <string>5#include <algorithm>6#include <cmath>7#include <queue>8 9 using namespacestd;Ten Const intmaxn= *; One intN; A intd[3]; - structnode - { the     intl,w,h; -     BOOL operator> (Constnode& p)Const -     { -         return(L&GT;P.L) && (w>P.W); +     } -NodeintllintWwinthh): L (LL), W (WW), H (HH) {} + node () {} A }; at  - BOOLcmpConstnode& x,Constnode&y) - { -     returnx.l*x.w<y.l*Y.W; - } - intdp[3*MAXN]; inVector<node>Ln; - intMain () to { +     intkas=1; -      while(SCANF ("%d", &n) = =1&&N) the     { *Memset (DP,0,sizeof(DP)); $ ln.clear ();Panax Notoginseng          for(intI=0; i<n;i++) -         { thescanf"%d%d%d", &d[0],&d[1],&d[2]); +Sort (d,d+3); ALn.push_back (Node (d[2],d[1],d[0])); theLn.push_back (Node (d[2],d[0],d[1])); +Ln.push_back (Node (d[1],d[0],d[2])); -         } $     //sort (ln,ln+n); $ sort (Ln.begin (), Ln.end (), CMP); -         //Dp[i] Represents the maximum height of the brick as Ln[i] -          thedp[0]=ln[0].h; -         intans=dp[0];Wuyi          for(intI=1; I<ln.size (); i++) the         { -dp[i]=ln[i].h; Wu              for(intk=i-1; k>=0; k--) -             { About             //if (Ln[k]<ln[i]) $                 if(ln[i]>Ln[k]) -                 { -Dp[i]=max (dp[i],dp[k]+ln[i].h); -                 } A             } +ans=Max (ans,dp[i]); the         } -printf"Case %d:maximum height =%d\n", kas++, ans); $          the          the     } the     return 0; the}
View Code

"Doubt" actually still not quite understand greedy there .... Vague.... Why is this sort? First by area, then judge is not strictly small

"Algorithm Series learning" [Kuangbin you fly] topic 12 basic DP1 C-monkey and Banana

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.