標籤:style blog http color os io
單調遞增子序列的變形,一種長方體雖說可以有無限個,但它最多有3中擺放方法(我們假設x方向的長度不小於y方向的長度)。
然後對x遞減一級排序,y遞減二級排序,相當於按面積遞減排序。
dp初始化就是對應狀態的長方體的高度
如果第j個長方體的x,y分別(嚴格)大於第i個長方體的x,y (這裡排序後的j<i)
說明第j個長方體可以放在第i個長方體的下面
更新壘起來的長度
1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 using namespace std; 7 8 struct Cube 9 {10 int x, y, z;11 }cube[100];12 13 int dp[100];14 15 bool cmp(Cube a, Cube b)16 {17 if(a.x != b.x)18 return (a.x > b.x);19 return (a.y > b.y);20 }21 22 int main(void)23 {24 #ifdef LOCAL25 freopen("1069in.txt", "r", stdin);26 #endif27 28 int n, kase = 0;29 while(scanf("%d", &n) && n)30 {31 int i, j;32 for(i = 0; i < n; ++i)33 {34 int x, y, z;35 scanf("%d%d%d", &x, &y, &z);36 cube[i*3].z = z;37 cube[i*3].x = max(x, y);38 cube[i*3].y = min(x, y);39 40 cube[i*3 + 1].z = x;41 cube[i*3 + 1].x = max(y, z);42 cube[i*3 + 1].y = min(y, z);43 44 cube[i*3 + 2].z = y;45 cube[i*3 + 2].x = max(x, z);46 cube[i*3 + 2].y = min(x, z);47 }48 sort(cube, cube + n*3, cmp);49 50 for(i = 0; i < n*3; ++i)51 dp[i] = cube[i].z;52 for(i = 1; i < n*3; ++i)53 for(j = 0; j < i; ++j)54 {55 56 if(cube[j].x > cube[i].x 57 && cube[j].y > cube[i].y)58 dp[i] = max(dp[i], dp[j] + cube[i].z);59 }60 61 int ans = cube[0].z;62 for(i = 0; i < n*3; ++i)63 ans = max(ans, dp[i]);64 printf("Case %d: maximum height = %d\n", ++kase, ans);65 }66 return 0;67 }代碼君