Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fef26fb

Browse files
solves min average waiting time in python
1 parent 38ce8a8 commit fef26fb

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from heapq import heappush, heappop
2+
3+
tasks = []
4+
5+
N = int(input())
6+
7+
for _ in range(N):
8+
arrival, cook_time = map(int, input().split())
9+
tasks.append((arrival, cook_time))
10+
11+
tasks.sort(reverse=True)
12+
13+
pq = []
14+
time_waiting = 0
15+
current_time = 0
16+
17+
while tasks or pq:
18+
while tasks and tasks[-1][0] <= current_time:
19+
heappush(pq, tasks.pop()[::-1])
20+
if pq:
21+
current_task = heappop(pq)
22+
current_time += current_task[0]
23+
time_waiting += current_time - current_task[1]
24+
else:
25+
heappush(pq, tasks.pop()[::-1])
26+
current_time = pq[0][1]
27+
28+
print(time_waiting // N)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /