天天看點

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;	
} 
           

繼續閱讀