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的更多相关文章

  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目录及资料收集

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

  5. Cracking the Coding Interview(Trees and Graphs)

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

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

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

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

  8. 《Cracking the Coding Interview》——第11章:排序和搜索——题目4

    2014-03-21 21:28 题目:给定一个20GB大小的文本文件,每一行都是一个字符串.请设计方法将这个文件里的字符串排序. 解法:请看下面的注释. 代码: // 11.4 Given a fi ...

  9. 《Cracking the Coding Interview》——第11章:排序和搜索——题目3

    2014-03-21 20:55 题目:给定一个旋转过的升序排序好的数组,不知道旋转了几位.找出其中是否存在某一个值. 解法1:如果数组的元素都不重复,那么我的解法是先找出旋转的偏移量,然后进行带偏移 ...

  10. 《Cracking the Coding Interview》——第11章:排序和搜索——题目2

    2014-03-21 20:49 题目:设计一种排序算法,使得anagram排在一起. 解法:自定义一个comparator,使用额外的空间来统计字母个数,然后比较字母个数. 代码: // 11.2 ...

随机推荐

  1. Linux教程之:Nginx [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)

    Nginx [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use) 使用命令关闭占用80端口的程序 sudo fuser - ...

  2. IIS https绑定主机头方法 [可实现禁止直接通过IP访问及“IIS中绑定多个https(但需要多个IP地址)”]

    步骤: 打开:%systemroot%\SYSTEM32\inetsrv\config\ 打开:applicationHost.config 查找:<binding protocol=" ...

  3. Docker cgroup.procs no space left on device

    环境:centos6 运行docker 时 错误提示: System error: write /sys/fs/cgroup/docker/01f5670fbee1f6687f58f3a943b1e1 ...

  4. SQL语句关于时间的查询小心得,希望大家给点意见

    完全使用时间函数去搞定查询日期,之前写的可能有些问题,现在删了修正一下 本月记录: SELECT * FROM 表 WHERE datediff(month,[dateadd],getdate())= ...

  5. Android(java)学习笔记63:Clock App 编写报错01

    1. 首先我们二话不说直接先看报错内容如下: 07-12 08:25:03.572: E/dalvikvm(3602): native fork pid:0 done. 07-12 08:25:03. ...

  6. 【HHHOJ】ZJOI2019模拟赛(十五)03.17 解题报告

    点此进入比赛 得分: \(42+10+14=66\) 排名: \(Rank\ 3\) \(Rating\):\(+53\) \(T1\):[HHHOJ200]稗田的梦中之梦(点此看题面) 暴力\(DF ...

  7. python 爬糗事百科

    糗事百科网站段子爬取,糗事百科是我见过的最简单的网站了!!! #-*-coding:utf8-*- import requests import re import sys reload(sys) s ...

  8. 数长方形有多少个?POJ(1693)

    题目链接:http://poj.org/problem?id=1693 解题报告: 随机选两根横的,再找一下与这两根横线相交的竖线有多少根,m,那么就有(m-1)*m/2个长方形. #include ...

  9. 2018年Linux运维人员必会开源运维工具体系总结

    操作系统:Centos,Ubuntu,Redhat,suse,Freebsd 网站服务:nginx,apache,lighttpd,php,tomcat,resin数据库:MySQL,MariaDB, ...

  10. 初始化mysql数据库时提示字符编码错误的解决办法

    有时候在安装完数据库并初始化的时候会出现如下错误: root@localhost mysql-5.5.19]# bash scripts/mysql_install_db --user=mysql - ...