1014 Waiting in Line (30 分)
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 window1and 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人。有k为顾客需要办理业务,给出了每个客户的办理业务时间。
- 银行在8点开始服务,如果窗口都排满了,客户就得在黄线外等候。如果有一个窗口用户服务结束,
- 黄线外的客户就进来一个。如果有多个可选,选窗口id最小的。
- 输出查询客户的服务结束时间。
- 如果客户在17点(注意是包括的!!!就在这里被坑了,一开始还以为不包括。。。)或者以后还没开始服务,就输出Sorry
- 如果已经开始了,无论多长都会继续服务的。
- 思路:
- 建立一个优先级队列,存储在黄线之内的所有客户。
- 对于m*n之前的人,依此往窗口里排队(即优先级队列里)即可。
- 对于之后的人,从优先级队列里取出结束时间最少的客户,有相同的话则是窗口最小的,
- 进入他所在的队列。
- 同时还要建立linetime数组,存储每个窗口排在末尾客户的结束时间。
- 当窗口j新加进来一个客户,他的起始时间则是linetime[j],结束时间则是linetime[j]+服务时间,同时更新linetime[j]
- 最后对于查询的客户,如果起始时间>=17:00,则输出Sorry
- 否则输出对应的结束时间即可,这里方便起见,以分数存储的,所以最后还要转化一下。
- 关于bool operator<(const st tmp)const的用法
-
#include <iostream> #include <cstdio> #include <algorithm> #include <string> #include <vector> #include <cstring> #include <queue> using namespace std; const int maxn=10010; int n,m,k,q; struct st { int line; int start_time; int end_time; int process_time; bool operator<(const st tmp)const { //取队列中最早结束的,如果时间一样,取窗口id最小的 if(end_time==tmp.end_time) return line>tmp.line; else return end_time>tmp.end_time; } } s[maxn]; int query[maxn]; int finish_time[maxn]; int main() { scanf("%d%d%d%d",&n,&m,&k,&q); for(int i=1; i<=k; i++) { scanf("%d",&s[i].process_time); } for(int i=1; i<=q; i++) { scanf("%d",&query[i]); } priority_queue<st>queueline; int t=0; st tmp; int linetime[maxn]; memset(linetime,0,sizeof(linetime)); //对于前m*n个人,往队列里插即可 for(int i=1; i<=m&&t<k; i++) { for(int j=0; j<n&&t<k; j++) { t++; tmp.line=j; tmp.start_time=linetime[j]; tmp.end_time=linetime[j]=tmp.start_time+s[t].process_time; s[t].line=j; s[t].start_time=tmp.start_time; s[t].end_time=tmp.end_time; queueline.push(tmp); } } st c; for(int i=t+1; i<=k; i++) { //取出结束时间最早的 tmp=queueline.top(); queueline.pop(); c.line=tmp.line; c.start_time=linetime[tmp.line]; c.end_time=linetime[tmp.line]=c.start_time+s[i].process_time; s[i].line=c.line; s[i].start_time=c.start_time; s[i].end_time=c.end_time; queueline.push(c); } int x; int maxtime=(17-8)*60; for(int i=1; i<=q; i++) { x=query[i]; if(s[x].start_time>=maxtime) { //原来开始的时间包括17:00。。。只要是17:00或者是以后的,就不被服务 printf("Sorry\n"); } else { int h=s[x].end_time/60; int m=s[x].end_time%60; printf("%02d:%02d\n",h+8,m); } } return 0; }