[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, 0);
        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 < 0) return;
        HtWt *cur = array[curIdx];
        vector<HtWt*> res;
        for (int i = 0; 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 + 1);
    }
    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;
    }
};

 

转载于:https://www.cnblogs.com/grandyang/p/4891506.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值