poj 2241 叠方块 基本动态规划
来源:互联网 发布:纯html商城源码 编辑:程序博客网 时间:2024/05/29 18:35
题意:给定n种block,每种block有无限多个,每个block有x, y, z三个属性
要求叠起来,使得在满足下面的长和宽严格大于上面的,情况下,高度最高
解法:
d[i] 表示 以第i个物品为顶能达到的最大高度
转移方程 d[i] = max{ d[i] , d[x] + height(x) 其中x要满足题目要求约束,遍取 0 - n - 1}
max(d[x])就是答案
#include <iostream>#include <vector>#include <map>#include <list>#include <set>#include <deque>#include <stack>#include <queue>#include <algorithm>#include <cmath>#include <cctype>#include <cstdio>#include <iomanip>#include <cmath>#include <cstdio>#include <iostream>#include <string>#include <sstream>#include <cstring>#include <queue>using namespace std;///宏定义const int INF = 990000000;const int maxn = 500 ;const int MAXN = maxn;///全局变量 和 函数//int T;int max(int a, int b){return a > b ? a : b;}int n;struct BLOCK{int x, y, z;bool operator < (const BLOCK& t) const{return (x * y) > (t.x * t.y);}};BLOCK blocks[maxn * 5];int d[maxn * 5];bool vis[maxn * 5];int cnt;int dp(int k){if (vis[k])return d[k];vis[k] = true;int i, j;int nowy, nowx, mh, maxheight;//以x, y为底mh = blocks[k].z;nowy = blocks[k].y;nowx = blocks[k].x;maxheight = mh;for (i = cnt - 1; i >= 0; i--){if ( (nowy < blocks[i].y && nowx < blocks[i].x) || (nowx < blocks[i].y && nowy < blocks[i].x) ) //写错了好几次{maxheight = max(maxheight, dp(i) + mh);}}return d[k] = maxheight;}int main(){///变量定义int i, j;int cases = 1;while(1){memset(vis, false, sizeof(vis));scanf("%d", &n);if (n == 0)break;cnt = 0;for (i = 0; i < n; i++){int x, y, z;scanf("%d %d %d", &x, &y, &z);blocks[cnt].x = x;blocks[cnt].y = y;blocks[cnt].z = z;cnt++;blocks[cnt].x = x;blocks[cnt].y = z;blocks[cnt].z = y;cnt++;blocks[cnt].x = y;blocks[cnt].y = z;blocks[cnt].z = x;cnt++;}//sort(blocks, blocks + cnt);//vis[0] = true;//d[0] = blocks[0].z;int ans = -1;for (i = cnt - 1; i >= 0; i--){ans = max(ans, dp(i));}printf("Case %d: maximum height = %d\n", cases++, ans);}///结束return 0;}
在此基础上上更改一下细节处理,对于block的面积从大到小进行排序
那么d[i] = max{ d[i], d[k] + height[k] k < i }
速度会更快
#include <iostream>#include <vector>#include <map>#include <list>#include <set>#include <deque>#include <stack>#include <queue>#include <algorithm>#include <cmath>#include <cctype>#include <cstdio>#include <iomanip>#include <cmath>#include <cstdio>#include <iostream>#include <string>#include <sstream>#include <cstring>#include <queue>using namespace std;///宏定义const int INF = 990000000;const int maxn = 500 ;const int MAXN = maxn;///全局变量 和 函数//int T;int max(int a, int b){return a > b ? a : b;}int n;struct BLOCK{int x, y, z;bool operator < (const BLOCK& t) const{return (x * y) > (t.x * t.y);}};BLOCK blocks[maxn * 5];int d[maxn * 5];bool vis[maxn * 5];int cnt;int dp(int k){if (vis[k])return d[k];vis[k] = true;int i, j;int nowy, nowx, mh, maxheight;//以x, y为底mh = blocks[k].z;nowy = blocks[k].y;nowx = blocks[k].x;maxheight = mh;for (i = k - 1; i >= 0; i--){if ( (nowy < blocks[i].y && nowx < blocks[i].x) || (nowx < blocks[i].y && nowy < blocks[i].x) ) //写错了好几次{maxheight = max(maxheight, dp(i) + mh);}}return d[k] = maxheight;}int main(){///变量定义int i, j;int cases = 1;while(1){memset(vis, false, sizeof(vis));scanf("%d", &n);if (n == 0)break;cnt = 0;for (i = 0; i < n; i++){int x, y, z;scanf("%d %d %d", &x, &y, &z);blocks[cnt].x = x;blocks[cnt].y = y;blocks[cnt].z = z;cnt++;blocks[cnt].x = x;blocks[cnt].y = z;blocks[cnt].z = y;cnt++;blocks[cnt].x = y;blocks[cnt].y = z;blocks[cnt].z = x;cnt++;}sort(blocks, blocks + cnt); //先进行排序vis[0] = true; //面积最大的作为顶的最大高度,记忆化搜索d[0] = blocks[0].z;int ans = -1;for (i = cnt - 1; i >= 0; i--){ans = max(ans, dp(i));}printf("Case %d: maximum height = %d\n", cases++, ans);}///结束return 0;}
- poj 2241 叠方块 基本动态规划
- 动态规划基本思想
- 动态规划 基本解法
- 动态规划基本问题
- 基本动态规划讲解
- 动态规划基本问题
- 动态规划-基本思想
- 动态规划基本问题
- poj 2411 动态规划
- Poj 1157(动态规划)
- POJ 1651动态规划
- Poj 1221 动态规划
- POJ 3186 动态规划
- [poj]动态规划2479
- [poj]动态规划1015
- [poj] 动态规划 1141
- POJ 1088 动态规划
- poj 1636 动态规划
- 虚拟机中的XP系统忘记密码解决办法
- Java内存模型
- 常用正则表达式
- 创建型设计模式——单例模式
- Getting thread count for proc in linux
- poj 2241 叠方块 基本动态规划
- ServletContextListener使用详解
- Java序列化【草案一】
- 分支预测器(Branch Predictor) 汇总介绍
- rails内置特性(样式设计积累)
- 致诸位新程序员:来自Chuck Jazdzewski慈父般的忠告
- Flex Thread Libray - Summary
- SMC技术
- 双击明细行数据查看具体内容