嵌套矩形先对矩形的长从大到小排列,长相同对宽从大到小排列(可证明不能不对宽排列),然后求LIS
dp[i] = max( dp[j] ) + w[i] ( j<i )
熟悉了这题以后
hdu 1069 Monkey and Banana 就很容易
/* ID: neverchanje PROG: LANG: C++11 */ #include<vector> #include<iostream> #include<cstring> #include<string> #include<algorithm> #include<cmath> #include<cstdio> #include<set> #include<queue> #include<map> #define INF 0Xfffffffff #define st_size (1<<18)-1 #define maxn typedef long long ll; using namespace std; int n; int a,b,c; struct node{ int a,b,c; //长宽(底面),高 bool operator<(const node& x)const{ if(a==x.a) return b>x.b; ///注意!! return a>x.a; } }cube[200]; int dp[200]; int main(){ // freopen("a.txt","r",stdin); // freopen(".out","w",stdout); int cs = 1; while(cin>>n){ if(!n) break; memset(dp,0,sizeof(dp)); int cnt=0,ans=0,maxv; for(int i=0;i<n;i++) { scanf("%d %d %d",&a,&b,&c); cube[cnt++] = (node){max(a,b),min(a,b),c}; cube[cnt++] = (node){max(b,c),min(b,c),a}; cube[cnt++] = (node){max(a,c),min(a,c),b}; } sort(cube,cube+cnt); for(int i=0;i<cnt;i++){ maxv=0; for(int j=0;j<i;j++) if( cube[j].a>cube[i].a && cube[j].b>cube[i].b ) maxv = max(maxv,dp[j]); dp[i] = maxv+cube[i].c; ans = max( dp[i],ans ); } printf("Case %d: maximum height = %d ",cs++,ans); } return 0; } /* DESCRIPTION: 没看到每个block都是unlimited的... 那就好写了: 实际上总共有3*n个block 用类似嵌套矩形的方法 */