PAT 甲级 1017 Queueing at Bank

本文通过一个具体的案例探讨了模拟算法的应用。模拟了顾客到达与服务的过程,利用优先队列记录每位顾客的服务时间,最终计算出平均等待时间。文章还回顾了优先队列的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模拟
以前老师说,模拟是最考验代码水平的(笑),确实啊。
这题和前面一题有点像,换了个不同的做法,模拟了每一秒时的情况,等待时间就是每秒钟排队的人数。
注意点就是是否服务取决于客户到来的时间,和当前时间是否超过17点没关系。
回顾了优先队列。

#include <iostream>
#include <cstdio>
#include <algorithm> 
#include <cstring>
#include <cmath>
#include <vector>
#include <queue> 
using namespace std;
struct Cus
{
    long long a_time;
    long long p_time;
};
bool cmp(Cus a, Cus b)
{
    return a.a_time < b.a_time;
}
bool operator < (const Cus &a, const Cus &b)
{
    return a.p_time > b.p_time;
}

int main()
{
    long long n, k;
    Cus c[10005];
    queue<Cus> q;
    priority_queue<Cus> w;
    while (~scanf("%lld %lld", &n, &k))
    {
        while (!q.empty())
        {
            q.pop();
        }
        while (!w.empty())
        {
            w.pop();
        }
        long long hh, mm, ss, p;
        long long st = 8 * 60 * 60;
        long long et = 17 * 60 * 60 + 1;
        long long time = st;
        double sum = 0;
        long long t;
        long long num = 0;
        for (long long i = 0; i < n; i++)
        {
            scanf("%lld:%lld:%lld %lld", &hh, &mm, &ss, &p);
            t = hh * 60 * 60 + mm * 60 + ss;
            if (t < et)
            {
                c[num].a_time = t;
                c[num++].p_time = p * 60;
            }
        }
        sort(c, c + num, cmp);
        long long cnt = 0;
        for (cnt = 0; cnt < num; cnt++)
        {
            if (c[cnt].a_time <= st)
            {
                q.push(c[cnt]);
                sum += st - c[cnt].a_time;
                c[cnt].a_time = st;
            }
            else break;
        }
        queue<Cus> temp;
        while (time < et||!q.empty())
        {
            while (w.size() < k && !q.empty())
            {
                w.push(q.front());
                q.pop();
            }
            while (c[cnt].a_time == time && cnt < num)
            {
                if (w.size() < k)
                {
                    w.push(c[cnt++]);
                }
                else
                {
                    q.push(c[cnt++]);
                }
            }
            sum += q.size();
            time++;
            if (!w.empty())
            {
                while (!w.empty())
                {
                    Cus tt = w.top();
                    tt.p_time--;
                    w.pop();
                    if (tt.p_time != 0)
                    {
                        temp.push(tt);
                    }
                }
                while (!temp.empty())
                {
                    w.push(temp.front());
                    temp.pop();
                }
            }
        }

        printf("%.1lf\n", sum / (60 * num));
    }
    return 0;
}
### 银行排队问题的Python实现 银行排队问题是典型的并行处理和任务分配场景,可以通过ZeroMQ框架中的Ventilator-Worker-Sink模型来解决[^1]。以下是基于该模型的一个简单Python实现: #### ZeroMQ Ventilator 实现 Ventilator负责生成任务并将它们发送给Workers。 ```python import zmq import time context = zmq.Context() # Socket to send tasks to workers sender = context.socket(zmq.PUSH) sender.bind("tcp://*:5557") print("Press Enter when the workers are ready...") _ = input() print("Sending tasks to workers...") # Send out tasks total_msec = 0 for task_nbr in range(100): workload = int((task_nbr * task_nbr) % 100 + 1) # Some random work load total_msec += workload sender.send_string(str(workload)) print(f"Total expected cost: {total_msec} msec") time.sleep(1) # Give 0MQ time to deliver ``` #### ZeroMQ Worker 实现 Worker接收来自Ventilator的任务并执行计算后将结果返回给Sink。 ```python import zmq import sys import time context = zmq.Context() # Socket to receive messages on receiver = context.socket(zmq.PULL) receiver.connect("tcp://localhost:5557") # Socket to send messages to sender = context.socket(zmq.PUSH) sender.connect("tcp://localhost:5558") while True: s = receiver.recv_string() print(f"Received request: {s}") # Do some 'work' time.sleep(int(s) / 10) # Send results to sink sender.send(b'') ``` #### ZeroMQ Sink 实现 Sink收集所有Worker的结果,并统计完成时间。 ```python import zmq import time context = zmq.Context() # Socket to collect worker responses receiver = context.socket(zmq.PULL) receiver.bind("tcp://*:5558") # Wait for start of batch s = receiver.recv() # Start our clock now tstart = time.time() # Process 100 confirmations for task_nbr in range(100): s = receiver.recv() if task_nbr % 10 == 0: sys.stdout.write(':') else: sys.stdout.write('.') sys.stdout.flush() # Calculate and report duration of batch tend = time.time() print(f"\nTotal elapsed time: {(tend-tstart)*1000} msec") ``` 上述代码展示了如何通过ZeroMQ构建一个简单的分布式任务管理系统[^2]。对于银行排队问题,可以将其视为多个客户作为任务被分配到不同的柜员(即Worker),而最终的结果由Sink汇总。 此外,在高并发环境下,还需要注意内存管理和I/O性能优化[^3]。建议使用缓存机制减少磁盘操作频率,并利用消息队列实现各模块间的异步通信。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值