1: 全错,
修改下句增加queryCnt<q 的判断:
while(queryCnt < q && cusSeq <= k){//need query && customer is crossing the yellow line
2:对了2个case(k < n*m的情况),得7分;
修改判断“Sorry”的条件:
//if(curEndTime <= 540){ //end transaction before 17:00 if(lastEndTime <= 540){ //start transaction before 17:00
3:又对了1个case, 得10分;
再修改判断"Sorry"条件:
if(lastEndTime < 540){ //start transaction before 17:00
4:又一个,14分;
5: 终于发现,题目要query的customer不一定会按照递增顺序给出,所以需要在模拟过程中把所有的customer 的结束time存下来,再按照query的次序依次打出,AC了,耗了了我快6个小时
PAT上不支持中文注释 import java.util.Scanner; import java.io.File; import java.util.List; import java.util.ArrayList; import java.util.LinkedList; public class Main{ public static void main(String [] args) throws Exception{ // Scanner in = new Scanner(new File("./in.txt")); //for local test Scanner in = new Scanner(System.in); //for online judge int n, m , k, q;//window num, capacity, customer num, query num List<Integer> proTime = new ArrayList<Integer>(); List<Integer> query = new ArrayList<Integer>(); n = in.nextInt(); m = in.nextInt(); k = in.nextInt(); q = in.nextInt(); int i; for(i = 0; i < k; i++){ int tmp = in.nextInt(); proTime.add(tmp); } for(i = 0; i < q; i++){ int tmp = in.nextInt(); query.add(tmp); } List<LinkedList<Integer>> cusQue = new ArrayList<LinkedList<Integer>>();//simulate the queue using linkedlist //one queue for each Window, storing the done time of each customer in the queue //add customer beyond the yellow line one by one //the customer beyond the yellow line join the queue with the smallest queue head(earliest done time) //so find the queue with smallest queue head, take out it and output it if query it for(i = 0; i < n; i++){// window : 0,1,2,...,n-1 cusQue.add(new LinkedList<Integer>()); } int cusSeq = 1;//customer 1,2,....,k String [] strRes = new String[k+1];//end time for customer 1,2,...,k while( cusSeq <= k){//in yellow line there are n*m customers StringBuilder res = new StringBuilder(); int winSeq = 0 ;//which window should the customer wait at, winSeq: 0,1,2,...,n-1 if(cusSeq <= n*m){//first n*m customers no need to judge winSeq = (cusSeq - 1)%n;//cusSeq mapping to window number: 0,1,2,...,n-1 }else{ int j, earliestPos = 0, earliestTime = cusQue.get(0).peek();//find the smallest head of each window queue for(j = 0 ; j < n; j++ ){ int leaveTime = cusQue.get(j).peek(); if(leaveTime < earliestTime){ earliestPos = j; earliestTime = leaveTime; } } winSeq = earliestPos; } LinkedList<Integer> toAddLs = cusQue.get(winSeq); int lastEndTime = 0; if(toAddLs.size() > 0){ lastEndTime = toAddLs.getLast();//the tail time } int curEndTime = lastEndTime + proTime.get(cusSeq - 1); toAddLs.add(curEndTime); //add the done time of current customer if(cusSeq > n*m){//after n*m customers toAddLs.poll();//remove the head after inserting } if(lastEndTime < 540){ //start transaction before 17:00 int hour = 8 + curEndTime/60; int min = curEndTime%60; String strTime = String.format("%02d", hour)+":"+String.format("%02d", min);// the fucking format res.append(strTime); }else{ res.append("Sorry"); } strRes[cusSeq] = res.toString(); cusSeq++; } StringBuilder sb = new StringBuilder(); for(int seq:query){ //the sequence in query may not be increasing order !!!! sb.append(strRes[seq]+"\n"); } sb.deleteCharAt(sb.length()-1);//remove the last "\n" System.out.println(sb.toString()); } }