1017. Queueing at Bank (25)

本文探讨了一个算法,用于解决银行服务中客户排队等待时间的问题。通过模拟客户到达时间和处理时间,该算法计算出所有客户的平均等待时间。重点在于通过排序和窗口分配策略,确保每个客户在合理的时间内获得服务。

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

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
思路:考察的是排队事件的模拟题 涉及到字符串time2整形以及反向变化。对到达的数据小->大排序(必定是最早到的最先得到服务),对窗口进行筛选,因为每次必定是先找最小时间的窗口进行服务的!

#include <iostream>
#include <string>
#include <iomanip>
#include <algorithm>
#include <stdio.h>
using namespace std;
struct Node{
    string arrive;
    int process;
};
int time2int(string s){
    int base = 3600;
    int time = 0;
    int loc;
    for(int i=0;i<3;i++){
        loc = s.find(":");
        time += atoi(s.substr(0,loc).c_str())*base; //This part is the core
        s = s.substr(loc+1);
        base /= 60;
    }
    return time;
}
string int2time(int n){
    string s;
    char part[3];
    for(int i=0;i<3;i++){
        sprintf(part,"%02d",n%60);  //change the int to char[]
        s = (string)part +s;
        n /= 60;
        if(i!=2)
            s = ":"+s;
    }
    return s;
}
bool compare(Node a,Node b){
    return a.arrive<b.arrive;
}
int main()
{
    int N,K;
    cin>>N>>K;
    Node *arr = new Node[N];
    int *waitTime = new int[N];
    string *window = new string[K];
    for(int i=0;i<K;i++){
        window[i] = "08:00:00";
    }
    for(int i=0;i<N;i++){
        cin>>arr[i].arrive>>arr[i].process;
    }
    sort(arr,arr+N,compare);    //Finish sort the arr
    int minN = 0;
    int flag = 0;   //mark the available time
    for(int i=0;i<N;i++){
        if(time2int(arr[i].arrive)>17*3600){
            continue;
        }
        for(int j=0;j<K;j++){   //find the min window
            if(window[j]<window[minN]){
                minN = j;
            }
        }
        if(arr[i].arrive<window[minN]){ //you need to wait
            int windowTime;
            waitTime[i] = time2int(window[minN])-time2int(arr[i].arrive);  //it is by seconds
//            cout<<waitTime[i]<<endl;
            windowTime = time2int(window[minN]) + arr[i].process*60;
            window[minN] = int2time(windowTime);
            flag++;
        }else{
            int windowTime;
            waitTime[i] = 0;
//            cout<<waitTime[i]<<endl;
            windowTime = time2int(arr[i].arrive) + arr[i].process*60;
            window[minN] = int2time(windowTime);
            flag++;
        }
    }
    double result = 0.0;
    for(int i=0;i<flag;i++){
        result += waitTime[i];
    }
    if(flag == 0){
        cout<<"0";
    }else{
        cout<<fixed<<setprecision(1)<<(result/(flag*60.0));
    }
    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、付费专栏及课程。

余额充值