天天看點

PAT 1017 Queueing at Bank (25)(25 分)

1017 Queueing at Bank (25)(25 分)

Suppose a bank has K windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. All the customers have to wait in line behind the yellow line, until it is his/her turn to be served and there is a window available. It is assumed that no window can be occupied by a single customer for more than 1 hour.

Now given the arriving time T and the processing time P of each customer, you are supposed to tell the average waiting time of all the customers.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 numbers: N (<=10000) - the total number of customers, and K (<=100) - the number of windows. Then N lines follow, each contains 2 times: HH:MM:SS - the arriving time, and P - the processing time in minutes of a customer. Here HH is in the range [00, 23], MM and SS are both in [00, 59]. It is assumed that no two customers arrives at the same time.

Notice that the bank opens from 08:00 to 17:00. Anyone arrives early will have to wait in line till 08:00, and anyone comes too late (at or after 17:00:01) will not be served nor counted into the average.

Output Specification:

For each test case, print in one line the average waiting time of all the customers, in minutes and accurate up to 1 decimal place.

解答:剛開始我做的時候,通過不斷地+1秒來模拟windows的情況,結果部分正确,應該是有細節沒有注意到。後來自己看了其他人的代碼,找到了一個比較好的idea:即每次排隊時,顧客都會排在累計時間最短的視窗,如果這時顧客的到達時間比視窗最短時間大,那麼就不需要等待;反之,則需要等待,并且等待時間就是它們的差。

AC代碼如下:

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<deque>

using namespace std;

typedef struct{
	int hour, min, sec, arrivetime;
	int servetime;   //用秒存儲,友善計算 
}cus;

bool isEarly(cus c1, cus c2){
	return c1.arrivetime < c2.arrivetime;
}

int getQueueWindows(vector<int>& windows){
	int min = windows[0];
	int index = 0;
	
	for(int i = 1; i < windows.size(); ++i){
		if(windows[i] < min){
			min = windows[i];
			index = i;
		}	
	}
	return index;
}

int main()
{
	int N, K;
	
	scanf("%d %d", &N, &K);
	deque<cus> cuss;
	int startime = 8 * 3600, timelimit = 17 * 3600;
	
	//記錄所有17:00:00之前到的顧客 
	for(int i = 0; i < N; ++i)
	{
		int mins;
		cus tmp;
		scanf("%d:%d:%d %d", &tmp.hour, &tmp.min, &tmp.sec, &mins);
		tmp.servetime = mins * 60;
		tmp.arrivetime = tmp.hour*3600 + tmp.min*60 + tmp.sec;
		if(tmp.arrivetime < timelimit) 
			cuss.push_back(tmp);
	}
	//對顧客根據到的時間進行排序
	sort(cuss.begin(), cuss.end(), isEarly); 
	//通過對windows數組進行時間變化模拟顧客處理業務
	vector<int> windows(K, 0); 
	int totalwait = 0, totalPeople = cuss.size();
	//對windows進行初始化(僅對8:00:00之前來的人)
	for(int i = 0; i < windows.size(); ++i){
		windows[i] = startime;
	} 
	//對人進行排隊 
	for(int i = 0; i < cuss.size(); ++i){
		int minindex = getQueueWindows(windows);
		if(windows[minindex] > cuss[i].arrivetime){
			totalwait += windows[minindex] - cuss[i].arrivetime;
			windows[minindex] = windows[minindex] + cuss[i].servetime;
		}else{
			windows[minindex] = cuss[i].arrivetime + cuss[i].servetime;
		}
	}
	double avg_mins = 1.0 * totalwait / 60 / totalPeople;
	printf("%.1f\n", avg_mins);
	return 0;
}