Tuesday, March 20, 2012

Towering of people in a cricus.


A circus is designing an act consisting of a tower 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 her. Given the heights and weights of each person in the circus, what is the largest possible number of people in such a tower?


Input(ht wt) : (65, 100) (90, 150) (50, 120) (56, 90) (75, 190) (60, 95) (68, 110) (80, 92)
Input format:
8 - number of people.
65 100
90 150
50 120
56 90
75 190
60 95
68 110
80 92


Output: The longest tower is length 5 and includes from top to bottom: 
MaxLen: 5
H:90 :: W:150
H:68 :: W:110
H:65 :: W:100
H:60 :: W:95
H:56 :: W:90


step 1: sort the people by their weights (in ascending order) --- O(nlogn)
step 2: compute the longest increasing subsequence in height --- O(nlogn)

No comments:

Post a Comment