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.
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 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.
Input Specification:
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 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
题目大意:
有N个窗口,每个窗口的黄线前可以排最多M个人。客户到银行时优先排在人数少的窗口前面,如果有多个窗口人数一样就排在序号小的窗口前。在下午5点前还没有开始服务的客户无法完成业务。求询问的客户完成业务的时间(无法完成输出sorry)。
解答:
先送客,后入队,入队后迎客(设定left_time)
①送客 判断当前队列中的客人的left_time是否与当前Time相等,相等即服务完成,出队
②入队 每一分钟都要判断一次是否有窗口前黄线内未站满人,以每个窗口前都站1个人,2个人,3个人来遍历(即i),判断每个窗口是否满足了这个i人数,这样也能达到每个窗口人数相同时优先站前面窗口的功能
③迎客 即为黄线内的客人设定left_time,如果黄线内的客人lefe_time为0,就设定
#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
struct customer{
int process_time,left_time=0; //初始化未得到服务left_time为0
};
customer c[1001];//储存所有所有客人信息结构体数组
int main(){
int N,M,K,Q; //N->windows number.M->Maximum number of people per window
cin>>N>>M>>K>>Q;
queue<int> q[N];
int cursor=1;//cursor 用来计数入队客人序号
for(int i=1;i<=K;i++) cin>>c[i].process_time;
//24小时分钟遍历
for(int Time=480;Time<1020;Time++){
//送客
for(int i=0;i<N;i++){
if(q[i].size()){
int j = q[i].front();
if(c[j].left_time==Time) q[i].pop();
}
}
//入队
for(int i=1;i<=M;i++){ //所有窗口前都站1人,2人,3人...时
for(int j=0;j<N;j++) //j 窗口序号
if(q[j].size()<i)
if(cursor<=K){
q[j].push(cursor);
cursor++;
}
}
//迎客
for(int i=0;i<N;i++){//遍历窗口给已经在队列中的人设定离队时间
if(q[i].size()){
int j=q[i].front();
if(c[j].left_time==0){
c[j].left_time=Time+c[j].process_time;// 给在黄线内的客人设定离开时间
}
}
}
}
int search;
while(Q--){
cin>>search;
if(c[search].left_time==0) cout<<"Sorry\n";
else printf("%02d:%02d\n",c[search].left_time/60,c[search].left_time%60);
}
return 0;
}
PS:因为VS上只能定义常量数组,所以队列的定义要用new
queue<int>* q;
q = new queue<int>[N];