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

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

1、定义一个custormer结构体信息，包括到达银行时间和所需服务时间。将到达银行时间不超过17:00:00的顾客信息存储到结构体中，并对到达时间进行排序。

2、定义一个数组a[]，用于存储每个窗口完成服务的时间，将时间转换成十进制，初始值为28800，及08:00:00。

3、每次遍历寻找数组中的最小值，及服务完成的最早时间，然后依次将到达银行时间最早的顾客与窗口时间进行比较，若比窗口时间小，则更新等待时间和窗口时间，反之，说明该顾客不用等待，则更新窗口时间即可。

#include<stdio.h>
#include<string>
#include<algorithm>
#include<iostream>
#include<iomanip>
using namespace std;
typedef struct customers {
int ArrTime;
int ProTime;
};
bool cmp(customers a, customers b) {
return a.ArrTime < b.ArrTime;
}

int FindMin(int* a, int K) {
int s, min = 0xfffffff;
for (int i = 0; i < K; i++)
if (a[i] < min) {
min = a[i];
s = i;
}
return s;
}

int main() {
struct customers cus[10001];
int a[101] = { 0 };
int N, K, cnt=0, WaitTime = 0;
string At;
int Pt;
cin >> N >> K;
for (int i = 0; i < N; i++) {
cin >> At >> Pt;
if ((At[0] - '0') * 10 + (At[1] - '0') >= 17)
continue;
int at = ((At[0] - '0') * 10 + (At[1] - '0')) * 3600 + ((At[3] - '0') * 10 + (At[4] - '0')) * 60 + ((At[6] - '0') * 10 + (At[7] - '0'));
cus[cnt].ArrTime = at;
cus[cnt++].ProTime = Pt;
}
sort(cus, cus + cnt, cmp);
fill(a, a+K,28800);
for (int i = 0; i < cnt; i++) {
int s = FindMin(a, K); //找出最快完成服务的窗口
if (a[s] <= cus[i].ArrTime) {
a[s] = cus[i].ArrTime + (cus[i].ProTime * 60);//顾客不需等待，直接更新窗口时间
}
else {
WaitTime += abs(a[s] - cus[i].ArrTime);//更新等待时间
a[s] += (cus[i].ProTime * 60);
}

}
if (cnt == 0)
cout << "0.0" << endl;
else {
double aveTime = WaitTime * 1.0 / (60 * cnt);
cout << setiosflags(ios::fixed) << setprecision(1) << aveTime << endl;
}

}



©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客