题解 | #任务调度#

任务调度

http://www.nowcoder.com/practice/f7efb182b285403a84c10ee4e6f6075a

python 双堆

import heapq

n,m,p = map(int,input().split())

idea = [[] for _ in range(3005)]
for i in range(p):
    a,b,c,d = map(int,input().split())
    t = [c,d,b,d,a,i]  # 题目要求的优先级顺序
    idea[b].append(t)

h1,h2 = [],[]  # h1是程序员,h2是任务堆
ans = [0]*p
for i in range(3005):
    # 释放已经结束的程序员
    while h1 and h1[0]<=i:
        heapq.heappop(h1)
    # 处理当前任务堆中的元素
    for x in idea[i]:
        heapq.heappush(h2, x)
    left = m-len(h1)
    while left>0 and h2:
        t = heapq.heappop(h2)
        heapq.heappush(h1,i+t[-3])
        ans[t[-1]] = i+t[-3]
        left = m-len(h1)
for i in range(3005,6005):
    while h1 and h1[0]<=i:
        heapq.heappop(h1)
    left = m-len(h1)
    while left>0 and h2:
        t = heapq.heappop(h2)
        heapq.heappush(h1,i+t[-3])
        ans[t[-1]] = i+t[-3]
        left = m-len(h1)
    if not h2:
        break



for x in ans:
    print(x)
全部评论

相关推荐

黑皮白袜臭脚体育生:春节刚过就开卷吗?哈基馆,你这家伙......
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客企业服务