银行排队问题之单队列多窗口服务 (25 分)

假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙。当有窗口空闲时,下一位顾客即去该窗口处理事务。当有多个窗口可选择时,假设顾客总是选择编号最小的窗口。

本题要求输出前来等待服务的N位顾客的平均等待时间、最长等待时间、最后完成时间,并且统计每个窗口服务了多少名顾客。

输入格式:

输入第1行给出正整数N(≤1000),为顾客总人数;随后N行,每行给出一位顾客的到达时间T和事务处理时间P,并且假设输入数据已经按到达时间先后排好了顺序;最后一行给出正整数K(≤10),为开设的营业窗口数。这里假设每位顾客事务被处理的最长时间为60分钟。

输出格式:

在第一行中输出平均等待时间(输出到小数点后1位)、最长等待时间、最后完成时间,之间用1个空格分隔,行末不能有多余空格。

在第二行中按编号递增顺序输出每个窗口服务了多少名顾客,数字之间用1个空格分隔,行末不能有多余空格。

输入样例:

9
0 20
1 15
1 61
2 10
10 5
10 3
30 18
31 25
31 2
3

输出样例:

6.2 17 61
5 3 1

参考代码:

#include<bits/stdc++.h>
using namespace std;
struct Customers {
    int arrived_time;
    int need_time;
    int wait_time;
};
int counter_time[10] = {0};
int counter_serve[10] = {0};
int main() {
    vector<Customers> customers;
    int n, k;
    cin>>n;
    for(int i = 0; i < n; i++) {
        Customers t;
        cin>>t.arrived_time>>t.need_time;
        if(t.need_time>60) t.need_time = 60;
        customers.push_back(t);
    }
    cin>>k;
    for(int i = 0; i < n; i++) {
        int choose = 0;
        for(int j = 0; j < k; j++) {
            if(counter_time[choose] > counter_time[j]) choose = j;
            if(customers[i].arrived_time >= counter_time[choose]) break;
        }
        if(customers[i].arrived_time >= counter_time[choose]) {
            counter_time[choose] = customers[i].arrived_time + customers[i].need_time;
            customers[i].wait_time = 0;
        } else {
            customers[i].wait_time = counter_time[choose] - customers[i].arrived_time;
            counter_time[choose] += customers[i].need_time;
        }
        counter_serve[choose]++;
    }
    double sum = 0;
    double longest_wait = 0;
    double longest_complete = 0;
    for(int i = 0; i < n; i++) {
        sum += customers[i].wait_time;
        if(customers[i].wait_time > longest_wait) longest_wait = customers[i].wait_time;
    }
    for(int i = 0; i < k; i++) {
        if(counter_time[i] > longest_complete) longest_complete = counter_time[i];
    }
    printf("%.1lf ",sum/n);
    cout<<longest_wait<<" "<<longest_complete<<endl;
    for(int i = 0; i < k; i++) {
        if(i != 0) cout<<" ";
        cout<<counter_serve[i];
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值