Skip to content

Instantly share code, notes, and snippets.

@thomasfedb
Created March 9, 2017 05:20
Show Gist options
  • Save thomasfedb/57bc8482d54180988154ec4220554d51 to your computer and use it in GitHub Desktop.
Save thomasfedb/57bc8482d54180988154ec4220554d51 to your computer and use it in GitHub Desktop.
def bsearch(kmin, kmax, n, tmax, dur):
# Base
if kmin == kmax:
return kmin
# Recurse
middle = (kmin + kmax) // 2
if simulate(middle, n, tmax, dur):
return bsearch(kmin, middle, n, tmax, dur)
else:
return bsearch(middle + 1, kmax, n, tmax, dur)
def simulate(k, n, tmax, dur):
slot_finish = [0] * k
next_cow = 0
while next_cow < n:
min_slot = -1
min_slot_val = float("inf")
for i in range(k):
if slot_finish[i] < min_slot_val:
min_slot = i
min_slot_val = slot_finish[i]
slot_finish[min_slot] += dur[next_cow]
next_cow += 1
if max(slot_finish) > tmax:
return False
return True
infile = open("cowdance.in")
n, tmax = [int(x) for x in infile.readline().split()]
dur = []
for i in range(n):
dur.append(int(infile.readline().strip()))
print n, tmax, dur
print bsearch(0, n, n, tmax, dur)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment