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. Jmeter代理录制脚本

    录制的原理: 1.LR/Jmeter录制是针对网络通讯协议层面的,它只关心客户端与服务器端的通讯包2.LR/Jmeter的并发测试实际上就是并发客户端与服务器端的通讯过程3.压力是通过多进程/多线程方 ...

  2. win10中文简体繁体切换快捷键

    win10中文简体繁体切换快捷键Ctrl+Shift+F

  3. html的<head><title><meta>

    head <head>用来标记HTML的头部,里面通常需要包括标题,基础信息,源信息等,定义在HTML头部的内容往往不会在网页上直接显示 title <title>的内容设置在 ...

  4. 面试之jsp、Servlet相关知识——生命周期, 区别等

    1.servlet生命周期 所谓生命周期,指的是servlet容器如何创建servlet实例.分配其资源.调用其方法.并销毁其实例的整个过程. 阶段一: 实例化(就是创建servlet对象,调用构造器 ...

  5. 笔者的编辑语法:MarkDown

    由于博客园里的文章有很多排版不好,一大堆文字堆在一块会影响到阅读. MarkDowm:百科 Markdown 是一种轻量级标记语言,创始人为约翰·格鲁伯(John Gruber).它允许人们“使用易读 ...

  6. runc start container流程分析

    1.runc/start.go Action: func(context *cli.Context) error 该函数首先调用container, err := getContainer(conte ...

  7. 初始化httpclient的几种方式

    1.CloseableHttpClient httpclient = HttpClients.createDefault(); 2.DefaultHttpClient client = new Def ...

  8. ifrog-1028 Bob and Alice are playing numbers(trie树)

    题目链接: Bob and Alice are playing numbers DESCRIPTION Bob and his girl friend are playing game togethe ...

  9. C语言异或运算在程序设计中的妙用

    异或运算符∧也称XOR运算符.它的规则是若参加运算的两个二进位同号,则结果为0(假):异号则为1(真).即0∧0=0,0∧1=1,1∧1=0. 性质: (1).与1异或会翻转 (2).与0异或保持不变 ...

  10. 定制你的Unity编辑器

    Unity的编辑器可以通过写脚本进行界面定制,包括添加功能菜单,今天写游戏Demo用到了记录一下. 为Unity添加子菜单 示例程序 [AddComponentMenu("Defend Ho ...