-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsolution.py
More file actions
32 lines (24 loc) · 846 Bytes
/
solution.py
File metadata and controls
32 lines (24 loc) · 846 Bytes
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
"""
Complejidad Temporal: O(n^2)
"""
def expected_value(c: list[int]) -> list[float]:
n = len(c)
# Ordenar los cofres en orden descendente
c.sort(reverse=True) # * O(n log n)
result = []
# Cálculo de las respuestas para cada k
for k in range(1, n + 1): # * O(n) iteraciones
total_gain = 0
j = 0
for i in range(0, n, k): # * O(n) iteraciones
interval_sum = sum(c[i : min(n, i + k)])
total_gain += j * interval_sum
j += 1
expected_gain = total_gain / n
result.append(expected_gain)
# print(f"Ganancia esperada para k = {k}: {total_gain}/{n} = {expected_gain}")
# * O(n log n) + O(n) * O(n) = O(n^2) en total
return result
if __name__ == "__main__":
c = list(map(int, input().split()))
result = expected_value(c)