[编程题-搜狐]马戏团

[编程题]马戏团

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

第二次提交代码 通过

参考

https://www.nowcoder.com/questionTerminal/c2afcd7353f84690bb73aa6123548770

#include<iostream>
#include<algorithm>
using namespace std;
struct player{
    int w;
    int h;
};
bool com_w(player p1, player p2)
{
    //按照体重从小到大排序
    if(p1.w!=p2.w)
        return p1.w<=p2.w;
    //在体重相等的条件下,身高高的在前(在上)
    else
        return p1.h>p2.h;
}
int main()
{
    int N;
    int h;
    int w;
    int index;
    vector<player> p;
    while(cin>>N)
    {
        p.clear();
        for(int i=0;i<N;i++)
        {
            player pt;
            cin>>index>>w>>h;
            pt.w=w;
            pt.h=h;
            p.push_back(pt);
        }
        sort(p.begin(),p.end(),com_w);
        //按身高求最大上升子序列
        int dp2[N+1];
        int max=0;
        dp2[0]=1;
        for(int i=1;i<N;i++)
        {
            dp2[i]=1;
            for(int j=0;j<i;j++)
            {
                if(p[j].h<=p[i].h && dp2[j]+1 > dp2[i])
                    dp2[i]=dp2[j]+1;
            }
        }
        for(int i=0;i<N;i++)
            if(max<dp2[i]) max=dp2[i];
        cout<<max<<endl;
    }
    return 0;
}

第一次提交代码 未通过

#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;
    }
}
原文地址:https://www.cnblogs.com/learning-c/p/5744707.html