其他分享
首页 > 其他分享> > 【dp每日一题】HDU 1069 Monkey and Banana

【dp每日一题】HDU 1069 Monkey and Banana

作者:互联网

大意:

给出n个种类的正方体,每种都有无穷多数量,现在要求搭建一个塔,从下到上用到的正方体是严格满足上面的边小于下面的边的,问最高能搭多高

思路:

首先需要将n个种类的正方体的六种摆放方式都存下来,然后\(dp[i]\)代表以第i个正方体为顶的塔的高度,那么\(n^2\)去枚举,符合严格小于的条件就更新即可

#include<bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
int n;
struct node{
    int x, y, z;
} a[N];
bool cmp(node a,node b){
    if (a.x == b.x) return a.y > b.y;
    else return a.x > b.x;
}
int dp[N],t=0;
int main(){
    while(scanf("%d",&n)&&n!=0){
        t++;
        for (int i = 0; i < n;i++){
            cin >> a[i * 6].x >> a[i * 6].y >> a[i * 6].z;
            a[i * 6 + 1].x = a[i * 6].y, a[i * 6 + 1].y = a[i * 6].x, a[i * 6 + 1].z = a[i * 6].z;
            a[i * 6 + 2].x = a[i * 6].y, a[i * 6 + 2].y = a[i * 6].z, a[i * 6 + 2].z = a[i * 6].x;
            a[i * 6 + 3].x = a[i * 6].x, a[i * 6 + 3].y = a[i * 6].z, a[i * 6 + 3].z = a[i * 6].y;
            a[i * 6 + 4].x = a[i * 6].z, a[i * 6 + 4].y = a[i * 6].x, a[i * 6 + 4].z = a[i * 6].y;
            a[i * 6 + 5].x = a[i * 6].z, a[i * 6 + 5].y = a[i * 6].y, a[i * 6 + 5].z = a[i * 6].x;
        }
        sort(a, a + 6 * n, cmp);
        for (int i = 0; i < 6 * n;i++){
            dp[i] = a[i].z;
        } 
        int res = 0;
        for (int i = 0; i < 6 * n;i++){
            for (int j = i+1; j < 6*n;j++){
                if(a[j].x<a[i].x&&a[j].y<a[i].y)
                    dp[j] = max(dp[j], dp[i] + a[j].z);
                res = max(dp[i], res);
            } 
        }
        printf("Case %d: maximum height = %d\n",t,res); 
    }
    return 0;
}

标签:1069,正方体,HDU,node,Monkey,int,long,++,dp
来源: https://www.cnblogs.com/dyhaohaoxuexi/p/14182344.html