1017 Queueing at Bank (25)

本文介绍了一种计算银行排队系统中所有顾客平均等待时间的方法。通过将顾客的到达时间和处理时间转换为距离0点的秒数,对顾客到达时间进行排序,选择窗口中时间最小的服务,并更新窗口时间,最终计算出所有被服务顾客的平均等待时间。

摘要生成于 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

解题思路及注意事项:

这题并不难,把时间转换为距离0点的秒数,然后给到达时间排序,循环依次从窗口中选择时间最小的来服务,并对窗口时间进行更新

需要注意的是
- 顾客所需的处理时间如果是>60分钟的话自动变成60分钟
- 17:00:00及之前的顾客都会被服务,即使服务的时间超过24点

代码

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<iomanip>
#include<climits>
using namespace std;
struct Customer {
    string arrive;
    int arr_int;
    int get_server;
    int process;
    Customer(int lea = -1) { get_server = lea; }
};

bool compare(Customer c1, Customer c2);
void serve(vector<Customer>&ctm_vec,int k);
void timeProcess(vector<Customer>&ctm_vec);
float average(vector<Customer>&ctm_vec);
int main() {
    int i, n, k;
    vector<Customer>ctm_vec;
    Customer customer;
    cin >> n >> k;
    for (i = 0; i < n; i++) {
        cin >> customer.arrive >> customer.process;
        ctm_vec.push_back(customer);
    }
    sort(ctm_vec.begin(), ctm_vec.end(), compare);

    timeProcess(ctm_vec);
    serve(ctm_vec, k);
    cout << setiosflags(ios::fixed) << setprecision(1);
    cout << average(ctm_vec);
}
bool compare(Customer c1, Customer c2) {
    if (c1.arrive < c2.arrive)
        return true;
    else
        return false;
}
void serve(vector<Customer>&ctm_vec,int k) {
    int i, j, min, m_index;
    int *window = new int[k];
    for (j = 0; j < k; j++) {
        window[j] = 8 * 3600;
    }
    for (i = 0; i < ctm_vec.size(); i++) {
        min = INT_MAX;
        if (ctm_vec[i].arr_int>61200)break;
        if (ctm_vec[i].process>60)ctm_vec[i].process = 60;

        for (j = 0; j < k; j++) {
            if (min > window[j]) {
                min = window[j];
                m_index = j;
            }
        }
        ctm_vec[i].get_server = window[m_index] > ctm_vec[i].arr_int ? window[m_index] : ctm_vec[i].arr_int;
        window[m_index] = ctm_vec[i].get_server + ctm_vec[i].process * 60;
    }

}
void timeProcess(vector<Customer>&ctm_vec) {
    int interval = 0, i;
    int hour, min, sec;
    for (i = 0; i < ctm_vec.size(); i++) {
        hour = (ctm_vec[i].arrive[0] - '0') * 10 + (ctm_vec[i].arrive[1] - '0');
        min= (ctm_vec[i].arrive[3] - '0') * 10 + (ctm_vec[i].arrive[4] - '0');
        sec= (ctm_vec[i].arrive[6] - '0') * 10 + (ctm_vec[i].arrive[7] - '0');
        interval = hour * 3600 + min * 60 + sec;
        ctm_vec[i].arr_int = interval;
    }
}
float average(vector<Customer>&ctm_vec) {
    int i, count=0;
    float total = 0;
    for (i = 0; i < ctm_vec.size(); i++) {
        if (ctm_vec[i].get_server != -1) {
            total += (ctm_vec[i].get_server - ctm_vec[i].arr_int);
            count++;
        }
    }
    if (count == 0)return total;
    else
        return total / (60 * count);
}
基于开源大模型的教学实训智能体软件,帮助教师生成课前备课设计、课后检测问答,提升效率与效果,提供学生全时在线练习与指导,实现教学相长。 智能教学辅助系统 这是一个智能教学辅助系统的前端项目,基于 Vue3+TypeScript 开发,使用 Ant Design Vue 作为 UI 组件库。 功能模块 用户模块 登录/注册功能,支持学生和教师角色 毛玻璃效果的登录界面 教师模块 备课与设计:根据课程大纲自动设计教学内容 考核内容生成:自动生成多样化考核题目及参考答案 学情数据分析:自动化检测学生答案,提供数据分析 学生模块 在线学习助手:结合教学内容解答问题 实时练习评测助手:生成随练题目并纠错 管理模块 用户管理:管理员/教师/学生等用户基本管理 课件资源管理:按学科列表管理教师备课资源 大屏概览:使用统计、效率指数、学习效果等 技术栈 Vue3 TypeScript Pinia 状态管理 Ant Design Vue 组件库 Axios 请求库 ByteMD 编辑器 ECharts 图表库 Monaco 编辑器 双主题支持(专业科技风/暗黑风) 开发指南 # 安装依赖 npm install # 启动开发服务器 npm run dev # 构建生产版本 npm run build 简介 本项目旨在开发一个基于开源大模型的教学实训智能体软件,帮助教师生成课前备课设计、课后检测问答,提升效率与效果,提供学生全时在线练习与指导,实现教学相长。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值