UVA 437 巴比伦塔 【DAG上DP/LIS变形】

【链接】:https://cn.vjudge.net/problem/UVA-437

【题意】:
给你n个立方体,让你以长宽为底,一个个搭起来(下面的立方体的长和宽必须大于上面的长和宽)求能得到的最长高,立方体能翻来覆去交换长宽高来用。 

【代码】:

#include<bits/stdc++.h>

using namespace std;
const int INF = 1e6;
const int N = 50010;
int n,m,T,c,ca;
struct node
{
    int x, y, z;
}a[N];
int d[N];
bool cmp(node a,node b)
{
    return a.x*a.y < b.x*b.y;
}
void dp()
{
    int Max = 0;
    for(int i=1; i<=c; i++){
        d[i] = a[i].z;
        for(int j=1; j<i; j++){
            if(a[i].x > a[j].x && a[i].y > a[j].y)
                d[i] = max(d[i], d[j] + a[i].z);
        }
        Max = max(Max,d[i]);
    }
    printf("Case %d: maximum height = %d\n", ++ca, Max);
}
int main()
{
    while(cin >> n, n)
    {
        int x,y,z;
        c = 0;
        memset(a,0,sizeof(a));
        memset(d,0,sizeof(d));
        for(int i=1;i<=n;i++){
            cin>>x>>y>>z;
            a[++c]=(node){x,y,z};
            a[++c]=(node){y,x,z};
            a[++c]=(node){z,x,y};
            a[++c]=(node){x,z,y};
            a[++c]=(node){y,z,x};
            a[++c]=(node){z,y,x};
        }
        sort(a+1, a+c+1, cmp);
        dp();
    }
}

猜你喜欢

转载自www.cnblogs.com/Roni-i/p/9011978.html