2014-03-20 04:15

题目:你有n个盒子,用这n个盒子堆成一个塔,要求下面的盒子必须在长宽高上都严格大于上面的。如果你不能旋转盒子变换长宽高,这座塔最高能堆多高?

解法:首先将n个盒子按照长宽高顺序排好序,然后动态规划,我写了个O(n^2)时间复杂度的代码。

代码:

 // 9.10 A stack of n boxes is form a tower. where every stack must be strictly larger than the one right above it.
// The boxes cannot be rotated.
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std; struct Box {
// width
int w;
// height
int h;
// depth
int d;
Box(int _w = , int _h = , int _d = ): w(_w), h(_h), d(_d) {}; bool operator < (const Box &other) {
if (w != other.w) {
return w < other.w;
} else if (h != other.h) {
return h < other.h;
} else {
return d < other.d;
}
};
}; int main()
{
int n, i, j;
Box box;
vector<Box> v;
vector<int> dp;
int result; while (scanf("%d", &n) == && n > ) {
v.resize(n);
for (i = ; i < n; ++i) {
scanf("%d%d%d", &v[i].w, &v[i].h, &v[i].d);
}
sort(v.begin(), v.end());
dp.resize(n);
for (i = ; i < n; ++i) {
dp[i] = v[i].h;
}
for (i = ; i < n; ++i) {
for (j = ; j < i; ++j) {
if (v[i].w > v[j].w && v[i].h > v[j].h && v[i].d > v[j].d) {
dp[i] = v[i].h + dp[j] > dp[i] ? v[i].h + dp[j] : dp[i];
}
}
}
result = dp[];
for (i = ; i < n; ++i) {
result = dp[i] > result ? dp[i] : result;
}
v.clear();
dp.clear();
printf("%d\n", result);
} return ;
}

《Cracking the Coding Interview》——第9章:递归和动态规划——题目10的更多相关文章

  1. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  2. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  5. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  6. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  7. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  8. 《Cracking the Coding Interview》——第9章:递归和动态规划——题目11

    2014-03-21 20:20 题目:给定一个只包含‘0’.‘1’.‘|’.‘&’.‘^’的布尔表达式,和一个期望的结果(0或者1).如果允许你用自由地给这个表达式加括号来控制运算的顺序,问 ...

  9. 《Cracking the Coding Interview》——第9章:递归和动态规划——题目9

    2014-03-20 04:08 题目:八皇后问题. 解法:DFS解决. 代码: // 9.9 Eight-Queen Problem, need I say more? #include <c ...

随机推荐

  1. April 9 2017 Week 15 Sunday

    In the evening one may praise the day. 入夜方能赞美白昼. I think that could be understand in different ways, ...

  2. OpenGL学习 Our First OpenGL Program

    This shows you how to create the main window with the book’s application framework and how to render ...

  3. CToolTipCtrl使用详细解说

    很多的界面设计都需要有Tip提示,下面描述一下Tip的简单使用方法: 1. 首先要New一个CToolTipCtrl的对象m_pContentTip 2. 调用CToolTipCtrl的create函 ...

  4. IOS 设置颜色的的详情

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...

  5. Pascal之Hello World

    Pascal入门篇. 平台:Windows 7 ultimate x64 工具:Free Pascal 下载安装,界面如下: 右键属性,选择“437(OEM-美国)”,重新打开程序,乱码消失.     ...

  6. 转:spring mvc返回json数据格式

    转:http://www.cnblogs.com/ssslinppp/p/4675495.html <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: ...

  7. Laplace算子和Laplacian矩阵

    1 Laplace算子的物理意义 Laplace算子的定义为梯度的散度. 在Cartesian坐标系下也可表示为: 或者,它是Hessian矩阵的迹: 以热传导方程为例,因为热流与温度的梯度成正比,那 ...

  8. Python 2.x 和 3.x的区别

    Python有两个版本,2.x 和 3.x ,两个版本不兼容,3.x 不不考虑对2.x代码的向后兼容. 在3.x中,一些语法,内建函数和对象的行为都有所调整. 大部分的python库都支持 pytho ...

  9. (转)ActionContext和ServletActionContext

    前面已经了解到ActionContext是Action执行时的上下文,里面存放着Action在执行时需要用到的对象,我们也称之为广义值栈. Struts2在每次执行Action之前都会创建新的Acti ...

  10. java后台导出pdf

    新页面打开wpf @RequestMapping("/showPdf") public String getpic( HttpServletRequest request, Htt ...