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. 使用Android studio创建的AIDL编译时找不到自定义类的解决办法

    使用AS创建ADIL文件时AS会在main文件夹下给我们生成一个aidl文件夹和一个相同包名的包,通常我们会把所有和ADIL相关的类或文件放在这个包下,但是如果存在自定义的类时,程序编译时无法通过,提 ...

  2. C# 文件操作(上传,下载,读取,写入)

    1. 通过byte[]数据下载文件(这种方法可用于以开放Api的形式传递文件内容) public void FileDownLoadByByte(byte[] fileData, string fil ...

  3. eclipse中基于maven构建的web项目pom.xml中指定的jar包无法发布到tomcat中

    eclipse运行maven web项目报错: 信息: Starting Servlet Engine: Apache Tomcat/7.0.57 一月 07, 2015 11:50:44 下午 or ...

  4. 深入剖析js命名空间函数namespace

    在看阿里员工写的开源数据库连接池的druid的源代码时,发现了其中在jquery的原代码中又定义了一个命名空间的函数:$.namespace(),其代码如下: 网址为:https://github.c ...

  5. Linux Shell 04 数字/字符串/文件测试

    一. 数字测试 格式:n1  -op  n2 测试操作op: eq/ne/le/ge/lt/gt    -->    等于/不等于/小于等于/大于等于/小于/大于 1. 数字比较可以使用特殊的( ...

  6. 湖南师范大学第五届大学生计算机程序设计竞赛--G--修路

    题目链接:http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11464&courseid=132 题目: ...

  7. iOS开发-微博客户端-基本界面搭建(01)

    1>创建程序载入界面 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDict ...

  8. Linux 磁盘与文件系统管理

    介绍一本书叫<Linux 鸟哥私房菜>, 一本教人用linux很经典的一本书,这两天又看了里面的一章节,做一点笔记.有一些很细节的东西的, 在平时运用过很容易被忽略. 1)U盘使用的文件格 ...

  9. Ubuntu查看系统的信息

    转载自:http://blog.chinaunix.net/uid-25885064-id-3440641.html 系统信息 # uname -a # 查看内核/操作系统/CPU信息 # cat / ...

  10. 给定一个整数N,找出一个比N大且最接近N,但二进制权值与该整数相同 的数

    1,问题描述 给定一个整数N,该整数的二进制权值定义如下:将该整数N转化成二进制表示法,其中 1 的个数即为它的二进制权值. 比如:十进制数1717 的二进制表示为:0000 0110 1011 01 ...