feat: added alternative python solutions.
This commit is contained in:
@@ -43,7 +43,7 @@
|
||||
},
|
||||
"solutions": {
|
||||
"main-ac": "ac.cpp",
|
||||
"alternative-ac": [],
|
||||
"alternative-ac": ["alternative-ac.py"],
|
||||
"wrong-answer": [],
|
||||
"time-limit": ["TLE.cpp"],
|
||||
"time-limit-or-ac": [],
|
||||
|
||||
Binary file not shown.
Binary file not shown.
32
rod-cutting/src/alternative-ac.py
Normal file
32
rod-cutting/src/alternative-ac.py
Normal file
@@ -0,0 +1,32 @@
|
||||
n = int(input())
|
||||
prices = list(map(int, input().split()))
|
||||
|
||||
dp = [0] * (n + 1)
|
||||
choice = [0] * (n + 1)
|
||||
|
||||
for i in range(1, n + 1):
|
||||
max_revenue = prices[i - 1]
|
||||
best_cut = 0
|
||||
|
||||
for j in range(1, i):
|
||||
revenue = prices[j - 1] + dp[i - j]
|
||||
if revenue > max_revenue:
|
||||
max_revenue = revenue
|
||||
best_cut = j
|
||||
|
||||
dp[i] = max_revenue
|
||||
choice[i] = best_cut
|
||||
|
||||
pieces = []
|
||||
remaining = n
|
||||
while remaining > 0:
|
||||
cut = choice[remaining]
|
||||
if cut == 0:
|
||||
pieces.append(remaining)
|
||||
remaining = 0
|
||||
else:
|
||||
pieces.append(cut)
|
||||
remaining -= cut
|
||||
|
||||
print(dp[n])
|
||||
print(' '.join(map(str, pieces)))
|
||||
Reference in New Issue
Block a user