题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069
题目大意:提供了n种砖,有长、宽、高的属性,尽可能的把砖叠高,满足上面的砖宽和长严格的小于下面的砖。求最高的高度。
解题思路:和求最长上升子序列的长度是一样的,只不过多了一个长或则宽的限制条件。首先将题目给的砖的所有种类进行从小到大排序(长为第一关键字,宽为第二关键字),。求这些砖能够叠多高。
AC代码:
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 1e5+10;
struct node{
int x,y,z;
bool operator<(const node a)const{
if(a.x==x) return a.y>y;
return a.x>x;
}
}a[MAXN<<2];
int cnt;
int dp[MAXN];
int cae=0;
void solve(){
int ans=0;
for(int i=0;i<cnt;i++){
dp[i]=a[i].z;
for(int j=0;j<i;j++){
if(a[j].x<a[i].x && a[j].y<a[i].y){
dp[i]=max(dp[i],dp[j]+a[i].z);
}
}
ans=max(ans,dp[i]);
}
printf("Case %d: maximum height = %d\n",++cae,ans);
}
int main(){
int n;
while(~scanf("%d",&n),n){
cnt=0;
for(int i=0;i<n;i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
a[cnt++]=(node){x,y,z};
a[cnt++]=(node){x,z,y};
a[cnt++]=(node){y,z,x};
a[cnt++]=(node){y,x,z};
a[cnt++]=(node){z,x,y};
a[cnt++]=(node){z,y,x};
}
sort(a,a+cnt);
solve();
}
return 0;
}