PAT 1017 Queueing at Bank (25 分)燚

版权声明:哈哈 你能采纳 我很开心 https://blog.csdn.net/m0_38090681/article/details/84143215

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 (≤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.

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.

Sample Input:

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

Sample Output:

8.2

题目大意: 银行每天8:00整开始营业,17:00关门 。给出一些人到银行的时间以及办理业务所需时间。求每个人从到达银行到开始办理业务的平均等待时间。结果保留一位小数

相似题 PAT 1014 题   解析传送门:https://mp.csdn.net/postedit/84146442

注意: 1.凡17点以后到达银行的不计入平均人数之内。

            2.只要在17点以前到达银行的不管银行必须给与服务,即使开始服务时间超过17点。

            3.凡在8点以前到的,必须等待到8点才能办理业务,该等待时间算入等待时间之内

思路:1.银行有多个窗口,用queue来模拟现实窗口,每个窗口只能有一个人  

           2.建立一个结构体,存储每个人到银行的时间,办理业务所需时间,开始办理时间,以及结束时间

           3.由于涉及时分秒,因此将时间转换为秒,并以该天零点为起始时间点

           4.先将来银行的所有人存入一个vector中,并按到达时间由小到大排序,然后从队头开始从数组中选取一个人依次填入窗口。如果他人的到达时间小于8:00则将他的开始办理时间记为8:00,否则将办理时间记为到达时间,再将开始时间和业务所需时间相加得到结束时间

            5.比较每个窗口的结束时间,选取最小结束时间的窗口,将正在等待的队列的第一个取出排在该窗口并更新他的开始办理时间以及结束时间(如果他的到达时间大于该窗口前一个人的结束时间,则开始办理时间为到达时间,否则开始办理时间为前一个人的结束时间)然后将该窗口的头元素取出扔掉,使新加入的元素为正在办理业务的人,从而保证每个窗口只有一个人。

            6.将17:00以前到达的人的等待时间相加并除以人数,再换为分钟的到结果。

坑点:凡17 点以前到达皆可被服务。


#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<stdio.h>
using namespace std;
struct Person {
	int arriveTime;  //到达时间
	int needTime;    //办理业务所需时间
	int startTime;   //开始服务时间
	int endTime;     //结束时间
};
//输入函数
void input(vector<Person> &person, vector<queue<Person>>&que) {
	int n, m;
	cin >> n >> m;
	que.resize(m);    //给vector m个分配空间
	for (int i = 0; i < n; i++) {
		int H, M, S;
		int needTime;
		scanf("%d:%d:%d%d", &H, &M, &S, &needTime);
		Person temp;
		temp.arriveTime = H * 3600 + M * 60 + S;
		temp.needTime = needTime;
		person.push_back(temp);
	}
}
//将等待队列按到达时间排序
int compareArriveTime(Person &a, Person &b) {
	return a.arriveTime < b.arriveTime;
}
//在银行服务前将每个窗口排一人(每个窗口第一个办理业务的人)
int fillQue(vector<Person> &person, vector<queue<Person>>&que) {
	int ptr = 0;
	for (int i = 0; i < que.size() && ptr < person.size(); i++) {

		if (person[ptr].arriveTime <= 8 * 3600) {    //如果8点以前到达则开始时间为8点
			person[ptr].startTime = 8 * 3600;

		}
		else {
			person[ptr].startTime = person[ptr].arriveTime; //如果8点以后到达则开始时间为到达时间
		}
		person[ptr].endTime = person[ptr].startTime + person[ptr].needTime * 60;
		que[i].push(person[ptr++]);
	}
	return ptr;
}
//计算每个人的开始服务时间和结束时间
void enQue(vector<Person> &person, vector<queue<Person>>&que, int ptr) {
	while (ptr < person.size()) {
		int min = 0;
		//找到最先结束的窗口
		for (int i = 1; i < que.size(); i++) {
			if (que[min].front().endTime > que[i].front().endTime) {
				min = i;
			}
		}
		if (person[ptr].arriveTime <= que[min].back().endTime)
			person[ptr].startTime = que[min].back().endTime;
		else
			person[ptr].startTime = person[ptr].arriveTime;
		person[ptr].endTime = person[ptr].startTime + person[ptr].needTime * 60;
		que[min].pop();
		que[min].push(person[ptr++]);
	}
}
//计算总的等待时间
int allWaiteTime(vector<Person>&person, int &ptr) {
	int allTime = 0;
	while (person[ptr].arriveTime <= 17 * 3600 && ptr < person.size()) {
		allTime += (person[ptr].startTime - person[ptr].arriveTime);
		ptr++;
	}
	return allTime;
}

int main() {
	vector<Person>person; //等待队列
	vector<queue<Person>>que;// 银行窗口
	input(person, que);
	sort(person.begin(), person.end(), compareArriveTime);
	int ptr = fillQue(person, que);
	enQue(person, que, ptr);
	ptr = 0;
	int allTime = allWaiteTime(person, ptr);
	//如果都17点以后到达的情况
	if (ptr == 0) {  
		cout << "0.0" << endl;
	}
	else
		printf("%.1lf\n", allTime / (60.0 * ptr));
}

猜你喜欢

转载自blog.csdn.net/m0_38090681/article/details/84143215