hdu 1069 Monkey and Banana

题目链接

其实,dp我一直是拒绝的。这方面我一直十分薄弱。但是难关还是一定要攻克的= =如是我终于开始磕dp专题了。那就先从比较简单的问题开始吧。

题目大意是有一些不同规格立方块无限个,把它们叠起来。要求下方的在横截面上长宽严格大于上面的,问叠起来最高的高度。

分析:

考虑到立方块可以交换长宽高,所以我把一个立方块变成六个不同的立方块来思考。将变换长宽高后的立方块按照关键词先后先a从小到大,再b从大到小,最后c从小到大的顺序排序并且去重。这样,我们能够简化考虑的维数。因为a已经按照从小到大的顺序排好了,我们只需要考虑b的大小就可以了。不难想到一个递推式子 dp[i]=max(dp[k])+height[i] 其中要求第k个立方块的b[k]

代码:
/*****************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

#define   offcin        ios::sync_with_stdio(false)
#define   DEBUG         freopen("#.txt", "r", stdin)
#define   sigma_size    26
#define   lson          l,m,v<<1
#define   rson          m+1,r,v<<1|1
#define   slch          v<<1
#define   srch          v<<1|1
#define   ll            long long
#define   ull           unsigned long long
#define   lowbit(x)     (x&-x)

const int    INF    = 0x3f3f3f3f;
const ll     INFF   = 1e18;
const double pi     = acos(-1.0);
const double inf    = 1e18;
const double eps    = 1e-9;
const ll     mod    = 1e9+7;
const int    maxmat = 10;
const ull    BASE   = 133333331;

/*****************************************************/
inline void RI(int &x) {
      char c;
      while((c=getchar())<'0' || c>'9');
      x=c-'0';
      while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
inline ll bits(ll x) {
      return !x ? x : bits(x - lowbit(x)) + 1;
}
/*****************************************************/

const int maxn = 35;

struct Node {
    int a, b, c;
    bool operator <(const Node &rhs) const {
        if (a == rhs.a && b == rhs.b) return c < rhs.c;
        if (a == rhs.a) return b > rhs.b;
        return a < rhs.a;
    }
    bool operator ==(const Node &rhs) const {
        return a == rhs.a && b == rhs.b && c == rhs.c;
    }
}node[maxn * 6];

int N;
int lis[maxn * 3];
int tree[(maxn * 6) << 2];
int dp[maxn * 6];

void PushUp(int v) {
    tree[v] = max(tree[slch], tree[srch]);
}

void update(int x, int val, int l, int r, int v) {
    if (l == r) tree[v] = max(tree[v], val);
    else {
        int m = (l + r) >> 1;
        if (x <= m) update(x, val, lson);
        else update(x, val, rson);
        PushUp(v);
    }
}

int query(int L, int R, int l, int r, int v) {
    if (L > R) return 0;
    if (L <= l && r <= R) return tree[v];
    int m = (l + r) >> 1;
    int ans = -1;
    if (L <= m) ans = max(ans, query(L, R, lson));
    if (R >  m) ans = max(ans, query(L, R, rson));
    return ans;
}

int main(int argc, char const *argv[]) {
    int kase = 1;
    while (~scanf("%d", &N) && N) {
        memset(tree, 0, sizeof(tree));
        int tot = 0, cnt = 0;
        for (int i = 0; i < N; i ++) {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            node[tot ++] = (Node){a, b, c};
            node[tot ++] = (Node){b, a, c};
            node[tot ++] = (Node){a, c, b};
            node[tot ++] = (Node){c, a, b};
            node[tot ++] = (Node){b, c, a};
            node[tot ++] = (Node){c, b, a};
            lis[cnt ++] = a;
            lis[cnt ++] = b;
            lis[cnt ++] = c;
        }

        sort(node, node + tot);
        sort(lis, lis + cnt);
        int len = unique(node, node + tot) - node;
        int num = unique(lis, lis + cnt) - lis;

        for (int i = 0; i < len; i ++) {
            int p = lower_bound(lis, lis + num, node[i].b) - lis;
            int d = query(0, p - 1, 0, num, 1);
            dp[i] = d + node[i].c;
            update(p, dp[i], 0, num, 1);
        }

        printf("Case %d: maximum height = %d\n", kase ++, *max_element(dp, dp + len));
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值