[CareerCup] 11.7 Tower of People in Circus 马戏团的人塔
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 马戏团的人塔的更多相关文章
- [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 ...
- [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. 这道题 ...
- [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 ...
- [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. 这道 ...
- [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 ...
- [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 ...
- [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 ...
- 2014/11/06 Oracle触发器初步 2014-11-06 09:03 49人阅读 评论(0) 收藏
触发器我就不多解释了,保证数据的完整性的神器,嗯..也是减少程序员工作托管给数据库操作的好帮手.就不讲一些大道理了.通俗点,我们对数据库的操作,无非就是增 删 改 查. 触发器就是在删,改,增的时候( ...
- CareerCup All in One 题目汇总 (未完待续...)
Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...
随机推荐
- Swift面向对象基础(上)——Swift中的枚举
Swift中枚举 学习笔记来自<极客学院> import Foundation /**********1*Swift定义枚举的语法格式*************/ /* enum 枚举名 ...
- Linux 下Firefox无法打开在'.domain'之前带有中划线的域名
问题 Linux系统下的Firefox无法打开在".domain"之前带有中划线的域名 eg:"http://su---.diandian.com/" 问题原因 ...
- 敏捷软件开发(1)--- STATE 模式
如果状态在运行过程中,不停的切换和改变,我们怎么办? 状态的迁移是我们生活和工程中非常普遍的一个概念.于是在数学上有一种理论来分析和解决这个问题. 有限状态机理论是一个非常成熟的理论,所有动作和流程的 ...
- ehcache入门
一.简介 ehcache是一个开源的,纯java进程内的缓存框架.它具有快速,简单,具有多种缓存策略等特点. Hibernate中默认就是用了ehcache.在我们的应用中使用ehcache可以快速地 ...
- MongoDB 存储引擎Wiredtiger原理剖析
今天开始看MongoDB 3.2的文档,发现了这么两句话 Support for Multiple Storage Engines MongoDB supports multiple storage ...
- MyEclipse下创建的项目导入到Eclipse中详细的图文配置方法
一.情景再现. 有些人比较喜欢用Myeclipse开发,有些人却比较喜欢用eclipse开发.但是其中有一个问题,Myeclipse里面的项目导入的时候出现了一个小小的问题. 如下: 二.说明问题 导 ...
- 数据结构--树状数组&&线段树--基本操作
随笔目的:方便以后对树状数组(BIT)以及基本线段树的回顾 例题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 例题:hdu 1166 敌兵布阵 T ...
- APP 接口开发及读取静态缓存
<?php /** * Description: App 接口 * Create date:2015-10-19 13:36 * Author: zhaoyingnan **/ class Re ...
- 【软件使用】Windows下的Objective-C集成开发环境搭建(IDE)
Objective-C是苹果软件的编程语言,想要上机学习.调试,有一个集成开发环境(IDE)方便很多.有三类方法搭建Objective-C的集成开发环境: 1) 使用苹果的平台,集成开发环境使用X ...
- 基于对话框的MFC应用程序基本结构
新建一个基于对话框的MFC应用程序,假设命名为 Test:则该应用程序在刚创建的时候,有4个非常重要的文件和3个类: 4个非常重要的文件 1.Test.h 2.Test.cpp (应用程序类头文件) ...