1014 Waiting in Line (30分)-queue、坑点分析

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 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, 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

解题思路:

  1. 题目不难,但是需要注意坑点(被一个小坑折磨了一下午)。
  2. 属于银行多窗口排队问题,大概意思就是黄线区内未满,后来的人优选选择队伍人数最少的窗口,如果多个窗口并列,则选择编号最小的窗口。如果黄线区内已满,后来的人在外面等待,当有一个队伍出现空缺时,及时补充。当一个人进入了黄线区,那么他离开的时间也就确定了。

  3. 显然这是一个队列的问题,首先建立一个窗口queue数组,用于记录窗口排队信息。另一个leavetime数组,用于记录每个人离开的时间。首先将黄线区填满,计算每个进入黄线区的人的离开时间,即个人服务时间加上所在窗口前一位等待人员的离开时间。如果黄线区填满之后还有等待的人,那么比较所有窗口队首的人离开的时间,选取最小的离开时间,然后将此人弹出队列,再从黄线区外补充人员,计算新人员的离开时间,直到所有人都进入黄线区。

  4. 查询信息输出。根据输入的id查询对应的离开时间,如果这个人开始接受服务的时间(注意不是离开时间)严格小于17:00(也就是说来使接受服务时间的最大值为16:59),则输出他的离开时间。否则,输出sorry。

AC代码:

#include <iostream>
#include <queue>
using namespace std;
int main(){
	queue<int> windows[20];
	int processtime[1000],leavetime[1000];
	int N,M,K,Q,i,j;
	cin>>N>>M>>K>>Q;
	for(i=0;i<K;i++)
		scanf("%d",&processtime[i]);
	for(i=0;i<M*N&&i<K;i++){
		if(i<N)
			leavetime[i]=processtime[i];	
		else
			leavetime[i]=leavetime[windows[i%N].back()]+processtime[i];
		windows[i%N].push(i);		
	}	
	for(;i<K;i++){
		int mintime=1E6,minwindow=-1,j;
		for(j=0;j<N;j++){
			int top=windows[j].front();
			if(leavetime[top]<mintime){
				mintime=leavetime[top];
				minwindow=j;	
			}	
		}
		int back=windows[minwindow].back();
		leavetime[i]=leavetime[back]+processtime[i];
		windows[minwindow].push(i);
		windows[minwindow].pop();		
	}
	while(Q--){
		int id;
		scanf("%d",&id);
		if(leavetime[id-1]-processtime[id-1]<540){
			int h=leavetime[id-1]/60;
			int min=leavetime[id-1]%60;	
			printf("%02d:%02d\n",h+8,min);
		} 	
		else
			printf("Sorry\n");		
	}
	return 0;	
} 

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值