PAT 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.
  • Customer​i​​ will take T​i​​ 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, customer​1​​ is served at window​1​​ while customer​2​​ is served at window​2​​. Customer​3​​ will wait in front of window​1​​ and customer​4​​ will wait in front of window​2​​. Customer​5​​ will wait behind the yellow line.

At 08:01, customer​1​​ is done and customer​5​​ enters the line in front of window​1​​ since that line seems shorter now. Customer​2​​ will leave at 08:02, customer​4​​ at 08:06, customer​3​​ at 08:07, and finally customer​5​​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

题目大意: 银行有m个窗口,每个窗口前可最多可排列一列n个人的队伍,来银行办理业务的人数为k个,因此剩余的(k-m*n)个人在黄线外的等待区。

输出:在8点到17点以前接受服务的元素都输出其结束时间,其他的输出Sorry

相似题 PAT 1017题  解析传送门:https://mp.csdn.net/postedit/84143215

       排队规则1.每个窗口最前面的办理完业务离开,等待队列队头元素选择一个最短窗口,如果同时有多个相同的最短窗口则选择号数最小的那一个窗口进行排队。

                        2.银行8点开始营业,17点结束营业,凡在此时间区间内开始接受服务的都有效,不管结束时间是否超过17点。

解题思想:1.每个元素只要排进了窗口队列,无论是否接受服务,其服务开始时间和服务结束时间就已固定,因此只需将等待区队列的元素按照上面的排队规则定位到他所在的窗口即可求出每个元素的服务时间。(除头元素外,其他元素的开始时间为上一个元素的结束时间,所以只需记录每个元素的结束时间)

                 2.在银行没开是营业之前将每个窗口的最大等待人数填满(在人数足够的情况下)。

                 3.然后银行开始营业,选出最先结束服务的窗口,扔掉窗口头的元素,用等待队列中的元素去填充,并更新新填充进去的元素的结束时间。从而保证每个窗口人数为n。直到等待队列中的元素被取完为止。然后输出给定元素的结束时间。

坑点:银行8点开始营业,17点结束营业,凡在此时间区间内开始接受服务的都有效,不管结束时间是否超过17点(坑了我两天时间)


#include
#include
#include
using namespace std;
struct Person{
	int needTime;//服务所需时间
	int finishTime;//结束时间
};
void input(int k,int q,vector &people,vector &resultPerson){
	for(int i=0;i>people[i].needTime;
	}
	for(int i=0;i>resultPerson[i];
	}
}
//在银行开业前填充每一个窗口
int fillTheQueue(vector&people,vector>&que,int m){ 
	int ptr=0; //ptr指向等待区下一个进入窗口队列的元素
	while(que[0].size() &people,int &ptr,vector>&que){
	while(ptr &resultPerson,vector&people){
	for(int i=0;i>n>>m>>k>>q;
	vectorpeople(k);  //等待队列
	vector>que(n); //银行办理窗口队列
	vectorresultPerson(q);  //要求输出的元素数组
    input(k,q,people,resultPerson);
	int ptr=fillTheQueue(people,que,m);//ptr指向等待区下一个进入窗口队列的元素
	enQue(people,ptr,que);
	output(resultPerson,people);
}

 

你可能感兴趣的:(code)