DS队列----银行单队列多窗口模拟

题目描述

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

本题要求输出前来等待服务的N位顾客的平均等待时间、最长等待时间、最后完成时间。

输入

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

输出

在一行中输出平均等待时间(输出到小数点后1位)、最长等待时间、最后完成时间,之间用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 62\n
\n

 

#include<iostream>
#include<string>
#include<queue>
#include<iomanip>
using namespace std;

int main(){

    int wait_time[3] ;
    for(int i = 0 ; i < 3 ; i++ ){
        wait_time[i] = 0;
    }
    int t;
    cin>>t;
    int *t_arrive = new int [t];
    int *t_wait = new int [t];
    queue<int> my_member[2];

    for(int i = 0 ; i < t ; i++ ){

        cin>>t_arrive[i]>>t_wait[i];
        my_member[0].push(t_arrive[i]);
        my_member[1].push(t_wait[i]);
    }

    int *arrive_wait_time = new int [t];
    bool flag_work=0;
    int statics_time = 0;

    int i_order = 0;
    for( ;  ; statics_time++ ){
        flag_work=0;

        for(int i = 0 ; i < 3 ; i++ ){
            if(statics_time>=wait_time[i]){
                if(!my_member[0].empty()){

                    int bank_wait=0;
                    if(my_member[0].front()<statics_time){
                        arrive_wait_time[i_order] = statics_time-my_member[0].front();
                        bank_wait = 0 ;
                    }
                    else{
                        arrive_wait_time[i_order] = 0;
                        bank_wait = my_member[0].front() - statics_time ;
                    }


                    wait_time[i]+=bank_wait+my_member[1].front();

//                    cout<<"now is "<<statics_time<<"  : "<<my_member[0].front()<<" , "<<my_member[1].front()<<" out "<<endl;
                    i_order++;
                    my_member[0].pop();
                    my_member[1].pop();
                }
            }

            if(wait_time[i]>statics_time)
                flag_work = 1;

        }//main action

        if(my_member[0].empty()&&flag_work==0)
            break;

    }

    int sum_time = 0;
    int max_time = 0;
    for(int i = 0 ; i < t ; i++ ){
        sum_time +=arrive_wait_time[i];
//        cout<<"wait_time ["<<i<<"] : "<<arrive_wait_time[i]<<endl;
        if(arrive_wait_time[i] > max_time)
            max_time = arrive_wait_time[i];

    }

    cout<<fixed<<setprecision(1)<<(double)(sum_time)/(double)(t);
    cout<<" "<<max_time<<" "<<statics_time<<endl;


    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值