11.7 A circus is designing a tower routine consisting of people standing atop one another's shoulders. For practical and aesthetic reasons, each person must be both shorter and lighter than the person below him or her. Given the heights and weights of each person in the circus, write a method to compute the largest possible number of people in such a tower.
 EXAMPLE:
 Input (ht,wt): (65, 100) (70, 150) (56, 90) (75, 190) (60, 95) (68, 110)
 Output:The longest tower is length 6 and includes from top to bottom: (56, 90) (60,95) (65,100) (68,110) (70,150) (75,190)

这道题说马戏团有一种人塔,上面的人要比下面的人既矮又轻,问我们最多能有多少个人组成人塔。那么就相当于求最长的递增子序列,我们的做法是先将所有的人按身高排个序,方法可参见我之前的博客C++ sort vector<vector<int> > or vector<MyClass> 容器的排序。然后对于体重序列,就相当于找最长连续递增子序列Longest Increasing Subsequence (LIS),找LIS的方法可参见我之前的博客Longest Increasing Subsequence 最长递增子序列,参见代码如下:

class HtWt {
public:
int Ht, Wt;
HtWt(int h, int w): Ht(h), Wt(w) {}
bool isBefore(HtWt *other) {
if (Ht < other->Ht && Wt < other->Wt) return true;
else return false;
}
}; bool cmp(HtWt const *a, HtWt const *b) {
return a->Ht < b->Ht;
} class Solution {
public:
vector<HtWt*> getIncreasingSequence(vector<HtWt*> &items) {
sort(items.begin(), items.end(), cmp);
return longestIncreasingSubsequence(items);
}
vector<HtWt*> longestIncreasingSubsequence(vector<HtWt*> &array) {
vector<vector<HtWt*> > solutions;
longestIncreasingSubsequence(array, solutions, );
vector<HtWt*> res;
for (auto &a : solutions) {
res = seqWithMaxLength(res, a);
}
return res;
}
void longestIncreasingSubsequence(vector<HtWt*> &array, vector<vector<HtWt*> > &solutions, int curIdx) {
if (curIdx >= array.size() || curIdx < ) return;
HtWt *cur = array[curIdx];
vector<HtWt*> res;
for (int i = ; i < curIdx; ++i) {
if (array[i]->isBefore(cur)) {
res = seqWithMaxLength(res, solutions[i]);
}
}
vector<HtWt*> new_solution = res;
new_solution.push_back(cur);
solutions.push_back(new_solution);
longestIncreasingSubsequence(array, solutions, curIdx + );
}
vector<HtWt*> seqWithMaxLength(vector<HtWt*> seq1, vector<HtWt*> seq2) {
if (seq1.empty()) return seq2;
if (seq2.empty()) return seq1;
return seq1.size() > seq2.size() ? seq1 : seq2;
}
};

[CareerCup] 11.7 Tower of People in Circus 马戏团的人塔的更多相关文章

  1. [CareerCup] 11.1 Merge Arrays 合并数组

    11.1 You are given two sorted arrays, A and B, where A has a large enough buffer at the end to hold ...

  2. [CareerCup] 11.2 Sort Anagrams Array 异位词数组排序

    11.2 Write a method to sort an array of strings so that all the anagrams are next to each other. 这道题 ...

  3. [CareerCup] 11.3 Search in Rotated Sorted Array 在旋转有序矩阵中搜索

    11.3 Given a sorted array of n integers that has been rotated an unknown number of times, write code ...

  4. [CareerCup] 11.4 Sort the File 文件排序

    11.4 Imagine you have a 20 GB file with one string per line. Explain how you would sort the file. 这道 ...

  5. [CareerCup] 11.5 Search Array with Empty Strings 搜索含有空字符串的数组

    11.5 Given a sorted array of strings which is interspersed with empty strings, write a method to fin ...

  6. [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵

    11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...

  7. [CareerCup] 11.8 The Rank of Number 数的排行

    11.8 Imagine you are reading in a stream of integers. Periodically, you wish to be able to look up t ...

  8. 2014/11/06 Oracle触发器初步 2014-11-06 09:03 49人阅读 评论(0) 收藏

    触发器我就不多解释了,保证数据的完整性的神器,嗯..也是减少程序员工作托管给数据库操作的好帮手.就不讲一些大道理了.通俗点,我们对数据库的操作,无非就是增 删 改 查. 触发器就是在删,改,增的时候( ...

  9. CareerCup All in One 题目汇总 (未完待续...)

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

随机推荐

  1. 问题解决——VS2010 将生成的文件复制到指定位置

    我是从VC6直接过渡到VS2010的,VS2008没怎么用过.用VS2010的时候,每次生成dll后,手工把dll.lib..h文件复制到指定文件夹太麻烦了,所以着手写了这个. =========== ...

  2. PL/SQL之--存储过程

    一.存储过程 存储过程是一组为了完成特定功能的SQL 语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它.oracle可以把PL/SQL程序储存在数 ...

  3. Oracle 11g 中恢复管理器RMAN介绍

    这是我平时摘录的笔记,从管理艺术那本书上摘录出来的,放到这里 RMAN 可在数据库服务器的帮助下从数据库内备份数据文件,可构造数据文件映像副本.控制文件和控制文件映像.对当日志 SPFILE 和RMA ...

  4. Nginx的安装与使用

    在 CentOS 7 系统上: $ sudo rpm --import http://nginx.org/keys/nginx_signing.key $ sudo rpm -ivh http://n ...

  5. 编译时:virtual memory exhausted: Cannot allocate memory

    一.问题 当安装虚拟机时系统时没有设置swap大小或设置内存太小,编译程序会出现virtual memory exhausted: Cannot allocate memory的问题,可以用swap扩 ...

  6. 【ubuntu】中文输入法安装二三事

    本来很愉快地刷着JS程序,很有感慨啊,想写篇博客记一下学习笔记,结果忘记了博客账号,后来通过邮箱找回了之后想要开始写..发现ubuntu的中文输入法不能用啊(其实不是不能用,就是小白没搞清楚状况,双系 ...

  7. centos7下docker 部署javaweb

    LXC linux container 百度百科:http://baike.baidu.com/link?url=w_Xy56MN9infb0hfYObib4PlXm-PW02hzTlCLLb1W2d ...

  8. Sample: Write And Read data from HDFS with java API

    HDFS: hadoop distributed file system 它抽象了整个集群的存储资源,可以存放大文件. 文件采用分块存储复制的设计.块的默认大小是64M. 流式数据访问,一次写入(现支 ...

  9. [转]ionic Accordion list three levels

    简化后的主要代码: $scope.groups = []; for (var i = 0; i < 2; i++) { $scope.groups[i] = { name: i, items: ...

  10. 使用LVS实现负载平衡之Windows Server 2008配置

    LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一.承载于 II ...