Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- class FinishedException(Exception):
- pass
- spots, total_runs, groups_amount = [int(i) for i in input().split()]
- groups = []
- for i in range(groups_amount):
- groups.append(int(input()))
- groups = np.array(groups)
- # Initializing the money and runs
- money = np.array([0], dtype=np.int64)
- runs = total_runs
- """
- # Very basic version, works if the dataset isn't huge.
- try:
- while True:
- cumulative = np.cumsum(groups)
- found_index = np.searchsorted(cumulative, spots, side='right')
- largest_found = cumulative[found_index-1]
- money += largest_found
- groups = np.roll(groups, -found_index)
- runs -= 1
- if runs < 1:
- raise FinishedException
- """
- # More advanced version, still doesn't work on large datasets
- # Initializing values
- cumulative = np.cumsum(groups)
- last_index = 0
- last_value = 0
- try:
- # If we can fit all the groups to the coaster at once, we get result from multiplication
- total = cumulative[-1]
- if total <= spots:
- money += total*runs
- raise FinishedException
- while True:
- new_index = np.searchsorted(cumulative, last_value+spots, side='right')
- new_value = cumulative[new_index-1]
- # Updating cumulative to be the portion that hasn't been searched through yet
- cumulative = cumulative[new_index:]
- new_size = cumulative.size
- if new_size == 0:
- # We've reached end of the vector.
- # Either we have right amount of people or not enough.
- if (new_value - last_value + groups[0]) > spots:
- # Just the right amount of people, no need to roll,
- # But need to reinitialize the vectors
- money += (new_value - last_value)
- runs -= 1
- last_value = 0
- cumulative = np.cumsum(groups)
- # At this point we've come around to the start
- # We check amount of runs done and left, and see if we can use
- # that info to remove tons of runs and add tons of money
- # with simple multiplication!
- divider = total_runs//(total_runs-runs)
- runs = total_runs-((total_runs-runs)*divider)
- money *= divider
- else:
- # Not quite enough people, making a vector by taking last_size
- # amount of components from end of 'groups' and then pasting 'groups'
- # in whole to it.
- last_value = 0
- cumulative = np.concatenate((groups[-last_size:], groups))
- cumulative = np.cumsum(cumulative)
- else:
- # If not at the end of vector, adding the difference to the money amount
- # and updating values
- money += (new_value - last_value)
- last_value = new_value
- last_size = new_size
- runs -= 1
- if runs < 1:
- raise FinishedException
- except FinishedException:
- print(money[0])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement