Zhejiang University Edition "Data Structure (2nd Edition)" Question Collection-Exercise 8.1

Exercise 8.1 Single-queue multi-window service of bank queuing problem (25point(s))

Suppose that the bank has K windows to provide services, and a yellow line is set in front of the window, and all customers line up in a long line after the yellow line according to the arrival time. When a window is free, the next customer will go to the window to process the transaction. When there are multiple windows to choose from, it is assumed that the customer always chooses the window with the lowest number.

This question requires to output the average waiting time, longest waiting time, and final completion time of N customers who came to wait for service, and count how many customers are served by each window.

Example:

#include <iostream>
#include <vector>
#include <cstdio>

using namespace std;

typedef struct CustomerType {
    int arrive;
    int used;
    int wait;
} *Customer;

typedef struct WindowType {
    int sum;
    int final;
} *Window;


int main()
{
    int N;
    cin >> N;
    Customer c = new CustomerType[N];
    for(int i = 0; i < N; i++) {
        cin >> c[i].arrive >> c[i].used;
        if(c[i].used > 60) c[i].used = 60;
    }
    int K;
    cin >> K;
    Window w = new WindowType[K];
    for(int i = 0; i < K; i++) w[i].final = 0;
    for(int i = 0; i < N; i++) {
        for(int h = 0; h < K; h++) {
            if(w[h].final < c[i].arrive) {
                w[h].final = c[i].arrive;
            }
        }
        int idle = 0;
        int minvalue = w[idle].final;
        for(int h = 1; h < K; h++) {
            if(w[h].final < minvalue) {
                idle = h;
                minvalue = w[h].final;
            }
        }
        w[idle].sum++;
        c[i].wait = w[idle].final - c[i].arrive;
        w[idle].final = w[idle].final + c[i].used;
    }
    int maxvalue = 0;
    int sum = 0;
    for(int i = 0; i < N; i++) {
        sum += c[i].wait;
        if(c[i].wait > maxvalue) maxvalue = c[i].wait;
    }
    printf("%.1f %d", (double)sum/N, maxvalue);
    maxvalue = 0;
    for(int h = 0; h < K; h++) {
        if(w[h].final > maxvalue) maxvalue = w[h].final;
    }
    cout << ' ' << maxvalue << endl;
    bool space = false;
    for(int h = 0; h < K; h++) {
        if(!space) space = true;
        else cout << ' ';
        cout << w[h].sum;
    }
    delete[] w;
    delete[] c;
    return 0;
}

Ideas:

Record the end time of each window, compare it with the arrival time of the next customer first, update the time, and then select the idle window that ends earliest from K windows, and update the end time.

Guess you like

Origin blog.csdn.net/u012571715/article/details/113385657