题意:给出长方体的长宽高,问能叠多高(上面一个的底的长宽要严格大于下一个)。
分析:按底面积排序,设dp[i]为第i个能达到的最大高度,则dp[i] = max{dp[j]} + h[i] ; (j<i)
View Code
1 /* 2 Author:Zhaofa Fang 3 Lang:C++ 4 */ 5 #include <cstdio> 6 #include <cstdlib> 7 #include <sstream> 8 #include <iostream> 9 #include <cmath> 10 #include <cstring> 11 #include <algorithm> 12 #include <string> 13 #include <utility> 14 #include <vector> 15 #include <queue> 16 #include <stack> 17 #include <map> 18 #include <set> 19 20 using namespace std; 21 22 typedef long long ll; 23 #define DEBUG(x) cout<< #x << ':' << x << endl 24 #define PII pair<int,int> 25 #define PB push_back 26 #define MP make_pair 27 #define FI first 28 #define SE second 29 #define lowbit(x) (x&(-x)) 30 #define INF (1<<30) 31 32 struct STONE 33 { 34 int l,w,h; 35 }s[100]; 36 int dp[100]; 37 bool cmp(STONE x,STONE y) 38 { 39 return x.l*x.w>y.l*y.w; 40 } 41 int main() 42 { 43 #ifndef ONLINE_JUDGE 44 freopen("in","r",stdin); 45 #endif 46 int n; 47 int a,b,c; 48 int cas = 0; 49 while(~scanf("%d",&n),n) 50 { 51 int nn=1; 52 while(n--) 53 { 54 scanf("%d%d%d",&a,&b,&c); 55 s[nn].l = a;s[nn].w = b;s[nn++].h=c; 56 s[nn].l = a;s[nn].w = c;s[nn++].h=b; 57 s[nn].l = c;s[nn].w = b;s[nn++].h=a; 58 } 59 sort(s+1,s+nn+1,cmp); 60 for(int i=1;i<nn;i++) 61 { 62 dp[i] = s[i].h; 63 for(int j=1;j<i;j++) 64 { 65 if((s[i].l<s[j].l && s[i].w<s[j].w) || (s[i].l<s[j].w && s[i].w<s[j].l)) 66 { 67 dp[i] = max(dp[i],s[i].h + dp[j]); 68 } 69 } 70 } 71 int ans = -1; 72 for(int i=1;i<nn;i++) 73 { 74 if(ans < dp[i])ans = dp[i]; 75 } 76 printf("Case %d: maximum height = %d\n",++cas,ans); 77 } 78 79 return 0; 80 }