【题目链接】:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=378

【题意】



给你n个方形;

由3个属性,长宽高决定;

你可以任意摆放这个方形(即把哪一面朝下方);

然后每种方形都有无限个;

一个方形能够摆在另外一个方形上面,当且仅当这个方形的长和宽都严格大于另外一个方形的长和宽(即changi>changj && kuani>kuanj);

问你这n个方形最多能叠多高;

【题解】



把每个方形的3种摆法都取出来;

(即取3个属性中的某一个属性出来作为高,另外两个作为宽和长);

然后如果某一个方形x可以放到另外一个方形y的上面;

则连一条有向边x指向y;

然后问题就能转化为一个有向无环图上的最长路了;

起点不一定

也即一条最长链

写个记忆化搜索就好;

f[x]=max(f[x],f[y]+h[x]),(x,y)∈E,h[x]为x的高



【Number Of WA】



1



【反思】



忘记初始化+忘记输出Case



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("D:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 30; struct abc{
LL c,k,g;
}; int n,b[4],nn;
LL dp[N*3+100];
abc a[N*3+100];
vector <int> G[N*3+100]; LL dfs(int x){
if (dp[x]!=-1) return dp[x];
LL &ans = dp[x];
ans = a[x].g;
int len = G[x].size();
rep1(i,0,len-1){
int y = G[x][i];
ans = max(ans,dfs(y) + a[x].g);
}
return dp[x];
} int main()
{
//Open();
int kk = 0;
while (~scanf("%d",&n) && n){
kk++;
ms(dp,-1);
nn = 0;
rep1(i,1,N*3) G[i].clear();
rep1(i,1,n){
rep1(j,1,3)
scanf("%d",&b[j]);
sort(b+1,b+1+3);
rep1(j,1,3){
nn++;
rep2(k,3,1)
if (k!=j){
a[nn].c = b[k];
break;
}
rep1(k,1,3)
if (k!=j){
a[nn].k = b[k];
break;
}
a[nn].g = b[j];
}
}
n = nn;
rep1(i,1,n)
rep1(j,1,n)
if (a[i].c > a[j].c && a[i].k > a[j].k)
G[i].pb(j);
LL d = 0;
rep1(i,1,n)
d = max(d,dfs(i));
printf("Case %d: maximum height = ",kk);
printf("%lld\n",d);
}
return 0;
}

【UVA 437】The Tower of Babylon(记忆化搜索写法)的更多相关文章

  1. poj1179 区间dp(记忆化搜索写法)有巨坑!

    http://poj.org/problem?id=1179 Description Polygon is a game for one player that starts on a polygon ...

  2. UVA 10003 Cutting Sticks 区间DP+记忆化搜索

    UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...

  3. UVA 10400 Game Show Math (dfs + 记忆化搜索)

    Problem H Game Show Math Input: standard input Output: standard output Time Limit: 15 seconds A game ...

  4. UVA 11884 A Shooting Game(记忆化搜索)

    A and B are playing a shooting game on a battlefield consisting of square-shaped unit blocks. The bl ...

  5. hdu 2089 记忆化搜索写法(数位dp)

    /* 记忆化搜索,第二维判断是否是6 */ #include<stdio.h> #include<string.h> #define N 9 int dp[N][2],digi ...

  6. UVa 437 The Tower of Babylon

    Description   Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of ...

  7. UVa 437 The Tower of Babylon(经典动态规划)

    传送门 Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details ...

  8. UVa 437 The Tower of Babylon(DP 最长条件子序列)

     题意  给你n种长方体  每种都有无穷个  当一个长方体的长和宽都小于还有一个时  这个长方体能够放在还有一个上面  要求输出这样累积起来的最大高度 由于每一个长方体都有3种放法  比較不好控制 ...

  9. UVA - 437 The Tower of Babylon(dp-最长递增子序列)

    每一个长方形都有六种放置形态,其实可以是三种,但是判断有点麻烦直接用六种了,然后按照底面积给这些形态排序,排序后就完全变成了LIS的问题.代码如下: #include<iostream> ...

随机推荐

  1. JS 判断中英文字符长度

    function strlen(str) {        var len = 0;        for (var i = 0; i < str.length; i++) {          ...

  2. js正则学习小计

    //元字符 {} () ^ $ . ? + //预定义字符 \d \D \w \W \s \S //量词 {n,m} {n} {n,} + ? * //贪婪和惰性 //反向引用 //分组 //候选 / ...

  3. ActiveMQ学习笔记(22)----ActiveMQ的优化和使用建议

    1. 什么时候使用ActiveMQ 1. 异步通信 2. 一对多通信 3. 做个系统的集成,同构,异构 4. 作为RPC的替代 5. 多个应用相互解耦 6. 作为事件驱动架构的幕后支撑 7. 为了提高 ...

  4. wordpress 后台登录增加访问效验,优化退出效果

    之前记录了 wordpress 后台登录增加访问效验, 记录了增加后台访问地址被直接访问的困难性的修改步骤. 还有一个地方需要补充一下,就是退出. 退出的时候不做调整会直接跳到首页,这样体验很不好. ...

  5. 批量删除harbor中的镜像

    一 说明 这个是我第一篇博客,所以我想放上原创的东西,尽管我一直都很担心自己写得太low,但是总要学会尝试,学会改变自己,相信自己.在写这个脚本时,由于我接触LInux不是很多,能力有限,仅仅是为了让 ...

  6. openssh 升级到7.5p1

    1. 参照: http://www.cnblogs.com/xiegj/p/5669800.html http://blog.csdn.net/u011080082/article/details/5 ...

  7. 即将到来的Autodesk 主要产品2015版 产品和API新功能在线培训(免费)

    一年一度的Autodesk主要产品和API在线培训课程在5月份即将開始.我们呈献给大家5个课程. 1. Revit 2015 产品新功能及API 概览 2. Vault 2015产品新功能及API 概 ...

  8. django 笔记14 中间件

    用户请求->中间件->urls->views->返回字符串->中间件->用户浏览器 settings MIDDLEWARE里面都是中间件 有的地方叫管道 请求来的时 ...

  9. Dictionary as a set of counters

    Suppose you are given a string and you want to count how many times each letters appears. There are ...

  10. Swift学习笔记(3):基本运算符

    目录: 运算符 元组比较 空和运算符 区间运算符 运算符 +, -, *, /, %, =, +=, -=, *=, /= 算术运算符 >, <, ==, >=, <=, != ...