码迷,mamicode.com
首页 > 其他好文 > 详细

[编程题-搜狐]马戏团

时间:2016-08-06 20:24:44      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

[编程题]马戏团

搜狐员工小王最近利用假期在外地旅游,在某个小镇碰到一个马戏团表演,精彩的表演结束后发现团长正和大伙在帐篷前激烈讨论,小王打听了下了解到, 马戏团正打算出一个新节目“最高罗汉塔”,即马戏团员叠罗汉表演。考虑到安全因素,要求叠罗汉过程中,站在某个人肩上的人应该既比自己矮又比自己瘦,或相等。 团长想要本次节目中的罗汉塔叠的最高,由于人数众多,正在头疼如何安排人员的问题。小王觉得这个问题很简单,于是统计了参与最高罗汉塔表演的所有团员的身高体重,并且很快找到叠最高罗汉塔的人员序列。 现在你手上也拿到了这样一份身高体重表,请找出可以叠出的最高罗汉塔的高度,这份表中马戏团员依次编号为1到N。

#include<iostream>
#include<vector>
#include<map>
#include<utility>
#include<algorithm>
using namespace std;
int lcs(vector<int>&a, vector<int>&b)
{
    int m = a.size(), n = b.size();
    vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
    for (int i = 0; i<m; i++)
    {
        for (int j = 0; j<n; j++)
        {
            if (a[i] == b[j]) dp[i + 1][j + 1] = dp[i][j] + 1;
            else dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
        }
    }
    return dp[m][n];
}
int main()
{
    int n;
    while (cin >> n)
    {
        map<int, vector<int>> hmap;
        map<int, vector<int>> wmap;
        for (int i = 0; i<n; i++)
        {
            int num, h, w;
            cin >> num >> w >> h;
            if (hmap.find(h) == hmap.end())
            {
                vector<int> v;
                v.push_back(num);
                hmap.insert(make_pair(h, v));
            }
            else
            {
                hmap[h].push_back(num);
            }

            if (wmap.find(w) == wmap.end())
            {
                vector<int> v;
                v.push_back(num);
                wmap.insert(make_pair(w, v));
            }
            else
            {
                wmap[h].push_back(num);
            }
        }
        //cout << "hsort  ";
        vector<int> hsort;
        for (auto& x : hmap)
        {
            for (auto& y : x.second)
            {
                hsort.push_back(y);
                //cout << y << ":";
                //cout << x.first << " ";
            }
        }
        //cout << endl;

        //cout << "wsort  ";
        vector<int> wsort;
        for (auto&x : wmap)
        {
            for (auto&y : x.second)
            {
                wsort.push_back(y);
                //cout << y << ":";
                //cout << x.first << " ";
            }
        }
        //cout << endl;
        cout << lcs(hsort, wsort) << endl;
    }
}

 

[编程题-搜狐]马戏团

标签:

原文地址:http://www.cnblogs.com/learning-c/p/5744707.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!