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:ios
- 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 (N M+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.
- Customeri will take Ti 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, customer1 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.算法
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).windows
The next line contains K positive integers, which are the processing time of the K customers.ide
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.spa
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.code
2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7orm
08:07
08:06
08:10
17:00
Sorry排序
8點開始服務,最終的時間超過下午5點不是sorry,只有下午5點開始服務纔是sorry,時間選擇化成分鐘,從上午8點到下午5點事540分鐘。顧客若排入隊伍就不能離開,即便其餘隊伍爲空也不能。只有等待前面人結束服務其才能開始服務。使用隊列queue做爲窗口排隊,其方法主要用到q.empty(),q.pop(),q.push().下面貼出代碼隊列
#include <queue> #include <iostream> using namespace std; int times[1001]; //處理時間 int asking[1001]; //完成時間 queue<int> q[21]; //窗口 int main(){ int N,M,K,Q; //N窗口數量 M窗口最大人數 K顧客數量 Q查詢數量 int i,j; cin >> N >> M >> K >> Q; for(i=1;i<=K;i++) cin >>times[i]; int sum = 0; // 窗口排隊人數 int count = 1; //人數 for(int time=0;time<540;time++){ //按時間排序 8:00-17:00 9小時540分鐘 for(i=0;i<N;i++){ for(j=0;j<q[i].size();j++){ if(time==asking[q[i].front()]){ //服務結束 q[i].pop(); sum--; if(!q[i].empty()){ //計算下一我的 這樣隊列第一個老是計算的 int tmp = q[i].front(); asking[tmp] = time + times[tmp]; } } } } while(sum<N*M&&count<=K){ int min = 0; for(i= 0;i<N;i++) //找到人數最小的窗口 if(q[min].size() > q[i].size()) min = i; if(q[min].size()==0) //無人直接計算 ,排在第一位直接pop asking[count] = time + times[count]; if(q[min].size()<M&&count<=K){ q[min].push(count); count ++; sum ++; } } } for(i=0;i<Q;i++){ int query; cin >> query; if(asking[query]==0) cout << "Sorry" <<endl; else{ int hour,min; hour = 8 + asking[query] / 60; min = asking[query] % 60; printf("%02d:%02d\n",hour,min); } } return 0; }