PAT甲级 1017 Queueing at Bank (25 分)

本文介绍了一种计算银行排队系统中客户平均等待时间的算法。考虑到银行营业时间限制和业务处理时间上限,该算法首先筛选出符合条件的客户,然后通过队列模拟的方式计算在多个窗口下所有客户的平均等待时间。

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

题目:戳这里

题意:
银行8点开门,17点关门,有k个窗口,每个窗口前只能有一人在处理业务。
共有n个人排队,问银行营业时间内所有人的平均等待时间。
需要注意:
只要其到达时间在8~17之间就行,即使这个人到窗口前已经过了17点了也会给他处理业务。
如果处理业务时间大于60分钟,则只会给他办理60分钟业务。

解题思路:
这题和 1014 很像,但不一样,建议对比着做。
因为所有人只要在8~17点以内到达,就必须要给他处理业务,所以没法暴力遍历时间去模拟,那样反而麻烦。
所以就先把不合理的人给剔除掉,剩下的人放队列里面模拟排队直到全部处理完为止。

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn = 1e4 + 10;
const int inf = 0x3f3f3f3f;
struct nod {
    int tim;
    int cost;
}nu[maxn];
char st[maxn];
int wd[maxn];
bool cmp(nod a, nod b) {
    return a.tim < b.tim;
}
queue<int> q[maxn];
int main() {
    int n, k, hh, mm, ss;
    scanf("%d %d", &n, &k);
    int now = 0;
    int bg = 8 * 60 * 60, ed = 17 * 60 * 60;
    double ans = 0.0, ansi = 0.0;
    for(int i = 0; i < n; ++i) {
        scanf("%d:%d:%d %d", &hh, &mm, &ss, &nu[now].cost);
        nu[now].tim = hh *3600 + mm * 60 + ss;
        if(nu[now].tim > ed) continue;//不满足条件的直接剔除
        nu[now].cost = min(nu[now].cost, 60) * 60;//大于60的,让他等于60
        ++now;
    }
    n = now;
    int cnt = bg;
    sort(nu, nu + n, cmp);
    fill(wd, wd + k, bg);
    for(int i = 0; i < n; ++i) {
        int minn = inf, mini = 0;
        for(int j = 0; j < k; ++j) {
            if(wd[j] < minn) {
                minn = wd[j];
                mini = j;
            }
        }
        if(wd[mini] > nu[i].tim) {
            ans += wd[mini] - nu[i].tim;
            wd[mini] += nu[i].cost;
        } else {
            wd[mini] = nu[i].tim + nu[i].cost;
        }
    }
    n = max(n, 1);//别忘了边界
    printf("%.1f\n", ans/n/60.0);
	return 0;
}

/*
1 3
17:00:01 2


*/
### 银行排队问题的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、付费专栏及课程。

余额充值