kuangbin专题十二 HDU1069 Monkey and Banana (dp)

通过排列组合不同尺寸的长方体块来搭建塔以达到特定高度。该问题要求长方体块在堆叠时,上面的块在两个维度上严格小于下面的块。采用动态规划方法解决最大高度塔的构建。

Monkey and Banana

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20094    Accepted Submission(s): 10725


Problem Description
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.
 

 

Input
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.
 

 

Output
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".
 

 

Sample Input
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
 

 

Sample Output
Case 1: maximum height = 40 Case 2: maximum height = 21 Case 3: maximum height = 28 Case 4: maximum height = 342
 

 

题目大意:给出n种长方体的x,y,z(任意个),然后堆起来(要求严格小于自己下面的长方体),求能达到的最大高度。

经典的矩形嵌套:把每种长方体的6种方法都存储起来,然后排序,然后再像上升子序列dp一样。见注释

 

 

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <math.h>
 4 #include <string.h>
 5 #include <stdlib.h>
 6 #include <string>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <queue>
11 #include <algorithm>
12 #include <sstream>
13 #include <stack>
14 using namespace std;
15 #define mem(a,b) memset((a),(b),sizeof(a))
16 #define mp make_pair
17 #define pb push_back
18 #define fi first
19 #define se second
20 #define sz(x) (int)x.size()
21 #define all(x) x.begin(),x.end()
22 #define forn(i, x, n) for(int i = (x); i < n; i++)
23 #define nfor(i, x, n) for(int i = n-1; i >= x; i--)
24 typedef long long ll;
25 const int inf = 0x3f3f3f3f;
26 const ll INF =0x3f3f3f3f3f3f3f3f;
27 const double pi = acos(-1.0);
28 const double eps = 1e-5;
29 const ll mod = 1e9+7;
30 
31 struct node{
32     int l, w, h;
33 }stu[200];
34 int dp[200];//dp[i] 表示 i能达到的最高高度 
35 
36 bool cmp(node a, node b) {//先x 后 y 
37     if(a.l == b.l)
38         return a.w < b.w;
39     return a.l < b.l;
40 }
41 
42 int main() {
43     int n, x, y, z, cur;
44     int icase = 1;
45     while(~scanf("%d", &n),n) {
46         cur = 0;
47         while(n--) {
48             scanf("%d%d%d", &x, &y, &z);//存储6种状态 
49             stu[cur].l = x; stu[cur].w = y; stu[cur++].h = z; 
50             stu[cur].l = x; stu[cur].w = z; stu[cur++].h = y;
51             stu[cur].l = y; stu[cur].w = z; stu[cur++].h = x;
52             stu[cur].l = y; stu[cur].w = x; stu[cur++].h = z;
53             stu[cur].l = z; stu[cur].w = x; stu[cur++].h = y;
54             stu[cur].l = z; stu[cur].w = y; stu[cur++].h = x;
55         }
56         sort(stu, stu+cur, cmp);//排序 
57         mem(dp, 0);
58         int maxx = -inf;
59         forn(i, 0, cur) {
60             dp[i] = stu[i].h;//初始化 
61             forn(j, 0, i) {//找到使自己最高的 
62                 if(stu[j].l < stu[i].l && stu[j].w < stu[i].w) {
63                     dp[i] = max(dp[j] + stu[i].h, dp[i]);
64                 }
65             }
66             maxx = max(maxx, dp[i]);//更新最高高度 
67         }
68         maxx = max(0, maxx);
69         printf("Case %d: maximum height = %d\n", icase++, maxx);
70     }
71 }

 

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索局部开发之间实现平衡。文章详细解析了算法的初始化、勘探开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOAMOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值