- 考察双向链表
-
import java.io.IOException; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.util.LinkedList; public class Main { @SuppressWarnings("uncheck") public static void main(String[] args) throws IOException { StreamTokenizer st = new StreamTokenizer(new InputStreamReader(System.in)); int n, m, k, q; st.nextToken(); n = (int) st.nval; st.nextToken(); m = (int) st.nval; st.nextToken(); k = (int) st.nval; st.nextToken(); q = (int) st.nval; LinkedList<int[]>[] queue = new LinkedList[n]; for (int i = 0; i < n; i++) { queue[i] = new LinkedList<>(); } int[][] time = new int[k + 1][2]; int[] cost = new int[k]; for (int i = 0; i < k; i++) { st.nextToken(); int v = (int) st.nval; cost[i] = v; if (i < m * n) { if (queue[i % n].isEmpty()) { queue[i % n].add(new int[]{i, 0, v}); time[i + 1] = new int[]{0, v}; continue; } int[] last = queue[i % n].getLast(); int finishedtime = last[2]; queue[i % n].add(new int[]{i, finishedtime, finishedtime + v}); time[i + 1] = new int[]{finishedtime, finishedtime + v}; } else { int min = 1000_000_00; int minindex = -1; for (int j = 0; j < n; j++) { int[] first = queue[j].peekFirst(); if (min > first[2]) { min = first[2]; minindex = j; } } int[] last = queue[minindex].getLast(); int finishedtime = last[2]; queue[minindex].pollFirst(); queue[minindex].add(new int[]{i, finishedtime, finishedtime + v}); time[i + 1] = new int[]{finishedtime, finishedtime + v}; } } int max = 540; for (int i = 1; i <= q; i++) { st.nextToken(); int index = (int) st.nval; if (time[index][0] < max) { System.out.println(format(time[index][1])); } else { System.out.println("Sorry"); } } } public static String format(int time) { int x = time / 60 + 8; int y = time % 60; StringBuilder sb = new StringBuilder(); if (x < 10) { sb.append("0").append(x); } else { sb.append(x); } sb.append(":"); if (y < 10) { sb.append("0").append(y); } else { sb.append(y); } return sb.toString(); } }