《Cracking the Coding Interview》——第11章:排序和搜索——题目7
2014-03-21 22:05
题目:给你N个盒子堆成一座塔,要求下面盒子的长和宽都要严格大于上面的。问最多能堆多少个盒子?
解法1:O(n^2)的动态规划解决。其实是最长递增子序列问题,所以也可以用O(n * log(n))的优化算法。
代码:
// 11.7 n boxes are to stack up to a tower. Every box must be strictly smaller in width and height than the one right below it.
// How many boxes at most can you stack up?
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std; struct Box {
int x;
int y;
Box(int _x = , int _y = ): x(_x), y(_y) {}; bool operator < (const Box &other) {
if (x != other.x) {
return x < other.x;
} else {
return y < other.y;
}
};
}; int main()
{
vector<Box> v;
vector<int> dp;
int n;
int i, j;
int res; while (scanf("%d", &n) == && n > ) {
v.resize(n);
for (i = ; i < n; ++i) {
scanf("%d%d", &v[i].x, &v[i].y);
}
sort(v.begin(), v.end());
dp.resize(n);
res = ;
for (i = ; i < n; ++i) {
dp[i] = ;
for (j = ; j < i; ++j) {
if (v[j].x < v[i].x && v[j].y < v[i].y) {
dp[i] = dp[j] + > dp[i] ? dp[j] + : dp[i];
}
}
res = dp[i] > res ? dp[i] : res;
}
printf("%d\n", res); v.clear();
dp.clear();
} return ;
}
解法2:用二分查找优化后的代码,其中使用了STL算法库提供的lower_bound(),二分也不总是要手写的。
代码:
// 11.7 n boxes are to stack up to a tower. Every box must be strictly smaller in width and height than the one right below it.
// How many boxes at most can you stack up?
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std; struct Box {
int x;
int y;
Box(int _x = , int _y = ): x(_x), y(_y) {}; bool operator < (const Box &other) {
if (x != other.x) {
return x < other.x;
} else {
return y < other.y;
}
};
}; int main()
{
vector<Box> v;
vector<int> dp;
vector<int>::iterator it;
int n;
int i; while (scanf("%d", &n) == && n > ) {
v.resize(n);
for (i = ; i < n; ++i) {
scanf("%d%d", &v[i].x, &v[i].y);
}
sort(v.begin(), v.end());
dp.push_back(v[].y);
for (i = ; i < n; ++i) {
if (v[i].y > dp[dp.size() - ]) {
dp.push_back(v[i].y);
} else {
it = lower_bound(dp.begin(), dp.end(), v[i].y);
*it = v[i].y;
}
}
printf("%d\n", (int)dp.size()); v.clear();
dp.clear();
} return ;
}
《Cracking the Coding Interview》——第11章:排序和搜索——题目7的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目4
2014-03-21 21:28 题目:给定一个20GB大小的文本文件,每一行都是一个字符串.请设计方法将这个文件里的字符串排序. 解法:请看下面的注释. 代码: // 11.4 Given a fi ...
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目3
2014-03-21 20:55 题目:给定一个旋转过的升序排序好的数组,不知道旋转了几位.找出其中是否存在某一个值. 解法1:如果数组的元素都不重复,那么我的解法是先找出旋转的偏移量,然后进行带偏移 ...
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目2
2014-03-21 20:49 题目:设计一种排序算法,使得anagram排在一起. 解法:自定义一个comparator,使用额外的空间来统计字母个数,然后比较字母个数. 代码: // 11.2 ...
随机推荐
- 笨办法学Python(十三)
习题 13: 参数.解包.变量 在这节练习中,我们将讲到另外一种将变量传递给脚本的方法(所谓脚本,就是你写的 .py 程序).你已经知道,如果要运行 ex13.py,只要在命令行运行 python e ...
- vm中efi模式安装windows10
选择dvd: 界面出现“Press any key to boot from CD or DVD”时,再迅速按下任意键就OK了.
- Python-Django框架学习笔记——第二课:Django的搭建
Django 环境搭建 一. 版本选择 Django 1.5.x 支持 Python 2.6.5 Python 2.7, Python 3.2 和 3.3. Django 1.6.x 支持 Pytho ...
- 【转】Android xml资源文件中@、@android:type、@*、?、@+含义和区别
一.@代表引用资源 1.引用自定义资源.格式:@[package:]type/name android:text="@string/hello" 2.引用系统资源.格式:@andr ...
- 【转】基于JavaMail的Java邮件发送
http://blog.csdn.net/xietansheng/article/details/51673073 http://blog.csdn.net/xietansheng/article/d ...
- hihocoder1398 网络流五之最大权闭合子图
最大权闭合子图 虽然我自己现在总结不好最大权闭合子图.但也算稍稍理解辣. 网络流起步ing~~~(- ̄▽ ̄)- #include<iostream> #include<cstdio& ...
- 关于css 中position使用的浅谈
在css中有一种属性position.在W3C上我们可以找到他又一下几种属性:absolute.fixed.relative.static.inherit.但是position的使用却并不是简简单单的 ...
- Android笔记(预安装APK)
一般一个安卓的产品在出厂时,会预安装许多APK,关于这些APP,主要分为下面这几类 1.系统级别APK 这一类应用一般是:电话/设置或者厂家自己特定的应用. 2.系统预安装APK 因为商业原因,产品出 ...
- Java面试不得不知的程序(二)
[程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 斐波那契数列:前面相邻两项之和,构 ...
- java mysql多次事务 模拟依据汇率转账,并存储转账信息 分层完成 dao层 service 层 client层 连接池使用C3p0 写入库使用DBUtils
Jar包使用,及层的划分 c3p0-config.xml <?xml version="1.0" encoding="UTF-8"?> <c3 ...