1014 Waiting in Line (30 point(s))
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.
- 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 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 window1 while 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.
Example:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<deque>
using namespace std;
int main()
{
int N, M, K, Q;
cin >> N >> M >> K >> Q;
vector<string> Done(K+1);
vector<deque<int>> Queue(N);
for(int i = 0; i < K; i++) {
int process;
int start;
cin >> process;
if(i < N * M) {
if(Queue[i % N].empty()) {
start = 0;
Queue[i % N].push_back(process);
} else {
start = Queue[i % N].back();
Queue[i % N].push_back(start + process);
}
} else {
int minimal = 0;
for(int k = 1; k < N; k++)
if(Queue[k].front() < Queue[minimal].front()) minimal = k;
Queue[minimal].pop_front();
start = Queue[minimal].back();
Queue[minimal].push_back(start + process);
}
if(start >= 540)
Done[i+1] = "Sorry";
else {
char str[6];
sprintf(str, "%02d:%02d", 8+(start+process)/60, (start+process)%60);
Done[i+1] = str;
}
}
for(int i = 0; i < Q; i++) {
int query;
cin >> query;
cout << Done[query] << endl;
}
}
思路:
每个窗口设置一条队列,用来记录业务结束时间:
开始时间(i) = 结束时间(队列前一位)
结束时间(i) = 开始时间(i) + 处理时间(i)。
根据开始时间是否 >=540(8:00为基点),判断是否打印 Sorry,否则转换成对应墙上时间。
当队列未满时,直接排队;如果都满了,则需要判断哪个窗口的队头结束时间最小,然后进入该队列。