NYOJ_232_How to eat more Banana
有限制条件的DP:
首先其实能堆叠起来,下面砖头的长和宽比上面的严格要大。
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> using namespace std; struct point { int x,y,z; }p[1005]; bool cmp(point a,point b) { if(a.x==b.x) return a.y>b.y; return a.x>b.x; } int dp[1005]; int main() { int n,i,j,k=1,a,b,re,c; while(~scanf("%d",&n)&&n) { re=0; for(i=0;i<n;++i) { scanf("%d%d%d",&a,&b,&c); /* 三种长宽高就够了,因为只要保证,x恒大于y */ p[re].x=a>b?a:b; p[re].y=a<=b?a:b; p[re++].z=c; p[re].x=a>c?a:c; p[re].y=a<=c?a:c; p[re++].z=b; p[re].x=c>b?c:b; p[re].y=c<=b?c:b; p[re++].z=a; } sort(p,p+re,cmp); for(i=0;i<re;++i) dp[i]=p[i].z; int Max=0,all=0; for(i=0;i<re;++i) { for(j=0,Max=0;j<i;++j) { if(p[i].x<p[j].x&&p[i].y<p[j].y&&Max<dp[j]) Max=dp[j]; } if(dp[i]+Max>dp[i]) dp[i]+=Max; //dp[i]表示取第i个元素的能堆叠起来的最大高度为多少 if(dp[i]>all) all=dp[i]; } printf("Case %d: maximum height = %d\n",k++,all); } return 0; }