Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import collections
- import sys
- # Constants
- INFINITY = float('inf')
- # Turtle Tuple
- Turtle = collections.namedtuple('Turtle', 'weight strength capacity')
- # Compute Weights
- def compute_weights(turtles):
- # TODO: 1. Insert sentinel/padding into list of turtles
- table = [[INFINITY for i in range(len(turtles)+1)] for j in range(len(turtles)+1)]
- n = len(turtles)
- # TODO: 2. Initialize weights table:
- #
- # Weights(i, j) = Minimum weight of selecting j turtles from first i turtles
- #
- # By default, we set Weights(i, j) to INFINITY
- # If we can select no turtles (j == 0), then minimum weight is 0
- # TODO: 3. Construct table to minimize weight of selecting j turtles from
- # the first i turtles:
- #
- # If Turtle(i)'s capacity >= Weights(i - 1, j - 1) Then
- # Weights(i, j) = Min(Weights(i - 1, j),
- # Weights(i - 1, j - 1) + Turtles[i].weight)
- # Otherwise
- # Weights(i, j) = Weights(i - 1, j)
- for i in range(n):
- for j in range(n):
- if j == 0:
- table[i][j] = 0
- continue
- if turtles[i].capacity >= table[i-1][j-1]:
- table[i][j] = min(table[i-1][j], table[i-1][j-1] + turtles[i].weight)
- else:
- table[i][j] = table[i-1][j]
- # TODO: 4. Return table of weight
- return table
- # Count turtles
- def determine_count(Turtles, Weights):
- # TODO: Determine number of turtles in stack
- count = 0
- bottom_row = len(Turtles) - 1
- for turtle in Weights[bottom_row]:
- if turtle != INFINITY:
- count += 1
- return count
- # Main execution
- if __name__ == '__main__':
- Numbers = map(lambda line: list(map(int, line.split())), sys.stdin)
- Turtles = map(lambda args: Turtle(args[0], args[1], args[1] - args[0]), Numbers)
- Turtles = sorted(Turtles, key=lambda x: x.capacity)# TODO: Sort turtles by capacity
- Weights = compute_weights(Turtles)
- Count = determine_count(Turtles, Weights)
- print(Count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement