【贪心算法】Job Schedule

Description:

There are nn(1<=n<=1051<=n<=105) distinct jobs, labeled J1,J2,…,JnJ1,J2,…,Jn, which can be performed completely independently of one another. Each jop consists of two stages: first it needs to be preprocessed on the supercomputer, and then it needs to be finished on one of the PCs. Let’s say that job JiJi needs pipi(int) seconds of time on the supercomputer, followed by fifi(int) seconds of time on a PC. Since there are at least nn PCs available on the premises, the finishing of the jobs can be performed on PCs at the same time. However, the supercomputer can only work on a single job a time without any interruption. For every job, as soon as the preprocessing is done on the supercomputer, it can be handed off to a PC for finishing. Let’s say that a schedule is an ordering of the jobs for the supercomputer, and the completion time of the schedule is the earlist time at which all jobs have finished processing on the PCs. Give a polynomial-time algorithm that finds a schedule with as small a completion time as possible.

Input:

The input will be two lines, the first line means p1,p2,…,pnp1,p2,…,pn and the second line means f1,f2,…,fnf1,f2,…,fn, separated by spaces.

Output:

The output will be a minimum time.

Sample Input:

1000 2000 3000
3000 1000 2000

Sample Output:

7000

原题地址:

UOJ#39

解题思路:

一开始看到本题,很容易联想到母题,流水作业调度的Johnson法则,如下图所示,原题描述以及代码见此
在这里插入图片描述
但本题是在母题(流水作业最优调度)的情况下修改了一个条件,即第二台机器的数量和队列中作业的数量相等,该条件等价于对第二台机器的数量不限量,也就是说我们要使得总耗时最少,总方向和流水作业调度一样,即机器1总是在工作,机器2的空闲时间最少

细微不同在于我们在多步决策过程中,直接可以选择未执行作业队列仅在机器2中最耗时的作业作为当前局部最优解,因此该问题显现出了贪心选择性质。所以我们可以仅对作业中在机器2上的耗时做降序排序,得出的最终耗时即为最短时间,也就是最优解

注:由于机器2数量不限量这个条件的改变,可以证明作业调度在机器2上耗时排序与在机器1上的耗时排序无关,因为不论按什么规则排序,机器1总是在忙碌中无空闲,不影响最后计算总耗时。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct JobUnit{
    long T1;  // 作业在第1个机器上的时间
    long T2;  // 作业在第2个机器上的时间
};
class Solution{
    static bool cmp(JobUnit a, JobUnit b){
        return a.T2 > b.T2;
    }
public:
    long jobSchedule(vector<JobUnit>& job, int n){
        // 1.仅对T2作降序排序
        sort(job.begin(), job.end(), cmp);
        // 2.计算最低总耗时
        for(int i = 0;i < n;i++){
            minTime += job[i].T1;
            if(i > 0)
                // 1)之前所有任务在第二台机子的累计剩余、2)前一个任务在第二台机子上的剩余和3)当前任务在第二台机子上的耗时取最大
                remainTime = max(remainTime - job[i].T1, max((job[i-1].T2 - job[i].T1), job[i].T2));
            else
                remainTime = job[i].T2;
        }
        return minTime + remainTime;
    }
private:
    long minTime = 0, remainTime;
};
int main(){
    int n;
    JobUnit jobunit;
    vector<JobUnit> job;
    while(cin.peek() != '\n'){
        scanf("%ld", &jobunit.T1);
        job.push_back(jobunit);
    }
    n = job.size();
    for(int i = 0;i < n;i++)
        scanf("%ld", &job[i].T2);
    Solution sol;
    cout<<sol.jobSchedule(job, n)<<endl;
    return 0;
}
}

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SL_World

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值