PAT 1014 Waiting in Line
生活随笔
收集整理的這篇文章主要介紹了
PAT 1014 Waiting in Line
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
1: 全錯,
修改下句增加queryCnt<q 的判斷:
while(queryCnt?<?q?&&?cusSeq?<=?k){//need?query???&&??customer?is?crossing?the?yellow?line2:對了2個case(k < n*m的情況),得7分;
修改判斷“Sorry”的條件:
//if(curEndTime?<=?540){ //end?transaction?before?17:00 if(lastEndTime?<=?540){ //start?transaction?before?17:003:又對了1個case, 得10分;
再修改判斷"Sorry"條件:
if(lastEndTime?<?540){????????????????//start?transaction?before?17:004:又一個,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?testScanner?in?=?new?Scanner(System.in); //for?online?judgeint?n,?m?,?k,?q;//window?num,?capacity,?customer?num,?query?numList<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?itfor(i?=?0;?i?<?n;?i++){//?window?:?0,1,2,...,n-1cusQue.add(new?LinkedList<Integer>());}int?cusSeq?=?1;//customer?1,2,....,kString?[]?strRes?=?new?String[k+1];//end?time?for?customer?1,2,...,kwhile(?cusSeq?<=?k){//in?yellow?line?there?are?n*m?customersStringBuilder?res?=?new?StringBuilder();int?winSeq?=?0?;//which?window?should?the?customer?wait?at,?winSeq:?0,1,2,...,n-1if(cusSeq?<=?n*m){//first?n*m?customers?no?need?to?judgewinSeq?=?(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?queuefor(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?customerif(cusSeq?>?n*m){//after?n*m?customerstoAddLs.poll();//remove?the?head?after?inserting?}if(lastEndTime?<?540){ //start?transaction?before?17:00int?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());}}轉載于:https://my.oschina.net/kaneiqi/blog/289248
總結
以上是生活随笔為你收集整理的PAT 1014 Waiting in Line的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 文件夹的共享
- 下一篇: Ubuntu 14.04 / 13.10