题目
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:
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
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
直接模拟类的题。
线内的各个窗口各为一个队,线外的为一个,按时间模拟出队、入队。
注意点:即使到关门时间,已经在服务中的客户(窗口第一个,接待时间早于关门时间)还是可以被服务的。其它的则不服务。
代码:
#include <iostream> #include <queue> using namespace std; const int MAX=0x3fffffff; //假设最大时间 struct cus //客户信息 { int time; //剩余的时间,初始为需要的时间 int id; //客户编号,从0开始 int sv; //客服服务标识,1为已经开始服务,0为未开始 }; int main() { int n,m,k,q; cin>>n>>m>>k>>q; queue<cus> win[20],over_yel; //窗口队列、线外队列 cus custom; //临时信息 int over_time[1000]; //结束时间,客户从0开始编号 int i,j,p=0,time; for(i=0;i<1000;i++) //初始化时间 over_time[i]=MAX; //客户信息输入部分 for(i=0;i<m;i++) //输入客户信息(线内) { for(j=0;j<n;j++) { cin>>time; custom.time=time; custom.id=p; custom.sv=0; win[j].push(custom); p++; if(p==k) break; } if(p==k) //如果人数不到,退出 break; } for(;p<k;p++) //输入线外客户 { cin>>time; custom.time=time; custom.id=p; custom.sv=0; over_yel.push(custom); } //队伍处理部分 time=0; //初始化开始时间,0代表开始,即8:00 int min_time; while(time<540) { min_time=MAX; for(i=0;i<n;i++) //统计各窗口最早完成时间 { if(!win[i].empty()) { win[i].front().sv=1; //各窗口第一个开始服务 if(win[i].front().time<min_time) min_time=win[i].front().time; } } if(min_time<MAX) //防止溢出,无实际作用 { time+=min_time; } else break; for(i=0;i<n;i++) //队伍处理 { if(!win[i].empty()) //各个窗口第一个减时间 win[i].front().time-=min_time; if(!win[i].empty()&&win[i].front().time==0) //处理完成的客户 { j=win[i].front().id; over_time[j]=time; win[i].pop(); if(!over_yel.empty()) //线外的补充 { win[i].push(over_yel.front()); over_yel.pop(); } } } } for(i=0;i<n;i++) //!!!处理关门后已经在服务的客户 { if(!win[i].empty()&&win[i].front().sv==1) { j=win[i].front().id; over_time[j]=time+win[i].front().time; } } for(i=0;i<q;i++) //信息输出部分 { cin>>j; if(over_time[j-1]==MAX) cout<<"Sorry\n"; else { if(over_time[j-1]<120) cout<<"0"; cout<<8+over_time[j-1]/60<<":"; if(over_time[j-1]%60<10) cout<<"0"; cout<<over_time[j-1]%60<<endl; } } return 0; }