The Tower of Babylon(UVa 437)
题意:有n种立方体,每种都有无穷多个。选一些正方体摞成一根尽量高的柱子(可以选择任意一条边做高),使得每个立方体的底面长宽分别严格小于它下方的立方柱的底面长宽。
题解:可以套用DAG最长路算法,可以使用二元组来表示每个立方体的每一条边,如v[n][2]就可以用来表示第n个立方块的3个边。
DAG最长路算法:
int dp(int i,int j)
{
int &ans=dist[i][j];
if(ans>) return ans;///表示已经查找过此种状态
ans=;///根据题意赋相应的初值
int v[],v2[];
get_dimensions(v,i,j);///用v数组表示每个方块的长宽高
for(int a=;a<n;a++)
for(int b=;b<;b++){///对每种放置方法进行枚举
get_dimensions(v2,a,b);
if(v2[]<v[]&&v2[]<v[]) ans=max(ans,dp(a,b));///符合条件的方块,进行放置,查看是否最优,此处只是对下一个状态进行最优查找,并未将当前状态的高度计入(因为当前方案不一定可行)
}
ans+=blocks[i][j];///放置此种方块的方案可行,进行放置
return ans;
}
本题代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn=+;
int blocks[maxn][],dist[maxn][],n; void get_dimensions(int* v, int b, int dim) {
int idx = ;
for(int i=;i<;i++)
if(i != dim) v[idx++] = blocks[b][i];///找出每个方块对应的长,宽,高
} int dp(int i,int j)
{
int &ans=dist[i][j];
if(ans>) return ans;///表示已经查找过此种状态
ans=;
int v[],v2[];
get_dimensions(v,i,j);///用v数组表示每个方块的长宽高
for(int a=;a<n;a++)
for(int b=;b<;b++){///对每种放置方法进行枚举
get_dimensions(v2,a,b);
if(v2[]<v[]&&v2[]<v[]) ans=max(ans,dp(a,b));///符合条件的方块,进行放置,查看是否最优,此处只是对下一个状态进行最优查找,并未将当前状态的高度计入(因为当前方案不一定可行)
}
ans+=blocks[i][j];///放置此种方块的方案可行,进行放置
return ans;
} int main()
{
int kase=;
while(~scanf("%d",&n)&&n){
for(int i=;i<n;i++){
for(int j=;j<;j++) scanf("%d",&blocks[i][j]);
sort(blocks[i],blocks[i]+);
}
memset(dist,,sizeof(dist));
int ans=;
for(int i=;i<n;i++)
for(int j=;j<;j++){///枚举每个方块的每种摆放位置
ans=max(ans,dp(i,j));
}
printf("Case %d: maximum height = %d\n",++kase,ans);
}
return ;
}
The Tower of Babylon(UVa 437)的更多相关文章
- The Tower of Babylon UVA - 437 DAG上的动态规划
题目:题目链接 思路:每个方块可以用任意多次,但因为底面限制,每个方块每个放置方式选一个就够了,以x y为底 z 为高,以x z为底 y 为高,以y z为底 x为高,因为数据量很小,完全可以把每一种当 ...
- UVA 437 十九 The Tower of Babylon
The Tower of Babylon Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Subm ...
- UVa 437 The Tower of Babylon(经典动态规划)
传送门 Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details ...
- UVa 437 The Tower of Babylon
Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of ...
- UVa 437 The Tower of Babylon(DP 最长条件子序列)
题意 给你n种长方体 每种都有无穷个 当一个长方体的长和宽都小于还有一个时 这个长方体能够放在还有一个上面 要求输出这样累积起来的最大高度 由于每一个长方体都有3种放法 比較不好控制 ...
- uva The Tower of Babylon[LIS][dp]
转自:https://mp.weixin.qq.com/s/oZVj8lxJH6ZqL4sGCXuxMw The Tower of Babylon(巴比伦塔) Perhaps you have hea ...
- UVA The Tower of Babylon
The Tower of Babylon Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many det ...
- POJ2241——The Tower of Babylon
The Tower of Babylon Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2207 Accepted: 1 ...
- UVA437-The Tower of Babylon(动态规划基础)
Problem UVA437-The Tower of Babylon Accept: 3648 Submit: 12532Time Limit: 3000 mSec Problem Descrip ...
随机推荐
- #分组背包 Educational Codeforces Round 39 (Rated for Div. 2) D. Timetable
2018-03-11 http://codeforces.com/contest/946/problem/D D. Timetable time limit per test 2 seconds me ...
- RPM 包的构建 - 实例
打包目录 rpm 打包目录有一些严格的层次上的要求. rpm 的版本 <=4.4.x,rpmbuid 工具其默认的工作路径是 /usr/src/redhat.因为权限的问题,普通用户不能制作 r ...
- Learning-MySQL【2】:MySQL存储引擎及数据库的操作管理
一.存储引擎 存储引擎实际上就是如何存储数据.如何为存储的数据建立索引和如何更新.查询数据.存储引擎也可以称为表类型. MySQL提供了插件式(pluggable)的存储引擎,存储引擎是基于表的.同一 ...
- HDU 1043 Eight(八数码)
HDU 1043 Eight(八数码) 00 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Descr ...
- Springboot静态文件不更新的解决办法,以及Springboot实现热部署
Springboot静态文件不更新的解决办法,以及Springboot实现热部署 原文链接:https://www.cnblogs.com/blog5277/p/9271882.html 原文作者:博 ...
- 【四】jquery之文档处理习题(内部处理、外部处理)[移动节点]
参考资料:http://jquery.cuishifeng.cn/index.html 代码: <!DOCTYPE html> <html> <head> < ...
- 一个SQL查询出每门课程的成绩都大于80的学生姓名
name kecheng fenshu 张三 语文 81 张三 数学 75 李四 语文 76 李四 数学 90 王五 ...
- SAP跳过权限检查/前提是有debug权限
SE37,AUTH_CHECK_TCODE加断点: 执行事务代码,进入断点,修改sy-subrc = 0,执行,就可以跳过权限检查. 只给了事务代码权限,没有操作权限: CL_SUID_TOOLS== ...
- scrapy---反爬虫
反爬虫措施1)动态修改User-Agent2)动态修改ip3)延迟DOWNLOAD_DELAY = 0.5 1)在middleware中新建一个类,从fake_useragent中导入UserAgen ...
- Spring Boot + Spring Cloud 实现权限管理系统 权限控制(Shiro 注解)
技术背景 当前,我们基于导航菜单的显示和操作按钮的禁用状态,实现了页面可见性和操作可用性的权限验证,或者叫访问控制.但这仅限于页面的显示和操作,我们的后台接口还是没有进行权限的验证,只要知道了后台的接 ...