QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#439098 | #4954. Eliminating Ballons | sergiolozav | RE | 0ms | 0kb | Python3 | 2.6kb | 2024-06-11 16:06:46 | 2024-06-11 16:06:46 |
answer
import timeit
class Stopwatch:
hide = False
def __init__(self, name):
self.time = 0
self.calls = 0
self.name = name
self.current_execution = 0
def start_cumulative(self):
if Stopwatch.hide:
return
self.current_execution = timeit.default_timer()
self.calls += 1
def stop_cumulative(self):
if Stopwatch.hide:
return
assert self.current_execution != 0, "'start_cumulative' must be called before 'stop_cumulative'"
elapsed = timeit.default_timer() - self.current_execution
self.current_execution = 0
self.time += elapsed
def print(self):
if not Stopwatch.hide:
print(self)
def __repr__(self) -> str:
return f"{self.name} - {self.time}s - {self.calls}"
#input()
#heights = list(map(int, input().split()))
from cases import heights
import bisect
Stopwatch.hide = True
a = Stopwatch("height_time")
b = Stopwatch("find_children")
c = Stopwatch("total")
d = Stopwatch("bisect")
e = Stopwatch("pop")
c.start_cumulative()
def build_height_map(heights):
map = {}
highest_balloon = heights[0]
for index, height in enumerate(heights):
if height > highest_balloon:
highest_balloon = height
if map.get(height):
map[height].append(index)
continue
map[height] = [index]
for layers in map.values():
layers.sort()
return map, highest_balloon
arrows = 0
a.start_cumulative()
heights_map, start_height = build_height_map(heights)
a.stop_cumulative()
a.print()
current_height = start_height
while current_height != 0:
height_layer = heights_map.get(current_height)
if not height_layer:
current_height -= 1
continue
index = height_layer.pop(0)
arrows += 1
start_height = current_height
start_height -= 1
might_pop_list = heights_map.get(start_height)
b.start_cumulative()
while might_pop_list:
d.start_cumulative()
index_index = bisect.bisect_left(might_pop_list, index)
d.stop_cumulative()
if index_index == len(might_pop_list):
break
might_pop_index = might_pop_list[index_index]
e.start_cumulative()
might_pop_list.pop(index_index)
e.stop_cumulative()
start_height -= 1
might_pop_list = heights_map.get(start_height)
index = might_pop_index
b.stop_cumulative()
b.print()
print(arrows)
c.stop_cumulative()
d.print()
c.print()
e.print()
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
5 3 2 1 5 4