今日头条2018笔试编程题-第三题 任务调度

import java.util.*;
import java.lang.*;

/*
input
2 2 5
1 1 1 2
1 2 1 1
1 3 2 2
2 1 1 2
2 3 5 5
output
3
4
5
3
9
*/
public class toutiaoC {

	public static void solution(int numPM, int numProgramer, int numTask, Task[] tasks) {
		//任务状态:新建->就绪->执行
		
		Queue<Task> BornPQ = new PriorityQueue<Task>(numTask, new BornComparator()); //新建队列
		Queue<Task> ReadyPQ = new PriorityQueue<Task>(numTask, new ReadyComparator()); //就绪队列
		Queue<Task> RunningPQ = new PriorityQueue<Task>(numProgramer, new RunningComparator()); //运行队列
		int[] retFinishTimes = new int [numTask];


		//init BornPQ
		for (int i = 0; i < numTask; i++) {
			BornPQ.offer( tasks[i] );
		}

		//init RunningPQ
		for (int i = 0; i < numProgramer && i < numTask; i++) {
			Task curTask = BornPQ.poll();
			// curTask.startTime = curTask.bornTime;
			// curTask.finishTime = curTask.startTime + curTask.taskTime;
			curTask.setStartTime(curTask.bornTime);
			RunningPQ.offer(curTask);
		}

		while( !RunningPQ.isEmpty() ) {
			Task finishTask = RunningPQ.poll();
			int curTime = finishTask.finishTime;
			retFinishTimes[finishTask.index] = curTime;

			//update BornPQ and ReadyPQ: BornPQ.poll -> ReadyPQ.offer;
			//update BornPQ and ReadyPQ: ReadypQ.poll -> RunningPQ.offer;
			while( !BornPQ.isEmpty() && BornPQ.peek().bornTime <= curTime ) {
				ReadyPQ.offer( BornPQ.poll() );
			}
			while( RunningPQ.size() < numProgramer && !ReadyPQ.isEmpty() ) {
				Task runningTask = ReadyPQ.poll();
				// runningTask.startTime = curTime;
				// runningTask.finishTime = runningTask.startTime + runningTask.taskTime;
				runningTask.setStartTime(curTime);
				RunningPQ.offer(runningTask);
			}
		}

		//result
		for (int i = 0; i < numTask; i++) {
			System.out.println( retFinishTimes[i] );
		}
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int numPM = sc.nextInt();
		int numProgramer = sc.nextInt();
		int numTask = sc.nextInt();
		Task[] tasks = new Task[numTask];
		for (int i = 0; i < numTask; i++) {
			int PMId = sc.nextInt();
			int bornTime = sc.nextInt();
			int priority = sc.nextInt();
			int taskTime = sc.nextInt();
			tasks[i] = new Task(i, PMId, bornTime, priority, taskTime);
		}

		solution(numPM, numProgramer, numTask, tasks);
	}
}

//新建状态
class BornComparator implements Comparator<Task> {
	//bornTime 递增 @Override public int compare(Task o1, Task o2) {
		return o1.bornTime - o2.bornTime;
	}
}

//就绪状态
class ReadyComparator implements Comparator<Task> {
	//priority -> taskTime -> bornTime -> PMId @Override public int compare(Task o1, Task o2) {
		if (o1.priority != o2.priority) {
			return o1.priority - o2.priority;
		}
		else if ( o1.taskTime != o2.taskTime ) {
			return o1.taskTime - o2.taskTime;
		}
		else if ( o1.bornTime != o2.bornTime ) {
			return o1.bornTime - o2.bornTime;
		}
		else {
			return o1.PMId - o2.PMId;
		}
	}
}

//运行状态
class RunningComparator implements Comparator<Task> {
	//finishTime 递增 @Override public int compare(Task o1, Task o2) {
		return o1.finishTime - o2.finishTime;
	}
}


class Task {
	public int index;
	public int PMId;
	public int bornTime;
	public int priority;
	public int taskTime;

	public int startTime;
	public int finishTime;

	public Task(int index, int PMId, int bornTime, int priority, int taskTime) {
		this.index = index;
		this.PMId = PMId;
		this.bornTime = bornTime;
		this.priority = priority;
		this.taskTime = taskTime;
	}

	public void setStartTime(int startTime) {
		this.startTime = startTime;
		this.finishTime = this.startTime + this.taskTime;
	}
}


#字节跳动#
全部评论
优先级序号越小优先级越高??
点赞 回复 分享
发布于 2017-08-23 15:34
样例里的1 1 1 2和2 1 1 2这两条idea优先级,提出时间和需求时间一模一样,就是pm序号不一样。但题目里说没有pm在同一时刻提出完全一样的idea,是不是这个题目有问题
点赞 回复 分享
发布于 2017-08-23 16:28
谢谢分享! 不过我觉得题目意思和给出的测试样例有点矛盾。 题目说的排列顺序是优先等级->所需时间->提出时间->pm序号,也就是你程序中的priority -> taskTime -> bornTime -> PMId。 但是如果按这个排,测试样例中, input 2 2 5 1 1 1 2 1 2 1 1 1 3 2 2 2 1 1 2 2 3 5 5 程序员应该先执行第二个idea:1 2 1 1,因为它的优先级为1,且所需时间最短。理论上输出应该为3,然而样例的答案为4,排列顺序变成了priority -> bornTime -> taskTime -> PMId。
点赞 回复 分享
发布于 2017-08-28 19:32
头条内推码4EXU9YH 投递链接:https://job.toutiao.com/campus/
点赞 回复 分享
发布于 2017-09-06 08:06

相关推荐

helloWord大王:这时候hr来个转人工我就真绷不住了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务