[PAT]1017 Queueing at Bank--CG刷題之旅

[C++]1017 Queueing at Bank

1017 Queueing at Bank:
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.

輸入格式:
Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤10
​4
​​ ) - 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.
輸出格式:
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.

輸入:
7 3
07:55:00 16
17:00:01 2
07:59:59 15
08:01:00 60
08:00:00 30
08:00:02 2
08:03:00 10
輸出:
8.2

題目大意:
有N名顧客,K個窗口,一個窗口一次只能處理一名顧客,每名顧客有到來時間與處理時間,該店在8:00~17:00開業,八點之前來的顧客需等待,下午五點之後來的顧客不給予服務,顧客須在黃線外等候,問所有顧客的平均等待時間

解題分析:
可將所有時間換算成秒,顧客用結構體存放,然後根據到來時間進行排序,k個窗口的結束時間用優先隊列存放,如果隊首結束時間大於顧客到來時間,則顧客需等待endtime-cometime時間,若大於隊首結束時間,則無需等待,直接可以接受服務。如果顧客到來時間大於下午五點,則不用計算在其內。

AC代碼:

#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;

struct Cus{
	int gettime;
	int protime;
};

int cmp(Cus a, Cus b){
	return a.gettime < b.gettime;
}

int n, k;
Cus cus[100010];
priority_queue<int, vector<int>, greater<int>> win;

int main(){
	cin>>n>>k;
	
	for(int i = 0; i<n; i++){
		int h, m, s, p;
		scanf("%d:%d:%d %d", &h, &m, &s, &p);
		cus[i].gettime = h*3600 + m*60 + s;
		cus[i].protime = p*60;
	}
	
	int opentime = 8*3600;
	for(int i = 0; i<k; i++){
		win.push(opentime);
	}
	sort(cus, cus+n, cmp);
	int closetime = 17*3600;
	int ans = 0;
	double sum = 0;
	for(int i = 0; i<n; i++){
		if(cus[i].gettime+cus[i].protime > closetime)
			break;
		int endtime = win.top();
		win.pop();
		ans++;
		if(endtime > cus[i].gettime){
			sum += endtime - cus[i].gettime;
			win.push(endtime + cus[i].protime);
		}
		else
			win.push(cus[i].gettime + cus[i].protime);

	}
	printf("%.1f\n", sum/((double)ans*60.0));
	
	return 0;
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章