DS single-queue queue ---- Bank multi-window simulation

Title Description

Assuming that banks have K windows provide services, set up a yellow line before the window, all customers in arrival time after the yellow line arranged in a long queue. When a window is idle, the next customer that is the window to process transactions. Alternatively, when a plurality of windows, assuming the customer is always selects the lowest numbered window.

This question came to wait for service request output of the average N-bit customer waiting time, the maximum waiting time, to complete the final time.

 

Entry

Enter line 1 gives a positive integer N ( ≤), the total number of customers; followed by N lines, each line gives every customer's arrival time Tand transaction time P, and assume that the input data according to the time of arrival has already lined up order ; the last line gives a positive integer K ( ≤), the number of windows opened to business.

 

Export

The average waiting time the output line (output to one decimal place), the maximum waiting time, time last completed, separated by a space between the end of the line can not have extra space.

 

Sample input

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

Sample Output

6.2 17 62

prompt

#include<iostream>
#include<queue>
#include<iomanip>
using namespace std;
int queindex(int total,queue<int>Q)
{
    int index=0;
    queue<int>temp=Q;
    while(!temp.empty())
    {
        temp.pop();
        index++;
    }
    return total-index+1;
}
  
int main ()
{
    int virclock=0;
    double averagewait=0;
    int maxwait=0;
    int alreadytime=0;
    Queue < int > arrivetime; /// customers to time 
    Queue < int > solvetime; /// do business time 
    int T;
    CIN >> T;
     int TIMEWAIT [T]; /// each customer waiting time, the initialization is 0 
    for ( int I = 0 ; I <T; I ++ )
        timewait[i]=0;
    for(int i=0;i<T;i++)
    {
        int n,t;
        cin>>n>>t;
        arrivetime.push(n);
        solvetime.push(t);
    }
    int K;
    cin>>K;
    queue<int>bank[K];
    int done=0;
    while(done==0)
    {
        for(int i=0;i<K;i++)
        {
            IF (Bank [I] .empty ()) /// bank gapped 
            {
                 IF (! arrivetime.empty () && arrivetime.front () <= virclock) /// was bank 
                {
                    bank[i].push(solvetime.front());
                    arrivetime.pop();
                    solvetime.pop ();
                }
            }
        }
        int bankemp = 0 ; /// bank and several vacancies, vacancy when a judgment is 0 if there are people waiting 
        for ( int I = 0 ; I <K; I ++ )
             IF (Bank [I] .empty () )
                bankemp ++ ;
        if (bankemp == 0 )
        {
            Queue < int > the TEMP = arrivetime;
             the while (! temp.empty () && temp.front () <= virclock) /// If someone like he was the first to find a few clients, array plus waiting time 
            {
                 int Number The = queindex ( T, temp);
                timewait[number-1]+=1;
                temp.pop();
            }
        }
  
        for ( int I = 0 ; I <K; I ++) /// the minute processing 
        {
             IF (! Bank [I] .empty ())
            {
                int temp=bank[i].front()-1;
                bank[i].pop();
                bank[i].push(temp);
                if(bank[i].front()==0)
                {
                    bank[i].pop();
                }
            }
        }
  
        int EMP = . 1 ; /// determining whether a complete end 
        for ( int I = 0 ; I <K; I ++ )
        {
            if(!bank[i].empty())
            {
                emp=0;
                break;
            }
        }
        if(emp==1&&arrivetime.empty()&&solvetime.empty())
            done=1;
  
        virclock ++; /// into the next min 
    }
     for ( int I = 0 ; I <T; I ++) /// calculates the average waiting time and the maximum waiting time 
    {
         IF (maxWait < TIMEWAIT [I])
            maxwait=timewait[i];
        averagewait+=timewait[i];
    }
    averagewait/=T;
    alreadytime=virclock;
    cout<<fixed<<setprecision(1)<<averagewait<<" ";
    cout<<maxwait<<" ";
    cout<<alreadytime<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/SZU-DS-wys/p/12180737.html