A 1014 Waiting in Line
Problem Description
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.
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
题目大意:
n个窗口每个窗口最多排m个人,所有人都可以理解为在8:00到达,然后k个人依次按顺序循环排队,直到排满n个窗口的n*m个位置,剩下的人站在黄线外等待,哪个窗口的人先办完事情先到哪个窗口补队。最后是Q个询问,如果顾客在17:00(不含)之前开始在窗口办事,输出该顾客结束办事的时间,否则输出"Sorry"。
解题思路:
先填满n*m个位置,每个新入队列(最大长度为m)的顾客的结束时间是队尾顾客的结束时间加上该顾客的办事时间。之后是黄线外依次进入黄线内,每进一个人遍历一次所有窗口,选出最早办完事的窗口(窗口队列的队首顾客结束时间),然后将下一个人安排进该窗口队列队尾。
AC代码
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
const int start = 8*60; //开始时间8:00
const int last = 17*60; //结束时间17:00
const int maxn = 1005;
struct node{
int id; //顾客编号
int t; //办事结束时间
};
int ans[maxn],n,m,k,q,ask;
queue<node> win[21]; //记录窗口情况
int main(){
scanf("%d%d%d%d",&n,&m,&k,&q);
fill(ans,ans+maxn,-1);
vector<int> man(k+1);
for(int i=1;i<=k;i++){
scanf("%d",&man[i]);
}
int num=1;
int stop=n*m;
while(num<=stop){
if(num>k){
break;
}
int local = num%n==0 ? n : num%n; //当前窗口号
if(num<=n){
win[local].push({num,man[num]+start});
ans[num]=man[num]+start;
}else{
int st=win[local].back().t; //该窗口队伍最后一个人的结束时间
int res=man[num]+st;
win[local].push({num,res});
if(st<last){
ans[num]=res;
}
}
num++;
}
for(;num<=k;num++){
int mint=INF,minn;
for(int i=1;i<=n;i++){
if(!win[i].empty()){
if(win[i].front().t<mint){
mint=win[i].front().t; //窗口正在办事的人的结束时间
minn=i;
}
}
}
int st=win[minn].back().t;
int res=man[num]+st;
if(st<last){
ans[num]=res;
}
win[minn].push({num,res});
win[minn].pop();
}
while(q--){
scanf("%d",&ask);
if(ans[ask]==-1){
printf("Sorry\n");
}else{
int HH=ans[ask]/60;
int MM=ans[ask]%60;
printf("%02d:%02d\n",HH,MM);
}
}
return 0;
}