题目链接:http://www.patest.cn/contests/pat-a-practise/1014
题目:
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 Input2 2 7 5 1 2 6 4 3 534 2 3 4 5 6 7Sample Output
08:07 08:06 08:10 17:00 Sorry
分析:
这是一道模拟题,所谓模拟题,就是再现生活中的一些场景,比如排队、汽车计费或者其他之类的过程,是相对于其他一些用一些数学能解出最佳答案的题目而言的。
这是一道模拟银行排队的题目,每次顾客都会选择窗口中人数最小的排队(如果人数相同则选择窗口标号最小的)。
这题比较关键和有技巧的一点就是选择时间作为我们的循环变量(当然这只是其中一种方法),也更好理解。每次时间增加后,先查看队头的人是否有服务完成的,如果是则出队,并计算其后顾客(如果有)的结束时间:当前时间+顾客服务时间;再查看是否有人可以排进窗口队伍中。
这里假设顾客都是同时到达银行的,所以相对来说更简单了一些。
注意点:
(1)从5:00前就已经开始服务的,虽然最终的服务时间超过五点,也不是Sorry,而只有从五点之后才会被开始服务的,会是Sorry。这个很关键,不然会有答案错误的案例。
(2)对于时间的选择,最好是用都化成分钟的形式,如果用时间类来做,或者重载了其加号运算符和赋值运算符,会出现超时的情况。
(3)每个顾客进入到一个队伍之后,就不能再离开这个队伍,即使其他队伍中已经没有人了,也只能等当前队伍前面的人结束之后在本队伍接受服务。
案例分析:
2个窗口,每个窗口最多2个人,7个顾客,5次查询。
顾客的时间为1 2 6 4 3 534 2分钟。
查询的顾客为3 4 5 6 7号
流程:
时间 | 事件 |
---|---|
8:00 | 1号顾客进入队伍1 |
8:00 | 2号顾客进入队伍2 |
8:00 | 3号顾客进入队伍1 |
8:00 | 4号顾客进入队伍2 |
8:01 | 1号顾客离开队伍1 5号顾客进入队伍1 |
8:02 | 2号顾客离开队伍2 6号顾客进入队伍2 |
8:06 | 4号顾客离开队伍2 7号顾客进入队伍2 |
8:07 | 3号顾客离开队伍1 |
8:10 | 5号顾客离开队伍1 |
17:00 | 6号顾客离开队伍2 |
超时 | 7号顾客还没开始,Sorry |
看到注意点(3)中的,你就知道了,在8:10到17:00的时间里,虽然队伍1中没有一个人,但是因为8:06的时候7号顾客已经选择了队伍2,所以他也只能干瞪眼等到队伍2中6号顾客花了534分钟时间后,而不能换队伍。
所以,查询的3 4 5 6 7号结束服务的时间分别是:
08:07
08:06
08:10
17:00
Sorry
AC代码:
#include<stdio.h> #include<queue> using namespace std; int serve_time[1001]; int ans[1001]; queue<int> Q[21]; int main(void){ int w,cap,cus,k;//分别记录窗口数量、窗口最大人数、顾客数量和查询数量 int i,j; while(scanf("%d%d%d%d",&w,&cap,&cus,&k) != EOF){ for(i = 1; i <= cus; i ++){ scanf("%d",&serve_time[i]); } for(i = 0; i < w; i ++){ if(Q[i].empty() == false)Q[i].pop(); } int sum = 0; int count = 1; for(int ti = 0; ti < 540; ti = ti + 1){//以时间来作为循环,这个很关键 for(i = 0; i < w; i ++){ for(j = 0; j < Q[i].size(); j ++){ if(ti == ans[Q[i].front()]){//如果当前队伍的人服务结束了 Q[i].pop(); sum --; if(!Q[i].empty()){//并且计算当前队伍下一个人的结束时间 int tmp = Q[i].front(); ans[tmp] = ti + serve_time[tmp];//结束时间为当前时间+顾客的服务时间 } } } } while(sum < w * cap && count <= cus){ int min = 0; for(i = 0; i < w; i++){ if(Q[min].size() > Q[i].size()){ min = i; }//找到人最少的那个队伍 if(Q[min].size() == 0) ans[count] = ti + serve_time[count];//如果队伍没人则直接开始服务,并且计算结束时间 if(Q[min].size() < cap && count <= cus){ Q[min].push(count);//否则把让下一个顾客进队 count ++; sum ++; } } } } for(i = 0; i < k; i ++){ int query; scanf("%d",&query); if(ans[query] == 0)puts("Sorry");//值为0说明没有被开始服务,只能Sorry else{ int hour,min; hour = 8 + ans[query] / 60; min = ans[query] % 60; printf("%02d:%02d\n",hour,min);//把时间转换为标准格式并输出 } } } return 0; }
——Apie陈小旭
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 Input2 2 7 5 1 2 6 4 3 534 2 3 4 5 6 7Sample Output
08:07 08:06 08:10 17:00 Sorry