//2、多cpu多任务调度 public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); int K = scanner.nextInt(); int[] cpus = new int[N]; //优先队列排序:先按照优先级排序(从小到大),然后按照时间排序(从大到小) PriorityQueue<Node> queue = new PriorityQueue<>(new Comparator<Node>() { @Override public int compare(Node o1, Node o2) { if (o1.level == o2.level){ return o2.time - o1.time; } return o1.level - o2.level; } }); for (int i = 0; i < K; i++) { Node node = new Node(); node.time = scanner.nextInt(); node.level = scanner.nextInt(); queue.add(node); } //在0时刻,向cpu中写入各线程 for (int i = 0; i < N; i++) { cpus[i] = queue.poll().time; } int time = 1;//记录一个时刻 while (!queue.isEmpty()){ //遍历数组,查看是否有执行完的任务 for (int i = 0; i < N; i++) { if (time == cpus[i]){ cpus[i] += queue.poll().time; } } time++; } //寻找max并返回 int max = 0; for (int i = 0; i < N; i++) { max = Math.max(max,cpus[i]); } System.out.println(max); }
全部评论
(1) 回帖