UVa437 The Tower of Babylon

来源:互联网 发布:电子签章软件下载 编辑:程序博客网 时间:2024/05/23 17:41

题目描述
Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this tale have been forgotten. So now, in line with the educational nature of this contest, we will tell you the whole story: The babylonians had n types of blocks, and an unlimited supply of blocks of each type. Each type-i block was a rectangular solid with linear dimensions (xi,yi,zi). A block could be reoriented so that any two of its three dimensions determined the dimensions of the base and the other dimension was the height. They wanted to construct the tallest tower possible by stacking blocks. The problem was that, in building a tower, one block could only be placed on top of another block as long as the two base dimension softhe upper block were both strictly smaller than the corresponding base dimensions of the lower block. This meant, for example, that blocks oriented to have equal-sized bases couldn’t be stacked. Your job is to write a program that determines the height of the tallest tower the babylonians can build with a given set of blocks.


这题除块可以翻转就和嵌套矩形问题一样了,是DAG最长路,然而我一开始没发现,,,想了半天。
如何处理翻转问题呢?
刘汝佳的书里是采用的增加一个维度表示块以哪条边为高:参考代码
我这里采用的是增加5个块的状态。

for(int j=0;j<n;j++){     int i=j*6;     scanf("%d%d%d",&x[i],&y[i],&z[i]);     x[i+1]=y[i+2]=y[i+3]=z[i+4]=z[i+5]=x[i];     z[i+1]=x[i+2]=z[i+3]=y[i+4]=x[i+5]=y[i];     y[i+1]=z[i+2]=x[i+3]=x[i+4]=y[i+5]=z[i];}

设f[i]为以i块为顶的最大高度: f(i)=max(f(j)+y(i))(ij)
一开始写了个递推:

for(int i=0;i<n;i++){    for(int j=0;j<n;j++){        if(x[j]<x[i]&&z[j]<z[i]) f[i]=max(f[i],f[j]+y[i]);    }}

发现过不了样例~
然后加了个维度表示用了几块,就过了:

for(int k=2;k<n;k++){    for(int i=0;i<n;i++){        for(int j=0;j<n;j++){            if(x[j]<x[i]&&z[j]<z[i]) f[k][i]=max(f[k][i],f[k-1][j]+y[i]);        }    }}

时间复杂度O(n3)


分析错误是由于计算f[i]=max(f[i],f[j]+y[i]);时j有可能比i大,这样在计算f[i]时f[j]还没有被算过,所以出错了。
解决办法是把块按照x和z从小到大排好序,在进行递推。
完整代码如下:

#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;int f[190];struct babylon{    int x,y,z;}b[190];bool cmp(const babylon&a,const babylon&b){    return a.x==b.x?a.z<b.z:a.x<b.x;}int main(){    int n,kase=0;    while(cin>>n&&n){        for(int j=0;j<n;j++){            int i=j*6;            scanf("%d%d%d",&b[i].x,&b[i].y,&b[i].z);            b[i+1].x=b[i+2].y=b[i+3].y=b[i+4].z=b[i+5].z=b[i].x;            b[i+1].z=b[i+2].x=b[i+3].z=b[i+4].y=b[i+5].x=b[i].y;            b[i+1].y=b[i+2].z=b[i+3].x=b[i+4].x=b[i+5].y=b[i].z;        }        n*=6;        sort(b,b+n,cmp);         int ans=0;        for(int i=0;i<n;i++){            f[i]=b[i].y;            for(int j=0;j<i;j++){                if(b[j].x<b[i].x&&b[j].z<b[i].z) f[i]=max(f[i],f[j]+b[i].y);            }            ans=max(ans,f[i]);        }        printf("Case %d: maximum height = %d\n",++kase,ans);    }    return 0;}

这题记忆化搜索其实更方便:

#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;int x[190],y[190],z[190],f[190];    int n,kase=0;int dp(int i){    int &ans=f[i];    if(ans>-1) return ans;    ans=y[i];    for(int j=0;j<n;j++)        if(x[j]>x[i]&&z[j]>z[i]) ans=max(ans,dp(j)+y[i]);    return ans;}int main(){    while(cin>>n&&n){        for(int j=0;j<n;j++){            int i=j*6;            scanf("%d%d%d",&x[i],&y[i],&z[i]);            x[i+1]=y[i+2]=y[i+3]=z[i+4]=z[i+5]=x[i];            z[i+1]=x[i+2]=z[i+3]=y[i+4]=x[i+5]=y[i];            y[i+1]=z[i+2]=x[i+3]=x[i+4]=y[i+5]=z[i];        }        n*=6;        memset(f,-1,sizeof(f));        int ans=0;        for(int i=0;i<n;i++) ans=max(ans,dp(i));        printf("Case %d: maximum height = %d\n",++kase,ans);    }    return 0;}

时间复杂度都是O(n2)

总结:看来我还是太菜,很快地想出了处理翻转的方式却没想到这是DAG,我要多刷题啊啊啊!

原创粉丝点击