A1014. 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 (NM+1)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 customers 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, customer1 is served at window1while customer2 is served at window2. Customer3 will wait in front of window1 and customer4 will wait in front of window2. Customer5 will wait behind the yellow line.
At 08:01, customer1 is done and customer5 enters the line in front of window1 since that line seems shorter now. Customer2 will leave at 08:02, customer4 at 08:06, customer3 at 08:07, and finally customer5 at 08:10.
Input
Each input file contains one test case. Each case starts with a line containing 4 positive integers: N (<=20, number of windows), M (<=10, the maximum capacity of each line inside the yellow line), K (<=1000, number of customers), and Q (<=1000, 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
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
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<math.h> 5 #include<queue> 6 using namespace std; 7 typedef struct{ 8 queue<int> qu; 9 int nowT; 10 int endFirst; 11 }info; 12 info window[21]; 13 int N, M, K, Q; 14 int costum[1001], req[1001], leave[1001]; 15 int main(){ 16 scanf("%d%d%d%d", &N, &M, &K, &Q); 17 for(int i = 0; i < K; i++){ 18 scanf("%d", &costum[i]); 19 } 20 for(int i = 0; i < Q; i++){ 21 scanf("%d", &req[i]); 22 } 23 for(int i = 0; i < N; i++){ 24 window[i].nowT = 8 * 60; 25 } 26 for(int i = 0; i < K; i++){ 27 int index = -1, minLine = 100000000; 28 for(int j = 0; j < N; j++){ 29 if(window[j].qu.size() < minLine && window[j].qu.size() < M){ 30 index = j; 31 minLine = window[j].qu.size(); 32 } 33 } 34 if(index != -1){ 35 window[index].qu.push(i); 36 }else{ 37 int popIndex = -1, minTime = 100000000; 38 for(int j = 0; j < N; j++){ 39 window[j].endFirst = window[j].nowT + costum[window[j].qu.front()]; 40 if(window[j].endFirst < minTime){ 41 popIndex = j; 42 minTime = window[j].endFirst; 43 } 44 } 45 window[popIndex].nowT = window[popIndex].endFirst; 46 leave[window[popIndex].qu.front()] = window[popIndex].endFirst; 47 window[popIndex].qu.pop(); 48 window[popIndex].qu.push(i); 49 } 50 } 51 for(int i = 0; i < N; i++){ 52 while(window[i].qu.empty() == false){ 53 int Ci = window[i].qu.front(); 54 leave[Ci] = window[i].nowT + costum[Ci]; 55 window[i].nowT = leave[Ci]; 56 window[i].qu.pop(); 57 } 58 } 59 for(int i = 0; i < Q; i++){ 60 req[i]--; 61 if(leave[req[i]] - costum[req[i]] >= 17 * 60){ 62 printf("Sorry\n"); 63 }else{ 64 printf("%02d:%02d\n", leave[req[i]] / 60, leave[req[i]] % 60); 65 } 66 } 67 cin >> N; 68 return 0; 69 }
总结:
1、题意:有N个窗口,每个窗口可以排队M个人,有K个顾客来办理。每次顾客都选择最短的队伍排队。
2、由于顾客优先选择队列人少的地方排队而不是提前结束服务的队伍排队,所以需要保留队列信息。当存在不满的队列时,找一个最短的队列,将顾客加入。当队列都满时,选择一个能最早服务完一个人的队列,则该队列就是最短的队列,服务完队首元素后将顾客加入。
3、题目中要求的是在5点后没有开始服务的人无法被服务,而非5点后没有完成业务的人无法被服务。