找最大序列

    xiaoxiao2021-03-26  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)

    struct Node { int height; int weight; Node(int h, int w): height(h), weight(w) {} }; bool comp(const Node &left, const Node &right) { return left.height < right.height; } int fun(vector<Node> &a) { int n = a.size(); sort(a.begin(), a.end(), comp); int buf[n]; buf[0] = 1; int result = 1; for (int i = 1; i < n; i++) { buf[i] = 1; for (int j = 0; j < i; j++) { if (a[i].height > a[j].height && a[i].weight > a[j].weight) { buf[i] = max(buf[i], buf[j]+1); } } result = max(result, buf[i]); } return result; }

    转载请注明原文地址: https://ju.6miu.com/read-600211.html

    最新回复(0)