题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069
分析:每一个方块可以分成6种摆放情况,对于每种情况只能使用一次(严格小于);方块h作为权值不需要看,从大到小排长(排宽一样)
1 #include<iostream> 2 #include<sstream> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<string> 6 #include<cstring> 7 #include<algorithm> 8 #include<functional> 9 #include<iomanip> 10 #include<numeric> 11 #include<cmath> 12 #include<queue> 13 #include<vector> 14 #include<set> 15 #include<cctype> 16 #define PI acos(-1.0) 17 const int INF = 0x3f3f3f3f; 18 const int NINF = -INF - 1; 19 const int maxn = 1e6 + 5; 20 typedef long long ll; 21 using namespace std; 22 int n; 23 struct node 24 { 25 int x, y, h; 26 }cube[200]; 27 bool cmp(node a, node b) 28 { 29 return a.x > b.x; 30 } 31 int dp[200]; 32 int main() 33 { 34 int t = 0; 35 while (scanf("%d", &n) && n) 36 { 37 t++; 38 int num = 0; 39 while (n--) 40 { 41 int a, b, c; 42 scanf("%d%d%d", &a, &b, &c); 43 cube[num].x = a, cube[num].y = b, cube[num++].h = c; 44 cube[num].x = a, cube[num].y = c, cube[num++].h = b; 45 cube[num].x = b, cube[num].y = a, cube[num++].h = c; 46 cube[num].x = b, cube[num].y = c, cube[num++].h = a; 47 cube[num].x = c, cube[num].y = b, cube[num++].h = a; 48 cube[num].x = c, cube[num].y = a, cube[num++].h = b; 49 } 50 sort(cube, cube + num, cmp); 51 memset(dp, 0, sizeof(dp)); 52 dp[0] = cube[0].h; 53 for (int i = 1; i < num; ++i) 54 { 55 dp[i] = cube[i].h; 56 for (int j = i - 1; j >= 0; j--) 57 { 58 if (cube[j].x > cube[i].x && cube[j].y > cube[i].y) 59 dp[i] = max(dp[i], dp[j] + cube[i].h); 60 } 61 } 62 //for (int i = 0; i < num; ++i) 63 // cout << dp[i] << ' '; 64 //cout << endl; 65 int ans = 0; 66 for (int i = 0; i < num; ++i) 67 ans = max(ans, dp[i]); 68 //cout << "Case " << t << ": maximum height = " << ans << endl; 69 printf("Case %d: maximum height = %d ", t, ans); 70 } 71 return 0; 72 }