华为7.21笔试review 第二题

 //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);
    }

#华为机试##华为##笔试题目#
全部评论
可以
点赞 回复 分享
发布于 2021-07-31 17:27

相关推荐

每晚夜里独自颤抖:这个在牛客不是老熟人了吗
点赞 评论 收藏
分享
06-12 17:46
门头沟学院 Java
运营你豪哥:来说重点: ​1.项目前置,时间倒序。​​ 2.​项目描述强化结果与量化效果(STAR原则里的R)。​​ ​3.个人技能精炼,明确掌握程度,突出核心。​​ ​4.增加强有力开头的个人总结部分。​​ 5.​优化教育背景(成绩排名)、合并奖项与活动。​​
点赞 评论 收藏
分享
评论
1
12
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务