1. 程式人生 > >[HDU1069]Monkey and Banana

[HDU1069]Monkey and Banana

hdu brush spa cst 至少 include for scan div

題目大意:給你$n$種長方體,要你用這些長方體從下往上疊起來,下面的長方體的長和寬要嚴格大於上面的。求出最高能搭多高。

思路:先得出可以使用的長方體(長>寬,註意高也可以作為一條長或寬,那麽一個長方體至少有3種不同的長寬高),然後根據長排序,接著DP就行了。

具體見代碼。

C++ Code:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct cft{
	int a,b,h;
	bool operator<(const cft& rhs)const{
		if(a!=rhs.a)return a>rhs.a;
		return b>rhs.b;
	}
}box[122];
int dp[122];
int n;
int main(){
	int o=0;
	while(~scanf("%d",&n)&&n){
		int m=0;
		for(int i=1;i<=n;i++){//讀入、處理 
			int x[3];
			scanf("%d%d%d",&x[0],&x[1],&x[2]);
			sort(x,x+3);
			m++;
			box[m]=(cft){x[2],x[1],x[0]};
			m++;
			box[m]=(cft){x[2],x[0],x[1]};
			m++;
			box[m]=(cft){x[1],x[0],x[2]};
		}
		sort(box+1,box+1+m);
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=m;i++)dp[i]=box[i].h;
		for(int i=m-1;i>=1;i--)//DP begin 
		for(int j=i+1;j<=m;j++)
		if(box[i].a>box[j].a&&box[i].b>box[j].b)
		if(dp[i]<dp[j]+box[i].h)dp[i]=dp[j]+box[i].h;//DP end 
		printf("Case %d: maximum height = %d\n",++o,*max_element(dp+1,dp+1+m));
	}
	return 0;
}

  

[HDU1069]Monkey and Banana