标签:acm for monk struct ret 条件 str ble 函数
http://acm.hdu.edu.cn/showproblem.php?pid=1069
题意:给出立方体,求出所能搭成的最大高度,要求是上面一块立方体的长和宽必须严格小于下面一块的长和宽。
思路:每输入一个立方体的长宽高数据,长宽高各自排列组合可以形成6个立方体。用sort函数根据立方体的长从小到大进行排序,次要排序条件为宽。
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 }
标签:acm for monk struct ret 条件 str ble 函数
原文地址:http://www.cnblogs.com/zyb993963526/p/6341535.html