[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;
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章