标签:hal monk lin art put hang test ict contain
题目链接:https://vjudge.net/problem/HDU-1069
InputThe input file will contain one or more test cases. The first line of each test case contains an integer n,
representing the number of different blocks in the following data set. The maximum value for n is 30.
Each of the next n lines contains three integers representing the values xi, yi and zi.
Input is terminated by a value of zero (0) for n.
OutputFor each test case, print one line containing the case number (they are numbered sequentially starting from 1) and the height of the tallest possible tower in the format "Case case: maximum height = height".
Sample Input
1
10 20 30 2 6 8 10 5 5 5 7 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 5 31 41 59 26 53 58 97 93 23 84 62 64 33 83 27 0
Sample Output
Case 1: maximum height = 40 Case 2: maximum height = 21 Case 3: maximum height = 28 Case 4: maximum height = 342
思路:一个盒子六种排布方式故可以直接当作是六个盒子进行存储
在堆放时从最小的盒子开始往大盒子遍历看那些盒子可以放在它上面 找高度最大的并更新
注意0的break!我看了半小时。。。。
AC代码
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 struct block 6 { 7 int x,y,z; 8 }a[200005]; 9 int dp[200005]; 10 /* 11 排序需注意应对长和宽做出严格的要求来避免后面比前面小的情况 12 */ 13 bool cmp(block a,block b) 14 { 15 if(a.x==b.x) return a.y<b.y; 16 return a.x<b.x; 17 } 18 int main() 19 { 20 int n,num=1; 21 while(cin>>n) 22 { 23 if(n==0) break; 24 memset(dp,0,sizeof(dp)); 25 int i,j,temp=0; 26 for(i=0;i<n;i++) 27 { 28 int a1,a2,a3; 29 cin>>a1>>a2>>a3;//按照不同的摆放方式一共有六种长宽高的模式 30 a[temp].x=a1;a[temp].y=a2;a[temp++].z=a3; 31 a[temp].x=a1;a[temp].y=a3;a[temp++].z=a2; 32 a[temp].x=a2;a[temp].y=a1;a[temp++].z=a3; 33 a[temp].x=a2;a[temp].y=a3;a[temp++].z=a1; 34 a[temp].x=a3;a[temp].y=a1;a[temp++].z=a2; 35 a[temp].x=a3;a[temp].y=a2;a[temp++].z=a1; 36 } 37 sort(a,a+temp,cmp);//对所有的木块进行排序 38 int ans=0; 39 40 for(i=0;i<temp;i++)//最少也是有一块的高度 41 dp[i]=a[i].z; 42 43 for(i=1;i<temp;i++)//从最小的开始模拟 44 { 45 int index=0; 46 for(j=i-1;j>=0;j--)//找到可以放到最小的上面的高度最大的木块 47 { 48 if(a[i].x>a[j].x&&a[i].y>a[j].y) 49 { 50 if(dp[j]>index) index=dp[j]; 51 } 52 } 53 dp[i]=dp[i]+index; 54 } 55 for(i=0;i<temp;i++)//寻找最优解 56 if(ans<dp[i]) ans=dp[i]; 57 58 cout<<"Case "<<num<<": maximum height = "<<ans<<endl; 59 num++; 60 } 61 }
[kuangbin带你飞]专题1-23 Monkey and Banana
标签:hal monk lin art put hang test ict contain
原文地址:https://www.cnblogs.com/waaaaaa/p/12991650.html