给定有 n 个任务,每个任务有截止时间和利润,每个任务耗时 1 个时间单位、必须在截止时间前完成,且每个时间槽最多做 1 个任务。为了在规定时间内获得最大利润,可以采用贪心策略,即按利润从高到低排序,尽量安排,则横线处应填写( )。

fb852111 单选题 共 个子题
class Task:
    def __init__(self, deadline, profit):
        self.deadline = deadline
        self.profit = profit

def sort_by_profit(tasks):
    tasks.sort(key=lambda x: x.profit, reverse=True)

def max_profit(tasks):
    sort_by_profit(tasks)
    max_time = 0
    for task in tasks:
        if task.deadline > max_time:
            max_time = task.deadline

    slot = [False] * (max_time + 1)
    total_profit = 0

    for task in tasks:
        t = task.deadline
        while t >= 1:
            if not slot[t]:
                ———————————————————————————
            t -= 1

    return total_profit
A
slot[t] = True
total_profit += task.profit
break
B
slot[t] = True
total_profit += task.profit
C
slot[t] = False
total_profit += task.profit
break
D
slot[t] = True
total_profit -= task.profit
正在加载题目数据…
已答 /
子题
该题型需在试卷/作业中作答,单题练习暂不支持
答案解析
讨论
发布
登录后参与讨论 立即登录
发布讨论需先绑定并验证手机号 去绑定手机号
加载中…
我
我
回复 @
回复 @
发送
还没有人讨论,来发表第一条吧