感觉题目有点长啊其实就是一个贪心+优先队列的问题。第一步我们将所有的点按照完成所需要的时间排序这是显然的时间短的先完成肯定更好。第二步我们需要维护一个优先队列这个优先队列的作用就是存储等待的顾客。我们将等待时间最短的顾客放到队首然后我们每次完成一个订单的时候就将队首的顾客弹出如果这个顾客等待的时间超过了他的等待周期那么我们就将他的分数降低30分然后将他重新加入队列中。第三步我们需要维护一个总分数当
代码实现如下:
import heapq
n, m, k = map(int, input().split())
orders = []
for i in range(n):
s, t, d = map(int, input().split())
orders.append((s, t, d))
orders.sort(key=lambda x: x[1])
wait_queue = []
score = 0
for i in range(n):
s, t, d = orders[i]
if len(wait_queue) < m:
heapq.heappush(wait_queue, (s+d, s, t))
else:
_, start, end = heapq.heappop(wait_queue)
if s > start + k:
score -= 30
score += t
heapq.heappush(wait_queue, (max(s, start)+d, s, t))
while wait_queue:
_, start, end = heapq.heappop(wait_queue)
if k < end - start:
score -= 30
print(score)
时间复杂度为 $O(n\log n)$,空间复杂度为 $O(m)$。
原文地址: https://www.cveoy.top/t/topic/8I5 著作权归作者所有。请勿转载和采集!