2021-03-08

**

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

解决步骤:
1、定义结构体node,用存储客户的到达时间和窗口处理时间(用两个数组来存储也许),并建立数组node s[10000]用来存储所有客户信息
2、用一个string或字符串变量存储客户到达时间,并将其转换成分钟或秒,将信息存储到客户信息数组中
3、可用sort函数来递增排序数组
4、建立函数来算出客户平均等待时间,函数中可再建立一个数组来存储各窗口服务时间(指空闲时间),并用一个变量min_s来记录各窗口最闲(指最先空闲)的窗口,并将其分配给接下来的客户,记录等到时间,并更新窗口时间,再更新min_s
5、最后输出平均等待时间

注意:
1、计算的是平均等待时间
2、08:00之后才开始服务,提前来的要等到08:00才能去得到服务,这部分等待时间也要计算
3、17:00之后的客户不需要计算在内

C++代码

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

typedef struct node {//用来记录客户数据
    float MM;//分别到达时间,分
    float pro;//记录处理时间
}cus;

bool cmp(node a, node b) {
    return a.MM < b.MM;
}

float process(node* n, int N, int K) {
    if (K >= N) {
        return 0;
    }
    else
    {
        float p[100];//存储各窗口可服务时间
        float sum = 0;//记录总等待时间
        int min_s = 0;//标记最近可服务窗口
        for (int i = 0; i < K; i++) {//使各窗口起始可服务时间为早上八点
            p[i] = 8 * 60;
        }
        for (int i = 0; i < N; i++) {
            if (n[i].MM > 17 * 60) {
                N = i;//下午17:00后的客户不计入计算
                if (N == 0) {
                    return 0;
                }
            }
            else {
                if (n[i].MM > p[min_s]) {//若窗口可服务时间小于客户到达时间
                    sum += 0;
                    p[min_s] = n[i].MM + n[i].pro;//更新该窗口时间
                }
                else {//若窗口可服务时间不小于客户到达时间
                    sum += p[min_s] - n[i].MM;
                    p[min_s] = p[min_s] + n[i].pro;//更新该窗口时间
                }
                for (int j = 0; j < K; j++) {//更新min_s
                    if (p[j] < p[min_s]) {
                        min_s = j;
                    }
                }
            }
        }
        return sum / N;
    }
}

int main() {
    int N, K;//N为客户人数,K为窗口数量
    cin >> N >> K;
    cus n[10000];//用来记录所有客户到达时间,单位:分钟
    for (int i = 0; i < N; i++) {
        string s;
        cin >> s >> n[i].pro;
        n[i].MM = ((s[0] - '0') * 10 + (s[1] - '0')) * 60 + (s[3] - '0') * 10 + (s[4] - '0') + float((s[6] - '0') * 10 + (s[7] - '0') )/ 60;//要用float将整数除转化成小数
    }
   sort(n, n + N, cmp);
   float sum = process(n, N, K);
   printf("%.1f",sum);//保留一位小数
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值