Pagini recente » Cod sursa (job #322054) | Cod sursa (job #295134) | Cod sursa (job #2248312) | Cod sursa (job #12672) | Cod sursa (job #2541891)
with open('transport.in') as f:
lines = f.read().split("\n")
n = int(lines[0].split()[0])
k = int(lines[0].split()[1])
a = [int(x) for x in lines[1:n + 1]]
maxSol = sum(a)
minSol = max(a)
def getSolution(val):
currentSum = 0
nrOfGroups = 1
for elem in a:
currentSum += elem
if currentSum > val:
nrOfGroups += 1
currentSum = elem
if nrOfGroups > k:
return False
return True
def binarySearch(start, end):
middle = int((start + end) / 2)
middleSolution = getSolution(middle)
if start == end:
if middleSolution:
return middle
else:
return None
if middleSolution:
# putem mai bine => cauta in stanga
leftSolution = binarySearch(start, middle - 1)
if leftSolution is not None:
return leftSolution
return middleSolution
else:
# nu mere => cauta in dreapta
rightSolution = binarySearch(middle + 1, end)
return rightSolution
res = binarySearch(minSol, maxSol)
with open('transport.out', 'w') as g:
g.write(str(res))