//原文:
//
// 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)
//
#include <iostream>
#include <algorithm>
using namespace std; struct people
{
public:
people(){};
people(int _h, int _w){h = _h; w = _w;};
int h;
int w;
}; bool compare(const people &p1, const people &p2)
{
if ( p1.h == p2.h)
{
return p1.w < p2.w;
}
return p1.h < p2.h;
} //求最长子序列,参考http://www.csie.ntnu.edu.tw/~u91029/LongestIncreasingSubsequence.html#1
int getLIS(people *p, int size)
{
int *length = new int[size];
for (int i = 0; i<size ; i++)
{
length[i] = 1;
} for (int i =0; i <size ; i++)
{
for (int j = i+1; j<size; j ++)
{
if (p[i].w < p[j].w)
{
length[j] = max(length[j], length[i] + 1);
}
}
} int n = length[0];
for (int i = 0; i < size; i++)
{
n = (length[i]>n? length[i]:n);
}
return n;
} int main()
{
people p[5] = {people(2,10), people(5,18), people(4,17), people(3,9), people(7,20)};
for (int i = 0; i<5; i++)
{
cout<<p[i].h<<" "<<p[i].w<<endl;
}
cout<<"============="<<endl;
sort(p,p+5,compare);
for (int i = 0; i<5; i++)
{
cout<<p[i].h<<" "<<p[i].w<<endl;
}
cout<<getLIS(p,5)<<endl;
return 0;
}

Cracking The Coding Interview 9.7的更多相关文章

  1. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  2. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  3. Cracking the Coding Interview(Trees and Graphs)

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

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

  5. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  6. Cracking the coding interview目录及资料收集

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

  7. 《Cracking the Coding Interview》——第13章:C和C++——题目6

    2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...

  8. 《Cracking the Coding Interview》——第5章:位操作——题目7

    2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...

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

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

  10. 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)

    #include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...

随机推荐

  1. 动态规划-填格子问题 Domino and Tromino Tiling

    2018-09-01 22:38:19 问题描述: 问题求解: 本题如果是第一看到,应该还是非常棘手的,基本没有什么思路. 不妨先从一种简化的版本来考虑.如果仅有一种砖块,那么,填充的方式如下.

  2. Can't push you anymore...

    为什么我们不趁着年轻去冒险? 等我们准备好,也许都已经被生活冲淡了激情. Go to different places,to meet different people. To try, to fin ...

  3. ubuntu下安装Firefox中国版解决Ubuntu与Windows下Firefox账号同步问题(已解决)

    1. 下载最新版本火狐Linux版 下载地址:http://firefox.com.cn/download/ 选择火狐Linux64-bit版,下载后文件为:Firefox-latest-x86_64 ...

  4. 线性代数 | Linear Algebra

    网上说<线性代数应该这样学>非常不错,再配合大学教材,把线性代数的基本知识点过一遍. 线性代数 - 知乎 最近在跟一个教程:李宏毅的线性代数 基本知识: Rn :We denote the ...

  5. java异常:java.lang.NullPointerException

    /** * 功能:空指针异常测试 */ /* Object[] parameters1=null; if(parameters1.length>0&&parameters1!=n ...

  6. Shell脚本字体颜色

    [root@web01 scripts]# man console_codesecho -e "\033[背景颜色:字体颜色m字符串\033[0m",例:echo -e " ...

  7. Vue.js简单的状态管理和 Vuex的几个核心概念使用。

    由于状态零散地分布在许多组件和组件之间的交互中,大型应用复杂度也经常逐渐增长. 如果多层组件嵌套使用,传递prop,和事件emit.都很不方便. 不方便对数据的修改进行历史记录.影响后续的调试! 为了 ...

  8. 【IOS学习】【Swift语言】

    基本语法: OS X playground 引入 import Cocoa IOS playground 引入 import UIKit 基本数据类型 let 定义常量 定义完成之后无法修改 var ...

  9. 使用scrapy-crawlSpider 爬取tencent 招聘

    Tencent 招聘信息网站 创建项目 scrapy startproject Tencent 创建爬虫 scrapy genspider -t crawl tencent 1. 起始url  sta ...

  10. python基础之变量以及if语句

    1.变量 1.1定义:变量是一种介质,是将一些值暂时存储在内存中,方便后续程序调用.可将其看作容器但其内部的东西是可变化的. 1.2 变量的命名规则: 1.变量的命名只能由数字,字母,下划线构成. 2 ...