[HDU 1069 Monkey and Banana]DP
2015-08-31 17:27
357 查看
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1069
Total Submission(s): 9902 Accepted Submission(s): 5152
[align=left]Problem Description[/align]
A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a banana at the roof of a building, and at the mean time, provide the monkey with some blocks. If the monkey is clever enough, it shall
be able to reach the banana by placing one block on the top another to build a tower and climb up to get its favorite food.
The researchers have n types of blocks, and an unlimited supply of blocks of each type. Each type-i block was a rectangular solid with linear dimensions (xi, yi, zi). A block could be reoriented so that any two of its three dimensions determined the dimensions
of the base and the other dimension was the height.
They want to make sure that the tallest tower possible by stacking blocks can reach the roof. The problem is that, in building a tower, one block could only be placed on top of another block as long as the two base dimensions of the upper block were both strictly
smaller than the corresponding base dimensions of the lower block because there has to be some space for the monkey to step on. This meant, for example, that blocks oriented to have equal-sized bases couldn't be stacked.
Your job is to write a program that determines the height of the tallest tower the monkey can build with a given set of blocks.
[align=left]Input[/align]
The input file will contain one or more test cases. The first line of each test case contains an integer n,
representing the number of different blocks in the following data set. The maximum value for n is 30.
Each of the next n lines contains three integers representing the values xi, yi and zi.
Input is terminated by a value of zero (0) for n.
[align=left]Output[/align]
For each test case, print one line containing the case number (they are numbered sequentially starting from 1) and the height of the tallest possible tower in the format "Case case: maximum height = height".
[align=left]Sample Input[/align]
1
10 20 30
2
6 8 10
5 5 5
7
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
5
31 41 59
26 53 58
97 93 23
84 62 64
33 83 27
0
[align=left]Sample Output[/align]
Case 1: maximum height = 40
Case 2: maximum height = 21
Case 3: maximum height = 28
Case 4: maximum height = 342
[align=left]Source[/align]
University of Ulm Local Contest 1996
其实蛮简单的,思考一下就可以了。
题目意思是给你n个长方体的长宽高,长方体能用各种姿态摆,也就是长能做宽宽能做高高能做长,随意变化。。。
每一种长方体的个数无限,稍微想想就知道一个长方体能摆3个姿势,长宽高能定6种。
然后让你把这些长方体像金字塔一样堆叠起来,上面的长方体要小于下面的长方体,宽和长都要小于下面长方体的宽和长,让你求出能摆出的最高高度。
其实就是最大子序列和,写出转移方程的条件就行。
我的做法是,每给你一个长方体的数据,就存6种长方体,然后按照长或者宽从小到大排序,用dp记录用当前长方体做低能堆叠出的最高高度。
代码如下
Monkey and Banana
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9902 Accepted Submission(s): 5152
[align=left]Problem Description[/align]
A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a banana at the roof of a building, and at the mean time, provide the monkey with some blocks. If the monkey is clever enough, it shall
be able to reach the banana by placing one block on the top another to build a tower and climb up to get its favorite food.
The researchers have n types of blocks, and an unlimited supply of blocks of each type. Each type-i block was a rectangular solid with linear dimensions (xi, yi, zi). A block could be reoriented so that any two of its three dimensions determined the dimensions
of the base and the other dimension was the height.
They want to make sure that the tallest tower possible by stacking blocks can reach the roof. The problem is that, in building a tower, one block could only be placed on top of another block as long as the two base dimensions of the upper block were both strictly
smaller than the corresponding base dimensions of the lower block because there has to be some space for the monkey to step on. This meant, for example, that blocks oriented to have equal-sized bases couldn't be stacked.
Your job is to write a program that determines the height of the tallest tower the monkey can build with a given set of blocks.
[align=left]Input[/align]
The input file will contain one or more test cases. The first line of each test case contains an integer n,
representing the number of different blocks in the following data set. The maximum value for n is 30.
Each of the next n lines contains three integers representing the values xi, yi and zi.
Input is terminated by a value of zero (0) for n.
[align=left]Output[/align]
For each test case, print one line containing the case number (they are numbered sequentially starting from 1) and the height of the tallest possible tower in the format "Case case: maximum height = height".
[align=left]Sample Input[/align]
1
10 20 30
2
6 8 10
5 5 5
7
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
5
31 41 59
26 53 58
97 93 23
84 62 64
33 83 27
0
[align=left]Sample Output[/align]
Case 1: maximum height = 40
Case 2: maximum height = 21
Case 3: maximum height = 28
Case 4: maximum height = 342
[align=left]Source[/align]
University of Ulm Local Contest 1996
其实蛮简单的,思考一下就可以了。
题目意思是给你n个长方体的长宽高,长方体能用各种姿态摆,也就是长能做宽宽能做高高能做长,随意变化。。。
每一种长方体的个数无限,稍微想想就知道一个长方体能摆3个姿势,长宽高能定6种。
然后让你把这些长方体像金字塔一样堆叠起来,上面的长方体要小于下面的长方体,宽和长都要小于下面长方体的宽和长,让你求出能摆出的最高高度。
其实就是最大子序列和,写出转移方程的条件就行。
我的做法是,每给你一个长方体的数据,就存6种长方体,然后按照长或者宽从小到大排序,用dp记录用当前长方体做低能堆叠出的最高高度。
代码如下
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; typedef struct node //开个结构体数组,用来记录dp值和长宽高 { int x, y, z; int dp; }; int cmp(node a, node b) { return a.x < b.x; } node nod[185]; int n; int main() { // freopen("input.txt", "r", stdin); int s[3]; int a, b, c; int cnt; int tt = 1; while (scanf("%d", &n) != EOF&&n) { cnt = 1; for (int i = 1; i <= n; i++) { scanf("%d%d%d", &s[0], &s[1], &s[2]); sort(s, s + 3); nod[cnt].x = s[0]; //冗长的储存。。。 nod[cnt].y = s[1]; nod[cnt].z = s[2]; nod[cnt++].dp = s[2]; nod[cnt].x = s[1]; nod[cnt].y = s[0]; nod[cnt].z = s[2]; nod[cnt++].dp = s[2]; nod[cnt].x = s[1]; nod[cnt].y = s[2]; nod[cnt].z = s[0]; nod[cnt++].dp = s[0]; nod[cnt].x = s[2]; nod[cnt].y = s[1]; nod[cnt].z = s[0]; nod[cnt++].dp = s[0]; nod[cnt].x = s[2]; nod[cnt].y = s[0]; nod[cnt].z = s[1]; nod[cnt++].dp = s[1]; nod[cnt].x = s[0]; nod[cnt].y = s[2]; nod[cnt].z = s[1]; nod[cnt++].dp = s[1]; } sort(nod, nod + cnt, cmp); //按照宽或者长从小到大排序 for (int i = 1; i < cnt; i++) { for (int j = 1; j < i; j++) { if (nod[i].x > nod[j].x && nod[i].y > nod[j].y) //长/宽都要大于上面的长方体的长/宽 nod[i].dp = max(nod[i].dp, nod[j].dp + nod[i].z); } } int mx = -1; for (int i = 1; i < cnt; i++) //取最大值 mx = max(mx, nod[i].dp); printf("Case %d: maximum height = %d\n", tt++, mx); } return 0; }
相关文章推荐
- php curl的几个例子【转】
- 音乐播放器-manifest分析
- cocos2dx 屏幕触摸
- HDU1018 - Big Number (找规律)
- 比特币地址的生成算法是怎样的?
- JSON数据解析——jsoncpp源码编辑
- cocos2dx 屏幕触摸
- How to write RESTful web services using spring 3 mvc
- Android Studio 编译 freso问题
- 一些知名 APP 使用的第三方库
- Unity3d 学习笔记
- 【营销】包子和星座
- cocos2dx 屏幕触摸
- SAE部署CI框架代码
- ios多线程 GCD使用
- bzoj-3672 购票
- 读书记录(持续更新...)
- 模拟Post请求
- 【EasyUI】Combobox的联动和onChange/onSelect事件绑定
- Goods:动态加载所有的分类项到left.jsp