欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

1014 Waiting in Line

程序员文章站 2024-03-18 11:56:10
...

Java
Scanner 最后一个测试点超时

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

/**
 * Main
 */
public class Main {
    static int m, n, k, q;
    static final int MAX = 99999999;
    static final int END = 540;

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] l1 = reader.readLine().split(" ");
        // 窗口数量
        n = Integer.parseInt(l1[0]);
        // 每个窗口最大人数
        m = Integer.parseInt(l1[1]);
        // 顾客人数
        k = Integer.parseInt(l1[2]);
        // 查询此时
        q = Integer.parseInt(l1[3]);

        // 每个顾客的办事时间
        int[] prossesTime = new int[1005];
        // 每个窗口的当前开始办事的时间
        int[] inTime = new int[n];
        // 每个顾客的离开时间
        int[] outTime = new int[1005];
        // 窗口队列
        Queue<Integer>[] queue = new LinkedList[n];

        String[] l2 = reader.readLine().split(" ");
        for (int i = 0; i < k; i++) {
            prossesTime[i] = Integer.parseInt(l2[i]);
        }

        for (int i = 0; i < n; i++) {
            queue[i] = new LinkedList<>();
        }

        int index = 0;
        // 第一波能进去黄线区的顾客
        for (int i = 0; i < m * n && i < k; i++) {
            queue[index].add(i);
            outTime[i] = inTime[index] + prossesTime[i];
            inTime[index] = outTime[i];
            index = (index + 1) % n;
        }

        // 第一波未能进去黄线区的顾客
        for (int i = m * n; i < k; i++) {
            // 寻找最先出队的窗口号index
            int min = MAX;
            for (int j = 0; j < n; j++) {
                int top = queue[j].peek();
                if (outTime[top] < min) {
                    min = outTime[top];
                    index = j;
                }
            }

            queue[index].poll();
            queue[index].add(i);

            outTime[i] = inTime[index] + prossesTime[i];
            inTime[index] = outTime[i];
        }

        // 查询
        String[] l3 = reader.readLine().split(" ");
        for (int i = 0; i < q; i++) {
            int id = 0;
            id = Integer.parseInt(l3[i]) - 1;
            if (outTime[id] - prossesTime[id] >= END) {
                System.out.printf("%s\n", "Sorry");
            } else {
                System.out.printf("%02d:%02d\n", outTime[id] / 60 + 8, outTime[id] % 60);
            }

        }

        reader.close();
    }

}