https://vjudge.net/problem/UVA-437
这道题和HDU的Monkey and Banana完全一样。
1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 struct node 6 { 7 int l, w, h; 8 }v[181]; 9 10 int dp[181]; //存储第i块立方体为底时的最大高度 11 12 bool cmp(node x, node y) //sort的排序方法,按长从小到大排序 13 { 14 /* 15 if (x.l < y.l) return 1; 16 else if (x.l == y.l && x.w < y.w) return 1; 17 else return 0; 18 */ 19 20 if (x.l == y.l) return x.w < y.w; 21 return x.l < y.l; 22 } 23 24 25 int main() 26 { 27 //freopen("D:\txt.txt", "r", stdin); 28 int n, a, b, c; 29 int kase = 0; 30 while (cin >> n && n) 31 { 32 int k = 0; 33 for (int i = 0; i < n; i++) 34 { 35 cin >> a >> b >> c; 36 v[k].l = a; v[k].w = b; v[k++].h = c; 37 v[k].l = a; v[k].w = c; v[k++].h = b; 38 v[k].l = b; v[k].w = a; v[k++].h = c; 39 v[k].l = b; v[k].w = c; v[k++].h = a; 40 v[k].l = c; v[k].w = a; v[k++].h = b; 41 v[k].l = c; v[k].w = b; v[k++].h = a; 42 } 43 sort(v, v + k, cmp); 44 int maxn = 0; 45 for (int i = 0; i < k; i++) 46 { 47 dp[i]=v[i].h; 48 for (int j = 0; j < i; j++) 49 { 50 if (v[j].l < v[i].l && v[j].w < v[i].w) //如果第j块能搭在第i块上 51 { 52 dp[i] = max(dp[i], dp[j] + v[i].h); 53 } 54 } 55 if (dp[i]>maxn) maxn = dp[i]; 56 } 57 cout << "Case " << ++kase << ": maximum height = " << maxn << endl; 58 } 59 return 0; 60 }