找最大序列

来源:互联网 发布:java动物继承例子 编辑:程序博客网 时间:2024/06/06 03:07
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;}


0 0