英文描述
Waiting in Line (30)
Suppose a bank has N windows open for service. There is a yellow line in front of the windows which divides the waiting area into two parts. The rules for the customers to wait in line are:
1 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.
2 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.
3 Customer[i] will take T[i] minutes to have his/her transaction processed.
4 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个服务窗口。窗口前的一条黄通道将等待区域分成两个部分。顾客的等待规则如下:
1 在每个窗口前黄色通道里面的空间足够容纳这个通道上的M个顾客。因此当N条通道占满了人的时候,NM + 1的那个顾客将会站在黄线后的区域等待。
2 当每个顾客在黄色通道后等待时,他们将会去选择最短的通道进入。如果有2条或多条通道具有相同的长度是,总是选择窗口号最小的通道。
3 顾客i需要t(i)分钟去办理他的业务。
4 银行早上8点开始上班。
5 现在给出每位顾客需要办理业务的时间,你要求出每位顾客办完他的业务时的时间。
举一个例子,假设这个银行有2个窗口,每个窗口可以容纳2个顾客在黄线里面等待着。这里有5个顾客办理业务的时间是1,2,6,4和3分钟。在早上8点,顾客1在窗口1办理业务,顾客2在窗口2办理业务,顾客3在窗口1前等待,顾客4在窗口2等待。顾客5在黄线外等。
在08:01分,顾客1办理完业务,并且顾客5就去窗口1千等待了,窗口1那个通道里的人最少。顾客2在08:02办理完业务,顾客4在08:06办理完业务,顾客3在08:07办理完业务,顾客5在08:10办理完业务。
输入规格:
每个文件包含一个测试用例。每个用例的第一行包含4个正整数:N(<= 20,窗口数),M(<= 10,窗口容量数),K(<= 1000,顾客数量),Q(<= 1000,需要查询这些顾客几点办理完业务)
下一行包含K个正整数,每个整数代表每个顾客需要办理业务的时间。
最后一行包含Q个正整数,是我们要求得这些顾客啥时候办理完业务。每个顾客被标记为1到N。
输出规格:
对于要查询的Q个用户,在一行内打印他们办理完业务时候的时间,格式是:小时:分钟,小时从08到17,分钟从00到59。注意,银行为客户提供服务的时间在17:00点之前,如果17:00之后还有客户需要我们服务,我们只能对他说“Sorry”
样例输入:
2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7
样例输出:
08:07
08:06
08:10
17:00
分析与解题
总算把这题读明白了,花了快20分钟。题意是否忽略了排队时间。假如早8点大家全都到了,有20个窗口,每个窗口容纳10人,那么前200个人是不是1秒就排好队了,其他800人在黄线外等着,等着去选择队伍最短的。还是只有10个人去排队,剩下的990个人在哪等着,去动态的选择队伍最短的。按道理讲显示中应该是我理解的那样,但从给的那个例子以及题目中要求好像不是这样的,也就是队伍有空你就进去。只有在黄线外的那些顾客采取动态选择队伍最短的进去。
此刻代码已经写了出来,过了2个点,得了16分,画了快两天的时间。昨天看了别人的代码和思路,大致跟我的是一样的。所以我想在大体思路上是没有什么问题了,剩下的应该就是细节的处理问题了。我在这里通过文字把思路整体撸一遍,通过10个队列来模拟排队方式,初始化的时候,将NM个人,放入队列中,第一个人在1队列1号位置,第二个人在2队列1号位置,依次类推。这一点我是使用了队号和位置号来确定是第几个人的,假如是i队列和m位置号,那也就是第(i – 1)* n + j个人所在位置。我看了其他人的代码有通过直接去余的方法来写,觉得是蛮简单的。第i个人就在第i % N的位置上,次数他并没有去考虑第几个队列和第几个位置,因为i是一直在增加的,所以这样去初始化队列是蛮方便的。初始化时要考虑一件事情,k(所有顾客)是否小于nm,所以在初始化的时候要进行判断,我使用的方法是在入队处去统计人数,当k是小于nm时候,如果统计的人数
昨天又去把代码改进了一番,整体的思路是所有顾客都要入队,当然所有顾客也都要出队,后来又思索了很多,最后剩下一个点没过,报的错误是段错误,昨天晚上也没有很好的想明白这个事情。今天早上起来,我觉得去读一读ZJU的代码,看看两者的思路中的差异到底在哪。因为ZJU的代码是cpp写的,所以会记录一番关于cpp的相关知识。刚才去看了ZJU的代码,思路清晰,代码也简洁,整体的思路跟我的是一样的,只不过我的实现看起来复杂太多了。
我决定还是放弃了,按照ZJU的思路去写一遍吧。说不定能发现自己的错误到底在哪。调试到要抓狂的阶段了,真的是找不出来是哪里错了。按照ZJU的方法写了一遍,又是一堆的错误。pass吧,这道题。
C语言实现
1 |
|