69 lines
1.6 KiB
Python
69 lines
1.6 KiB
Python
"""
|
|
name: Nicholas Tamassia
|
|
|
|
Honor Code and Acknowledgments:
|
|
|
|
This work complies with the JMU Honor Code.
|
|
|
|
Comments here on your code and submission.
|
|
"""
|
|
|
|
|
|
def construct(sections: list[int], target: int) -> list[tuple[int, int]]:
|
|
s: list[int] = sorted(sections, reverse=True)
|
|
n: int = len(s)
|
|
|
|
IMPOSSIBLE: int = target + 1
|
|
|
|
memo: list[list[int]] = [([IMPOSSIBLE] * (target + 1)) for _ in range(0, n + 1)]
|
|
|
|
for i in range(n + 1):
|
|
memo[i][0] = 0
|
|
|
|
for i in range(n - 1, -1, -1):
|
|
for t in range(1, target + 1):
|
|
skip: int = memo[i + 1][t]
|
|
|
|
t_new: int = t - s[i]
|
|
use: int = memo[i][t_new] + 1 if t_new >= 0 else IMPOSSIBLE
|
|
|
|
memo[i][t] = min(skip, use)
|
|
|
|
total_sections: list[int] = [0] * n
|
|
|
|
i, t = 0, target
|
|
|
|
while i < n or t > 0:
|
|
skipped: int = memo[i + 1][t]
|
|
|
|
t_new2: int = t - s[i]
|
|
used: int = memo[i][t_new2] + 1 if t_new2 >= 0 else IMPOSSIBLE
|
|
|
|
if used <= skipped:
|
|
total_sections[i] += 1
|
|
t -= s[i]
|
|
else:
|
|
i += 1
|
|
|
|
return list(zip(s, total_sections))
|
|
|
|
|
|
# All modules for CS 412 must include a main method that allows it
|
|
# to imported and invoked from other python scripts
|
|
def main():
|
|
sections: list[int] = list(map(int, input().split()))
|
|
target: int = int(input())
|
|
|
|
best_sections: list[tuple[int, int]] = construct(sections, target)
|
|
total: int = 0
|
|
|
|
for i, n in sorted(best_sections):
|
|
total += n
|
|
print(f"{n} of length {i}")
|
|
|
|
print(f"{total} rocket sections minimum")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|