HDU1069 Monkey and Banana(dp)
作者:互联网
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069
题意:给定n种类型的长方体,每个类型长方体无数个,要求长方体叠放在一起,且上面的长方体接触面积要小于下面,长宽也小于下面的长方体,求最高能叠放多高?
思路:首先每个长方体有三种情况可以作为底部,那么一共是3*n种类型的长方体,首先按长宽的大小排序,然后dp。dp[i]表示以第i块长方体为顶的最大高度,那么转移方程就是dp[i] = max(dp[i],dp[j] + g[i]. h),形似一个LIS的dp,枚举 j 即可
AC代码:
1 #include<iostream> 2 #include<vector> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstring> 6 #include<queue> 7 #include<cstdio> 8 #include<unordered_map> 9 #define inf 0x3f3f3f3f 10 using namespace std; 11 typedef long long ll; 12 const int maxn = 100; 13 int dp[maxn]; 14 struct node{ 15 int x,y,z; 16 }g[maxn];//设z为高度 ,x,y分别为长和宽 17 bool cmp(node a,node b){ 18 if(a.x == b.x ) return a.y > b.y ; 19 return a.x > b.x ; 20 } 21 int m,n; 22 int main(){ 23 int cnt = 1; 24 while(scanf("%d",&n)){ 25 if(n == 0) break; 26 memset(dp,0,sizeof(dp)); 27 for(int i = 1;i<=3*n;i+=3){ 28 int x,y,z; 29 scanf("%d%d%d",&x,&y,&z); 30 g[i].x = x<y? x:y; 31 g[i].y = y>x? y:x; 32 g[i].z = z; 33 g[i+1].x = x<z? x:z; 34 g[i+1].y = z>x? z:x; 35 g[i+1].z = y; 36 g[i+2].x = y<z? y:z; 37 g[i+2].y = z>y? z:y; 38 g[i+2].z = x; 39 } 40 sort(g+1,g+1+3*n,cmp); 41 for(int i = 1;i<=3*n;i++) dp[i] = g[i].z ; 42 int ans = dp[1]; 43 for(int i = 2;i<=3*n;i++){ 44 for(int j = 1;j<i;j++){ 45 if(g[j].x > g[i].x && g[j].y > g[i].y ){ 46 dp[i] = max(dp[i],dp[j]+g[i].z );//形似一个LIS的dp 47 ans = max(ans,dp[i]); 48 } 49 } 50 } 51 printf("Case %d: maximum height = %d\n",cnt,ans); 52 cnt++; 53 } 54 return 0; 55 }
标签:cnt,return,Monkey,int,HDU1069,include,长方体,dp 来源: https://www.cnblogs.com/AaronChang/p/12222620.html