ZOJ  1093   Monkey and Banana  (LIS)解题报告

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/B

题目:

Description

一组研究人员正在设计一项实验,以测试猴子的智商。他们将挂香蕉在建筑物的屋顶,同时,提供一些砖块给这些猴子。如果猴子足够聪明,它应当能够通过合理的放置一些砖块建立一个塔,并爬上去吃他们最喜欢的香蕉。
 
研究人员有n种类型的砖块,每种类型的砖块都有无限个。第i块砖块的长宽高分别用xi,yi,zi来表示。 同时,由于砖块是可以旋转的,每个砖块的3条边可以组成6种不同的长宽高。
 
在构建塔时,当且仅当A砖块的长和宽都分别小于B砖块的长和宽时,A砖块才能放到B砖块的上面,因为必须留有一些空间让猴子来踩。
 
你的任务是编写一个程序,计算猴子们最高可以堆出的砖块们的高度。

Input

输入文件包含多组测试数据。
  每个测试用例的第一行包含一个整数n,代表不同种类的砖块数目。n<=30.
接下来n行,每行3个数,分别表示砖块的长宽高。
当n=  0的时候,无需输出任何答案,测试结束。

Output

对于每组测试数据,输出最大高度。格式:Case  第几组数据: maximum height =   最大高度

Sample Input

1
10 20 30 
6 8 10 
5 5 5 
1 1 1 
2 2 2 
3 3 3 
4 4 4 
5 5 5 
6 6 6 
7 7 7 
31 41 59 
26 53 58 
97 93 23 
84 62 64 
33 83 27 

Sample Output

Case 1: maximum height = 40 
Case 2: maximum height = 21   
Case 3: maximum height = 28   
Case 4: maximum height = 342
 
题目大意:
有N块已知长、宽、高的砖,每种砖的数量不限,将砖摞起来,使在上面的砖的长宽都小于下面的(不能等于),求能堆成的最大高度。
 
分析:
LIS问题。动态规划dp。
由于砖块可以旋转,那么给一块砖就相当于给了三块砖(即N<=30*3)。
求从编号 i 出发能堆出的最大高度(不包括砖块),dp(i) = max(dp(j) + j的高度; j是能放在i上面的砖块编号),将开始节点看成地板最大高度为无穷大,为dp(0);
 
代码:
 #include<cstdio>
#include<iostream>
#include<climits> //用INT_MAX
using namespace std;
const int M=; int box[M][];
int height[M];
int number; //互换长宽高,转换砖块的三种类型
void change(int index,int a,int b,int c)
{
box[index][]=a;
box[index][]=b;
box[index][]=c;
} int dp(int j)
{
int& ans=height[j];
if(ans!=-) //搜索过的直接返回
return ans;
for(int i=;i<=number;i++)//搜索每一层
{
if(i!=j)
{
if((box[i][]<box[j][]&&box[i][]<box[j][])||(box[i][]<box[j][]&&box[i][]<box[j][]))
{
int temp=dp(i)+box[i][];
if(temp>ans)
ans=temp;
}
}
}
if(ans==-)//最后一层未更新
ans=;
return ans;
} int main()
{
int n;
int m=;
while(scanf("%d",&n)&&n)
{
int a,b,c;
number=;
box[][]=box[][]=box[][]=INT_MAX;//地板看成无穷大砖块
for(int i=;i<=n;i++)
{
scanf("%d%d%d",&a,&b,&c);
change(++number,a,b,c);
change(++number,b,c,a);
change(++number,c,a,b);
}
for(int i=;i<=number;i++)
{
height[i]=-;
}
printf("Case %d: maximum height = %d\n",m++,dp());
}
return ;
}

每次都想完全自己写,可是自己写的都有很多错误,最后有看了别人的博客。

 

ZOJ 1093 Monkey and Banana (LIS)解题报告的更多相关文章

  1. HDU 1069 Monkey and Banana / ZOJ 1093 Monkey and Banana (最长路径)

    HDU 1069 Monkey and Banana / ZOJ 1093 Monkey and Banana (最长路径) Description A group of researchers ar ...

  2. 随手练——ZOJ 1093 Monkey and Banana(动态规划)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=93 一堆科学家研究猩猩的智商,给他M种长方体,每种N个. 然后,将一个 ...

  3. 「SDOI2014」Lis 解题报告

    「SDOI2014」Lis 题目描述 给定序列 \(A\),序列中的每一项 \(A_i\) 有删除代价 \(B_i\) 和附加属性 \(C_i\). 请删除若干项,使得 \(A\) 的最长上升子序列长 ...

  4. ZOJ Monthly, January 2018 训练部分解题报告

    A是水题,此处略去题解 B - PreSuffix ZOJ - 3995 (fail树+LCA) 给定多个字符串,每次询问查询两个字符串的一个后缀,该后缀必须是所有字符串中某个字符串的前缀,问该后缀最 ...

  5. zoj 2315 New Year Bonus Grant 解题报告

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1315 题目意思:Bill Hates 是公司的老总,她管辖着很多程序 ...

  6. hdu1069 Monkey and Banana LIS

    #include<cstdio> #include<iostream> #include<algorithm> #include<queue> #inc ...

  7. HDU 1069 Monkey and Banana(二维偏序LIS的应用)

    ---恢复内容开始--- Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  8. HDU 1069 Monkey and Banana(转换成LIS,做法很值得学习)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 Monkey and Banana Time Limit: 2000/1000 MS (Java ...

  9. zoj 2313 Chinese Girls' Amusement 解题报告

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1313 题目意思:有 N 个人(编号依次为1~N)围成一个圆圈,要求求 ...

随机推荐

  1. python读取word表格内容(1)

    1.首页介绍下word表格内容,实例如下: 每两个表格后面是一个合并的单元格

  2. Clear all username or password for login.

    Open cmd.exe In command, type in: control keymgr.dll. This is go to watch the password which you rem ...

  3. github--新手使用错误分析

    先上去github 或者任意托管的网站.注册账号,新建仓库, 在本地运行Xcode 新建工程,新建工程的时候 勾上本地 的仓库,然后 在本地的项目根目录执行下边的命令: git remote add ...

  4. 生成输出url

    继续使用前面的例子11-3URLTestDemo,修改Global.asax中的RegisterRoutes方法如下: public static void RegisterRoutes(RouteC ...

  5. linux下mysql出现Access denied for user 'root'@'localhost' (using password: YES)解决方法

    # /etc/init.d/mysql stop # mysqld_safe --user=mysql --skip-grant-tables --skip-networking & # my ...

  6. 时尚B2B方兴未艾-Maker’s Row 获100万美元种子投资 |华丽志

    时尚B2B方兴未艾-Maker's Row 获100万美元种子投资 |华丽志 华丽志 » 网internet, 时尚B2B方兴未艾-Maker's Row 获100万美元种子投资 由 luxeco 发 ...

  7. iOS 中多线程的简单使用

    iOS中常用的多线程操作有( NSThread, NSOperation GCD ) 为了能更直观的展现多线程操作在SB中做如下的界面布局: 当点击下载的时候从网络上下载图片: - (void)loa ...

  8. 日志记录Filter

    Filter也可以日志记录,在request 之前后, 该filter 使用Apache 日只记录工具,记录客户IP ,访问URI 以及消耗时间. LogFilter.java package com ...

  9. Maven+SpringMVC+MyBatis 上传图片

    上传文件我一直都觉得很难,好吧,所有涉及文件操作的我都觉得不容易.然后今天尝试了从网页上传图片保存到服务器.这个例子的前提是搭建好了服务器端框架:Maven+Spring MVC+MyBatis.当然 ...

  10. sqlserver 只有函数和扩展存储过程才能从函数内部执行

    一个SQLServer的自定义函数中调用一个自定义的存储过程,执行此函数后发出如下提示:“只有函数和扩展存储过程才能从函数内部执行". 原因:函数只能使用简单的sql语句,逻辑控制语句,复杂 ...