PAT Advanced 1017 Queueing at Bank(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 (≤104) - 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
算法描述:模拟排队问题 结构体
问题描述:
银行服务问题 n个人 k个窗口
分别给出n个人的 到银行时间 和 业务办理时长,晚于下午5点则不予办理,求所有成功办理业务人的平均等待时间
#include<iostream>
#include<algorithm>
using namespace std;
int n, k, win[110]; //win数组存储窗口正在服务的顾客序号 -1为窗口闲置
struct customers
{
int in_time, pro_time, leave_time = 0;
bool operator< (const customers& t)const
{
return in_time < t.in_time;
}
}c[10010];
int main()
{
cin >> n >> k;
for(int i = 0 ; i < n ; i ++)
{
int hh, mm, ss, p_time;
scanf("%d:%d:%d %d", &hh, &mm, &ss, &p_time);
c[i].in_time = hh * 3600 + mm * 60 + ss; // 存储以秒为单位
c[i].pro_time = p_time * 60;
if(c[i].pro_time > 3600) c[i].pro_time = 3600;
}
sort(c, c + n); // 按到银行的时间排序
int next = 0;
double sum = 0;
for(int i = 0 ; i < k ; i ++) win[i] = -1;
c[n].in_time = 99999;
for(int time = 28800 ; c[next].in_time <= 61200 ; time ++) // 顾客在下午五点之前到银行都提供服务 即便服务到5:59
{
// 送客(窗口置为空闲)
for(int i = 0 ; i < k ; i ++)
if(win[i] >= 0) // 如果当前窗口有人 且 此刻是这个人的离开时间
{
int j = win[i];
if(c[j].leave_time == time)
win[i] = -1;
}
// 入队(开始服务)
for(int i = 0 ; i < k ; i ++)
// 窗口没有人 且 此刻是下一个需要办业务的人到来时间 且 到来时间不晚于下午5点
if(win[i] == -1 && c[next].in_time <= time && c[next].in_time <= 61200)
{
win[i] = next;
next ++;
}
// 迎客(基于上一步 开始服务则计算离开时间)
for(int i = 0 ; i < k ; i ++)
// 刚开始服务 标志:leave_time = 0;
if(win[i] >= 0)
{
int j = win[i];
if(c[j].leave_time == 0)
{
sum += time - c[j].in_time;
c[j].leave_time = time + c[j].pro_time;
}
}
}
printf("%.1lf", sum / next / 60);
return 0;
}

浙公网安备 33010602011771号