第20645题 单选题
带截止时间任务调度最大利润贪心实现代码填空

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

struct Task {
    int deadline;    //截止时间
    int profit;      //利润
};

void sortByProfit(vector<Task>& tasks) {
    sort(tasks.begin(), tasks.end(),
        [](const Task& a, const Task& b) {
            return a.profit > b.profit;
        });
}

int maxProfit(vector<Task>& tasks) {
    sortByProfit(tasks);

    int maxTime = 0;
    for (auto& t : tasks) {
        maxTime = max(maxTime, t.deadline);
    }

    vector<bool> slot(maxTime + 1, false);
    int totalProfit = 0;

    for (auto& task : tasks) {
        for (int t = task.deadline; t >= 1; t--) {
            if (!slot[t]) {

                __________________________ //在此处填入代码
                break;
            }
        }
    }

    return totalProfit;
}
{{ option.label }}
子题{{ index + 1 }} {{ child.type_label }}
{{ option.label }}
✓ 正确 ◐ 部分正确 ✗ 错误
程序运行统计
暂无判题统计
提交{{ questionInfo.stats ? questionInfo.stats.submit_count : 0 }}次 正确率{{ statsAccuracy }}%
答案解析