【dp】HDU - 1069 Monkey and Banana

CN_swords發表於2017-05-06
/*
基礎dp
C - Monkey and Banana
時間: 2017/02/20
題意:疊放立方體,使其疊的高度最大。在按底的長寬排序後,進行求最大子序列和
題解:
1. 在按底的長寬排序後,進行求最大子序列和
2. 類似巢狀問題,記憶化搜尋
*/
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
#define N 210
#define INF 0x3f3f3f3f

struct code
{
    int x,y,z;
}f[N];
int mp[N][N],dp[N];
int n;
int dfs(int i)
{
    int &ans = dp[i];
    if(ans > 0) return ans;
    ans = f[i].z;
    for(int j = 0; j < 3*n; j++)
    {
        if(mp[i][j])
            ans = max(ans, dfs(j)+f[i].z);
    }
    return ans;
}
int main()
{
    int k = 0;
    while(~scanf("%d",&n)&&n)
    {
        k++;
        int n1 = n;
        int i = 0;
        while(n1--)
        {
            int a[3];
            scanf("%d%d%d",&a[0],&a[1],&a[2]);
            sort(a,a+3);
            f[i].y = f[i+1].z = f[i+2].x = a[1];
            f[i].x = f[i+1].x = f[i+2].z = a[2];
            f[i].z = f[i+1].y = f[i+2].y = a[0];
            i += 3;
        }
        memset(mp,0,sizeof(mp));
        memset(dp,0,sizeof(dp));
        for(int i = 0; i < 3*n; i++)
        {
            for(int j = 0; j < 3*n; j++)
            {
                if(f[i].x < f[j].x && f[i].y < f[j].y)
                    mp[i][j] = 1;
            }
        }
        int maxn = 0;
        for(int i = 0; i < 3*n; i++)
        {
            maxn = max(maxn,dfs(i));
        }
        printf("Case %d: maximum height = %d\n",k,maxn);
    }
    return 0;
}


相關文章