【排队论】1 Queueing at Bank

队列问题

source: https://www.nowcoder.com/pat/1/problem/4011

解题报告

// 银行排队问题
// 模拟队列
// 两种情况
//      1. 如果有空闲窗口,直接处理客户业务;
//      2. 否则,等待最先处理完毕的窗口。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

#ifdef ONLINE_JUDGE
#else
#include <fstream>
#endif

using namespace std;

struct Time
{
    int hour;
    int min;
    int sec;
};

struct Customer
{
    int arrival;
    int waittime;
};

int tos(const Time a)
{
    return (a.hour * 3600 + a.min * 60 + a.sec);
}

bool cmp(const Customer & a, const Customer & b)
{
    return a.arrival < b.arrival;
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    ifstream cin("case.txt");
#endif // ONLINE_JUDGE

    int n, w;
    cin >> n >> w;

    // input customers' time arived
    vector<Customer>customer;
    Customer cus;
    Time time;
    char c;
    const int noserver = 17 * 3600;
    for (int i = 0;i < n;++i)
    {
        cin >> time.hour >> c >> time.min >> c >> time.sec >> cus.waittime;
        // 筛选数据
        cus.arrival = tos(time);
        cus.waittime *= 60;
        if (tos(time) < noserver)
        {
            customer.push_back(cus);
        }
    }

    sort(customer.begin(), customer.end(), cmp);

    //窗口时间线
    vector<int>windows;
    const int starttime = 8 * 3600;
    for (int i = 0;i < w;++i)
    {
        windows.push_back(starttime);
    }

    // 处理逻辑
    double sum = 0;
    n = customer.size();
    for (int i = 0;i < n;++i)
    {
        int min = 10000001;
        int index = 0;
        int j = 0;
        for (;j < w;++j)
        {
            if (customer[i].arrival >= windows[j])
            {
                windows[j] = customer[i].arrival + customer[i].waittime;
                break;
            }
            else
            {
                if (windows[j] < min)
                {
                    min = windows[j];
                    index = j;
                }
            }
        }
        if (j == w)
        {
            sum += min - customer[i].arrival;
            windows[index] += customer[i].waittime;
        }
    }

    // 秒数转分钟
    sum = (sum * 1.0 / 60 ) / n;
    printf("%.1lf\n", sum);
    getchar();
    return 0;
}

(全文未完)


参考资料
排队论及其应用浅析.pdf

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值