马戏团(动态规划)---搜狐2016研发工程师编程题
来源:互联网 发布:淘宝专卖店与专营店 编辑:程序博客网 时间:2024/05/04 04:44
[编程题] 马戏团
搜狐员工小王最近利用假期在外地旅游,在某个小镇碰到一个马戏团表演,精彩的表演结束后发现团长正和大伙在帐篷前激烈讨论,小王打听了下了解到, 马戏团正打算出一个新节目“最高罗汉塔”,即马戏团员叠罗汉表演。考虑到安全因素,要求叠罗汉过程中,站在某个人肩上的人应该既比自己矮又比自己瘦,或相等。 团长想要本次节目中的罗汉塔叠的最高,由于人数众多,正在头疼如何安排人员的问题。小王觉得这个问题很简单,于是统计了参与最高罗汉塔表演的所有团员的身高体重,并且很快找到叠最高罗汉塔的人员序列。 现在你手上也拿到了这样一份身高体重表,请找出可以叠出的最高罗汉塔的高度,这份表中马戏团员依次编号为1到N。
输入描述:
首先一个正整数N,表示人员个数。 之后N行,每行三个数,分别对应马戏团员编号,体重和身高。
输出描述:
正整数m,表示罗汉塔的高度。
输入例子:
61 65 1002 75 803 80 1004 60 955 82 1016 81 70
输出例子:
4
动态规划,用到了最长上升子序列问题。首先按照体重从小到大排序,体重相同时,身高高的在上,然后求最长身高上升子序列的长度。
#include <iostream>#include <vector>#include <algorithm>using namespace::std;struct node {int w;int h;node(int _w, int _h) : w(_w), h(_h) {}};bool cmp(node first, node next) {if (first.w != next.w) {return first.w < next.w;}else {return first.h > next.h;}}int main() {int input;while (cin >> input) {int num, w, h;vector<node> vec;for (int i = 0; i < input; ++i) {cin >> num >> w >> h;node tmp(w, h);vec.push_back(tmp);}stable_sort(vec.begin(), vec.end(), cmp);vector<int> dq(input + 1, 0);dq[0] = 1;for (int i = 0; i < input; ++i) {dq[i] = 1;for (int j = 0; j < i; ++j) {if (vec[j].h <= vec[i].h && dq[j] + 1 >= dq[i]) {dq[i] = dq[j] + 1;}}}int max = 0;for (int i = 0; i < dq.size(); ++i) {if (max < dq[i]) max = dq[i];}cout << max << endl;}return 0;}
- 首页
- 上一页
- 1
- 2
- 下一页
- 末页
- 首页
- 上一页
- 1
- 2
- 下一页
- 末页
第二次做:
#include <iostream>#include <vector>#include <algorithm>using namespace::std ;struct node { int w ; int h ;} ;bool cmp( node first, node next ) { if ( first.w != next.w ) { return first.w < next.w ; } else { return first.h > next.h ; }}int main() { int input ; while ( cin >> input ) { vector<node> vec ; for ( int i = 0; i < input; ++ i ) { int num, w, h ; cin >> num >> w >> h ; node tmp ; tmp.w = w ; tmp.h = h ; vec.push_back( tmp ) ; } stable_sort( vec.begin(), vec.end(), cmp ) ; vector<int> dq( input + 1, 0 ) ; dq[0] = 1 ; for ( int i = 0; i < input; ++ i ) { dq[i] = 1 ; for ( int j = 0; j < i; ++ j ) { if ( vec[j].h <= vec[i].h && dq[j] + 1 > dq[i] ) { dq[i] = dq[j] + 1 ; } } } int max = 0 ; for ( int i = 0; i < input; ++ i ) { if ( max < dq[i] ) max = dq[i] ; } cout << max << endl ; } return 0 ;}
第三次做:
#include <iostream>#include <vector>#include <algorithm>using namespace::std ;struct node { int w ; int h ;} ;bool cmp( node first, node next ) { if ( first.w != next.w ) return first.w < next.w ; else return first.h > next.h ;}int main() { int n ; while ( cin >> n ) { if ( n <= 0 ) break ; vector<node> vec ; for ( int i = 0; i < n; ++ i ) { int num, weight, height ; cin >> num >> weight >> height ; node tmp ; tmp.w = weight ; tmp.h = height ; vec.push_back( tmp ) ; } stable_sort( vec.begin(), vec.end(), cmp ) ; vector<int> liss( n + 1, 1 ) ; int max = 0 ; for ( int i = 0; i < n; ++ i ) { for ( int j = 0; j < i; ++ j ) { if ( vec[j].h <= vec[i].h && liss[j] + 1 > liss[i] ) { liss[i] = liss[j] + 1 ; if ( max < liss[i] ) max = liss[i] ; } } } cout << max << endl ; } return 0 ;}
0 0
- 马戏团(动态规划)---搜狐2016研发工程师编程题
- 年终奖(动态规划)---- 京东2016研发工程师编程题
- 奖学金--动态规划--网易2016研发工程师编程题
- 笔试题:马戏团(搜狐)
- 发奖金----搜狐2016研发工程师编程题
- 搜狐2016研发工程师编程试题
- 最长递增子序列,(搜狐[编程题]马戏团)
- 路灯--动态规划(保留2位小数输出)--网易2016研发工程师编程题
- 风口的猪-中国牛市(动态规划)----百度2016研发工程师在线编程题
- 拜访(动态规划)----美团2016研发工程师在线编程题
- 股票交易日(动态规划)----美团2016研发工程师编程题(二)
- 牛客网---2016---搜狐马戏团
- 搜狐笔试题 马戏团
- 搜狐2017秋招研发工程师笔试 —— 袋鼠过河(贪心、动态规划、转为图)
- 【牛客网】【搜狐2016研发工程师编程题】【最大上升子序列】
- 搜狐笔试编程题(2016研发岗)
- 名企笔试:搜狐2016研发工程师笔试题(最高罗汉塔)
- [编程题] 马戏团
- 使用mysqladmin命令修改Mysql密码
- Using the latest Jython with a WebSphere Application Server wsadmin thin client
- java ping++支付
- JavaSE配置window下环境变量
- Android Studio导入Eclipse的Android项目问题的小结
- 马戏团(动态规划)---搜狐2016研发工程师编程题
- C++ 类的静态成员详细讲解
- redis 下key的过期时间详解 :expire
- js 生成随机字符串
- Properties 读取配置文件
- 关于The serializable class does not declare a static final serialVersionUID field of type long
- Hadoop(二)WordCount
- Protocol Buffers学习小记-基于servlet的http RPC实现
- 学习笔记7——在CentOS中修改中文字符集