PAT甲级真题 1017 Queueing at Bank (25分) C++实现 (对比1014 Waiting in Line,不要思维定式)

题目

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 (<=10000) - 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

思路

PAT甲级真题 1014 Waiting in Line (30分) 和本题看似挺像,但千万不能受思维定式影响。有以下差别:

  1. 前者顾客一个紧接一个接受服务;本题一些窗口可能有“空窗期”,故需要独立记录每个窗口的当前时间。
  2. 前者17:00以后轮上的顾客不再对其服务;本题是只要在17:00前排上队,哪怕轮到他时已经过了17:00,都要为其服务。
  3. 前者每个窗口前有个队列;本题每个窗口前只有1人,即最近加入的人,故无需再额外存储窗口顾客信息。

一开始按和1014题类似的思路,踩了很多坑,后来参考柳神的方法,修修补补,优化代码,发现最后越来越像柳神的代码了…柳神代码:1017. Queueing at Bank (25)-PAT甲级真题(模拟)

思路是,用vector存储等待顾客信息(晚于17:00的不再考虑),将HH:MM:SS格式的到达时间转为秒数,按时间先后排序。

初始化每个窗口的当前时间是8:00,将顾客按顺序加入当前时间最小的窗口,更新该窗口的当前时间。若顾客来的早于当前时间,则累加顾客等待时间,并将当前窗口时间加上为顾客服务时间;若顾客来的晚于当前时间,则只需将当前窗口时间修改为顾客到来时间+顾客服务时间。

代码

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

struct C{  //顾客信息
    int arr;  //到达时间
    float dur;  //所需时间
};

bool cmp(C c1, C c2){
    return c1.arr < c2.arr;
}

int main(){
    int n, k;
    cin >> n >> k;
    vector<C> wait;
    for (int i=0; i<n; i++){
        int h, m, s;       
        float dur;
        scanf("%d:%d:%d %f", &h, &m, &s, &dur);
        int arr = 3600 * h + 60 * m + s;  //时间统一转换为秒
        if (arr <= 61200){  //3600 * 17 
            wait.push_back({arr, dur * 60});  //加入早于17:00到达的顾客
        } 
    }
    int len = wait.size();
    sort(wait.begin(), wait.end(), cmp);

    //处理过程
    float sum = 0;  //总等待时间
    vector<int> window(k, 28800);  //3600 * 8,每个窗口上个顾客结束时间
    for (int i=0; i<len; i++){
        //找出当前结束时间最早的窗口
        int minT = window[0];
        int minI = 0;
        for (int j=1; j<k; j++) {
            int t = window[j];
            if (t < minT){
                minT = t;
                minI = j;
            }
        }
        if (window[minI]==minT){
            if (wait[i].arr < window[minI]){  //若顾客到的比上个结束时间早
                sum += (window[minI] - wait[i].arr);
                window[minI] += wait[i].dur;
            }
            else {
                window[minI] = wait[i].arr + wait[i].dur;
            }
        }
    }
    if (len==0){
        printf("0.0\n");
        return 0;
    }
    else {
        printf("%.1f\n", sum / len / 60.0);
    }

    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Queueing theory is a mathematical study of waiting lines or queues that arise in various real-life scenarios, such as customer service, traffic congestion, hospital emergency rooms, and telecommunications networks. Basic queueing theory involves the following concepts: 1. Arrival Process: This is the process of customers arriving at the queue. The arrival process can be modeled using different distributions, such as Poisson or exponential. 2. Service Process: This is the process of serving customers in the queue. The service process can also be modeled using different distributions, such as Poisson or exponential. 3. Queue Length: This is the number of customers waiting in the queue at any given time. 4. Queue Occupancy: This is the proportion of time that the server is busy serving customers. 5. System Capacity: This is the maximum number of customers that the system can handle at any given time. 6. Utilization: This is the proportion of time that the server is busy serving customers compared to the total time. 7. Waiting Time: This is the time that a customer spends waiting in the queue before being served. 8. Service Time: This is the time that a customer spends being served by the server. 9. Queueing Models: There are different queueing models that can be used to analyze queueing systems, such as the M/M/1 model, M/M/c model, M/G/1 model, and M/D/1 model. 10. Performance Measures: Different performance measures can be used to evaluate queueing systems, such as average waiting time, average queue length, and system throughput.
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值