A1017 Queueing at Bank (25 分| 模拟,附详细注释,逻辑分析)

写在前面

  • 思路分析
    • n个客户, k个窗口
      • 已知每个客户到达时间和需要时长,如果有窗口就依次过去,没有窗口就在黄线外等候。求客户平均等待时长
        • 黄线外只有1个队伍,先来先服务
        • 银行开放时间为8点到17点,8点之前不开门, 8点之前来的人要等待,在17点后来的人不被服务。
    • 实现分析:
      • 客户结构体node,到达时间、需要服务时长
        • 先将所有满足条件的(到来时间点在17点之前)客户放入数组,数组长度就是需要服务的客户个数
        • window数组表示某个窗口的结束时间
          • 客户到,有空闲窗口,等待时间为0,刷新窗口结束时间
          • 客户到,无空闲窗口,循环比较等待时间最小的一个,刷新窗口结束时间
      • 开始所有窗口值都为8点整。对所有客户先进行排序,按来的时间早晚排序,之后再先来先服务
        • 时间暂时化解成秒数,便于计算
        • 如果1个可以被服务的客户都没有,输出0.0
  • 题目基础,细节处理耗费时间
    • 学习ing

测试用例

  • 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
    output:
    8.2
    

ac代码

  • #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    struct node
    {
        int come, time;
    } tmpcustomer;
    bool cmp1(node a, node b)
    {
        return a.come < b.come;
    }
    int main()
    {
        int n, k;
        scanf("%d%d", &n, &k);
        vector<node> custom;
        for(int i = 0; i < n; i++)
        {
            int hh, mm, ss, time;
            scanf("%d:%d:%d %d", &hh, &mm, &ss, &time);
            int cometime = hh * 3600 + mm * 60 + ss;
            // 17点后
            if(cometime > 61200) continue;
            tmpcustomer = {cometime, time * 60};
            custom.push_back(tmpcustomer);
        }
        sort(custom.begin(), custom.end(), cmp1);
    
        // 初始化为早8点
        vector<int> window(k, 28800);
        double result = 0.0;
    
        for(int i = 0; i < custom.size(); i++)
        {
            int tmpInx = 0, minfinish = window[0];
            // window数组表示某个窗口的结束时间,每1个客户到来的时候,选择最早结束时间的窗口
            for(int j = 1; j < k; j++)
            {
                if(minfinish > window[j])
                {
                    minfinish = window[j];
                    tmpInx = j;
                }
            }
    
            // 如果最早结束时间比他晚,累加等待的时间,更新window的值
            if(window[tmpInx] <= custom[i].come)
                window[tmpInx] = custom[i].come + custom[i].time;
            else
            {
                result += (window[tmpInx] - custom[i].come);
                window[tmpInx] += custom[i].time;
            }
        }
    
        if(custom.size() == 0)
            printf("0.0");
        else
            printf("%.1f", result / 60.0 / custom.size());
        return 0;
    }
    
发布了328 篇原创文章 · 获赞 107 · 访问量 39万+

猜你喜欢

转载自blog.csdn.net/qq_24452475/article/details/100618637