【今日头条2017内推】调度问题

这里写图片描述

解题思路

首先对所有的任务按照 提出时间,优先级,所需时间进行排序,即提出时间相等时按照优先级排序,优先级也相等时按照所需时间进行排序。

可以维护一个长度为M的堆,M为程序员员的数量,这个堆中存储的值是程序员完成某个任务的完成时间点,依次取排序后的任务,然后在堆中取一个程序员,取堆顶的那个,这个任务的完成时间即为提出任务时间和堆中取出的时间的较大值加上任务所需时间。然后再将这个时间更新进堆中。
需要注意的是输出的时候是按照输入的任务顺序进行输出的,因而需要保存任务的编号,然后按照编号由小到大的顺序输出结果。

这个题并没有在线上测试,线下写的,不知道有没得问题。。。。


import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;

class Task implements Comparable<Task> {
    int startTime;
    int needTime;
    int level;
    int sequence;

    @Override
    public int compareTo(Task o) {
        // TODO Auto-generated method stub
        if (startTime > o.startTime) {
            return 1;
        } else if (startTime < o.startTime) {
            return -1;
        } else {
            if (level > o.level) {
                return 1;
            } else if (level < o.level)
                return -1;
            else {
                if (needTime > o.needTime) {
                    return 1;
                } else if (needTime < o.needTime)
                    return -1;
                else {
                    return 0;
                }
            }
        }
    }
}

class FinishTime implements Comparable<FinishTime> {
    int sequence;
    int time;

    @Override
    public int compareTo(FinishTime o) {
        // TODO Auto-generated method stub
        return this.sequence > o.sequence ? 1 : (this.sequence < o.sequence ? -1 : 0);
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int M = sc.nextInt();
        int P = sc.nextInt();
        Task[] task = new Task[P];

        for (int i = 0; i < P; i++) {
            Task t = new Task();
            sc.nextInt();
            t.startTime = sc.nextInt();
            t.level = sc.nextInt();
            t.needTime = sc.nextInt();
            t.sequence = i;
            task[i] = t;
        }
        Arrays.sort(task);

        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(M);
        for (int i = 0; i < M; i++)
            priorityQueue.add(0);
        int index = 0;
        FinishTime[] result = new FinishTime[P];
        while (index < P) {
            int time = priorityQueue.poll();
            int finishTime = Math.max(task[index].startTime, time) + task[index].needTime;
            priorityQueue.add(finishTime);
            FinishTime t = new FinishTime();
            t.sequence = task[index].sequence;
            t.time = finishTime;
            result[index]=t;
            index++;
        }
        Arrays.sort(result);
        for(int i=0;i<P;i++)
            System.out.println(result[i].time);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值