找最大序列

2023-11-18

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;
}


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

找最大序列 的相关文章

随机推荐