1017 Queueing at Bank (25分)

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 (≤10​4​​) - 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
#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
#include<algorithm>
#include<map>


using namespace std;

struct customer{
    int hour;
    int min;
    int sec;
    long long int totol_sec;
    long long int start_sec;
    int served_min;
    long long int end_sec;
    int window;

};
bool cmp_end_time(vector<customer> a, vector<customer> b){

    return a.front().end_sec < b.front().end_sec;

}
bool cmp(customer a, customer b){

    return a.totol_sec < b.totol_sec;

}
int main(){

    vector<customer> customers;
    

    int n, k;



    cin >> n >> k;
    vector<customer> que[k];
    for(int i = 0; i < n; i++){

        customer temp_customer;
        int hour;
        int min;
        int sec;
        int serve_min;

        scanf("%d:%d:%d", &hour, &min, &sec);
        cin >> serve_min;
        temp_customer.hour = hour;
        temp_customer.min = min;
        temp_customer.sec = sec;
        temp_customer.served_min = serve_min;
        temp_customer.totol_sec = hour * 3600 + min * 60 + sec;
        customers.push_back(temp_customer);
    
    }

    sort(customers.begin(), customers.end(), cmp);


    // map<int, long long int> start_sec;
    // for(int i = 0; i < k; i++){

    //     start_sec[i] = 8 * 3600;

    // }

    long long int start_sec_temp[101];
    for(int i = 0; i < k; i++){

        start_sec_temp[i] = 28800;

    }

    for(int i = 0; i < min(k, n); i++){

        long long int max_start_sec;

        max_start_sec = max(start_sec_temp[i], customers[i].totol_sec);
        customers[i].start_sec = max_start_sec;
        customers[i].window = i;
        customers[i].end_sec = max_start_sec + customers[i].served_min * 60;
        que[i].push_back(customers[i]);
        start_sec_temp[i] = customers[i].end_sec;

    }
    sort(que, que + min(k, n), cmp_end_time);
    // cout << "##########" << endl;
    // for(int i = 0; i < k; i++){

        
    //     cout << que[i][0].hour << " " << que[i][0].min << " " << que[i][0].sec << " " << que[i][0].served_min << " " << que[i][0].totol_sec << " " << que[i][0].end_sec << endl;
    // }

    for(int i = k; i < n; i++){

        int flag = 0;

        long long int min = que[0][0].end_sec;

        for(int j = 0; j < k; j++){
            

            if(min > que[j][0].end_sec){

                min = que[j][0].end_sec;
                flag = j;
            }
                


        }
        long long int max_start_sec;

        max_start_sec = max(start_sec_temp[que[flag][0].window], customers[i].totol_sec);
        customers[i].start_sec = max_start_sec;
        customers[i].window = que[flag][0].window;
        customers[i].end_sec = max_start_sec + customers[i].served_min * 60;

        
            
        que[flag].pop_back();
        que[flag].push_back(customers[i]);
        
        start_sec_temp[que[flag][0].window] = customers[i].end_sec;
        // sort(que, que + k, cmp_end_time);

        // cout << "==========" << endl;
        // for(int i = 0; i < k; i++){

            
        //     cout << que[i][0].hour << " " << que[i][0].min << " " << que[i][0].sec << " " << que[i][0].served_min << " " << que[i][0].totol_sec << " " << que[i][0].end_sec << endl;
        // }
       


    }
    int count = 0;
    long long int wait_sec = 0;
    for(int i = 0; i < customers.size(); i++){
        if(customers[i].totol_sec <= 61200){

            count ++;
            wait_sec += (customers[i].start_sec - customers[i].totol_sec);

        }



    }

    printf("%.1f", wait_sec / count / 60.0);
    
    std::cout << endl;
    // for(int i = 0; i < customers.size(); i++){


    //      cout << customers[i].hour << " " << customers[i].min << " " << customers[i].sec << " " << customers[i].served_min << endl;
    // }



    return 0;
}
发布了32 篇原创文章 · 获赞 0 · 访问量 445

猜你喜欢

转载自blog.csdn.net/zbchenchanghao/article/details/103994064