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>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