PAT1017 Queueing at Bank
Description
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
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
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
Pasing
一个银行有k个窗口提供服务。窗口前有条黄线将等待区分为了两个部分。用户需要在黄线后等待,知道轮到他进行服务并且这个窗口现在是开放的。没有一个窗户会被一个顾客独占超过1小时。
现给出每个顾客的到达时间和处理时间,计算所有顾客的平均等待时间。
Idea
定义顾客类,保存到达时间、处理时间、离开时间。
来的早的顾客早排队早接受服务所以先对顾客排序。
然后随着时间推移,改变窗口状态。
Code
//
// Created by SYSZY on 2022/2/5.
//
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
int n, k;
struct Customer {
int arrive_time, P;
int leave_time;
} c[10005];
bool cmp(Customer a, Customer b) {
return a.arrive_time < b.arrive_time;
}
int main() {
cin >> n >> k;//n customers, k windows
int open_time = 8 * 3600, close_time = 17 * 3600;
for (int i = 0; i < n; i++) {
char C;
int hour, Min, sec, p;
cin >> hour >> C >> Min >> C >> sec >> p;
c[i].arrive_time = hour * 3600 + Min * 60 + sec;
c[i].P = min(3600, p * 60);
c[i].leave_time = 0;//初始化
}
c[n].arrive_time = close_time + 1;
sort(c, c + n, cmp);
double res = 0;
int next = 0;
int windows[105];
for (int i = 0; i < k; i++) windows[i] = -1;//-1表示该窗口空闲
for (int Time = open_time; c[next].arrive_time <= close_time; Time++) {
for (int i = 0; i < k; i++) {
if (windows[i] != -1) {
if (c[windows[i]].leave_time == Time) {//这个窗口的顾客到了离开的时间了
windows[i] = -1;
}
}
}
for (int i = 0; i < k; i++) {
if (windows[i] == -1) {
if (c[next].arrive_time <= Time && c[next].arrive_time <= close_time) {
windows[i] = next++;
}
}
}
for (int i = 0; i < k; i++) {
if (windows[i] != -1) {//这个窗口有人,但是这个人是刚开始服务,还没有对离开时间进行赋值
int j = windows[i];
if (c[j].leave_time == 0) {
res += Time - c[j].arrive_time;
c[j].leave_time = Time + c[j].P;
}
}
}
}
printf("%.1f", res / next / 60);
return 0;
}