PTA-1014——Waiting in Line
題目:
Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are:
- The space inside the yellow line in front of each window is enough to contain a line with M customers. Hence when all the N lines are full, all the customers after (and including) the (st one will have to wait in a line behind the yellow line.
- Each customer will choose the shortest line to wait in when crossing the yellow line. If there are two or more lines with the same length, the customer will always choose the window with the smallest number.
- Customer?i?? will take T?i?? minutes to have his/her transaction processed.
- The first N customers are assumed to be served at 8:00am.
Now given the processing time of each customer, you are supposed to tell the exact time at which a customer has his/her business done.
For example, suppose that a bank has 2 windows and each window may have 2 custmers waiting inside the yellow line. There are 5 customers waiting with transactions taking 1, 2, 6, 4 and 3 minutes, respectively. At 08:00 in the morning, customer?1?? is served at window?1?? while customer?2?? is served at window?2??. Customer?3?? will wait in front of window?1?? and customer?4?? will wait in front of window?2??. Customer?5?? will wait behind the yellow line.
At 08:01, customer?1?? is done and customer?5?? enters the line in front of window?1?? since that line seems shorter now. Customer?2?? will leave at 08:02, customer?4?? at 08:06, customer?3?? at 08:07, and finally customer?5?? at 08:10.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 4 positive integers: N (≤, number of windows), M (≤, the maximum capacity of each line inside the yellow line), K (≤, number of customers), and Q (≤, number of customer queries).
The next line contains K positive integers, which are the processing time of the K customers.
The last line contains Q positive integers, which represent the customers who are asking about the time they can have their transactions done. The customers are numbered from 1 to K.
Output Specification:
For each of the Q customers, print in one line the time at which his/her transaction is finished, in the format HH:MM
where HH
is in [08, 17] and MM
is in [00, 59]. Note that since the bank is closed everyday after 17:00, for those customers who cannot be served before 17:00, you must output Sorry
instead.
Sample Input:
2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7
Sample Output:
08:07
08:06
08:10
17:00
Sorry
分析:
隊列。
一個註意點:服務開始時間>=17:00才不服務,而不是結束時間.
代碼:
1 #include<iostream> 2 #include<queue> 3 #include<cstring> 4 using namespace std; 5 int n,m,k,q; 6 int need[1001]; //每個客戶需要的時間 7 int finish[1001]; //每個客戶的結束時間 8 queue<int> win[21]; //窗口的隊伍 9 int now[1001]; //每個窗口的時間 10 int main(){ 11 cin>>n>>m>>k>>q; 12 for(int i=1;i<=k;i++){ 13 cin>>need[i]; 14 } 15 memset(now,0,sizeof(now)); 16 for(int i=1;i<=k;i++){ //所有的客戶都安排進窗口 17 if(i<=n){ 18 win[i].push(i); 19 now[i]=need[i]; //這個窗口第一個客戶,當前時間設置為第一個客戶結束的時間 20 }else if(i<=n*m){ //黃線內有空位 21 win[(i-1)%n+1].push(i); 22 }else{ //黃線外依次替換 23 int earlist=1000000; //下一個最早結束的時間 24 int point=0; //結束的客戶所在的窗口位置 25 for(int j=1;j<=n;j++){ //找出最早結束的窗口 26 if(now[j]<earlist){ 27 earlist=now[j]; 28 point=j; 29 } 30 } 31 int customer=win[point].front(); //結束的客戶編號 32 win[point].pop(); //刪除服務結束的客戶 33 win[point].push(i); //後面的客戶進入黃線內 34 finish[customer]=now[point]; //記錄客戶服務結束的時間 35 now[point]+=need[win[point].front()]; //時間挪到該窗口下一個客戶結束的時間 36 } 37 } 38 for(int i=1;i<=n;i++){ //最後剩下的黃線內的人進行清空 39 while(win[i].size()>0){ 40 int customer=win[i].front(); 41 win[i].pop(); 42 finish[customer]=now[i]; 43 now[i]+=need[win[i].front()]; 44 } 45 } 46 for(int i=0;i<q;i++){ 47 int query; 48 cin>>query; 49 if(finish[query]-need[query]>=540){ //若開始辦理的時間大於等於540分鐘,即超過了17:00 50 cout<<"Sorry"<<endl; 51 }else{ //否則輸出時間 52 int a=8+finish[query]/60; 53 int b=finish[query]%60; 54 if(a<10){ 55 cout<<"0"<<a<<":"; 56 }else{ 57 cout<<a<<":"; 58 } 59 if(b<10){ 60 cout<<"0"<<b<<endl; 61 }else{ 62 cout<<b<<endl; 63 } 64 } 65 } 66 return 0; 67 }
PTA-1014——Waiting in Line