forked from marcosfede/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bathroom_stalls.py
56 lines (46 loc) · 1.33 KB
/
bathroom_stalls.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import heapq
from collections import Counter
from decimal import Decimal
from math import ceil, floor
# python heapq is a min heap so we need to invert the signs
# O(k*log(k))
def solve2(n, k):
heap = [-1 * n]
for step in range(1, k + 1):
x = -1 * heapq.heappop(heap)
x0 = ceil((x - 1) / 2)
x1 = floor((x - 1) / 2)
if step == k:
return x0, x1
heapq.heappush(heap, -1 * x0)
heapq.heappush(heap, -1 * x1)
# using decimal cause of float precision
# there can only be 4 elements in the set or counter at the same time
# so the max op is bounded by constant time. steps are the number of times
# it goes through the loop which is O(log(n))
def solve(n, k):
s = {Decimal(n)}
counter = Counter()
counter[n] = 1
p = 0
while True:
x = max(s)
x0 = Decimal(ceil((x - 1) / 2))
x1 = Decimal(floor((x - 1) / 2))
cx = counter[x]
p += cx
if p >= k:
return int(x0), int(x1)
s.remove(x)
counter.pop(x)
s.add(x0)
s.add(x1)
counter[x0] += cx
counter[x1] += cx
def read_input():
ncases = int(input())
for case in range(1, ncases + 1):
n, k = map(int, input().split(" "))
mx, mn = solve(n, k)
print(f'CASE #{case}: {mx} {mn}')
read_input()