最开始用了优先队列写,准备提交的时候看到说当有多个窗口可用的时候客户会选择序号最小的窗口,故还是暴力搜一遍好了

// PTA 7-1 银行排队问题之单队列多窗口服务
#include <iostream>
#include <iomanip>
#include <vector>

#define ARRIVE_TIME first
#define TIME_COST second

const int inf = 0x3f3f3f;

using namespace std;

struct serviceWindow
{
    int availableTime;
    int id;

    serviceWindow(int id_)
    {
        availableTime = 0;
        id = id_;
    }
};

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int customerCount;
    cin >> customerCount;
    vector<pair<int, int>> customers(customerCount);
    for (int i = 0; i < customerCount; i++)
    {
        cin >> customers[i].ARRIVE_TIME >> customers[i].TIME_COST;
        customers[i].TIME_COST = min(customers[i].TIME_COST, 60);
    }
    int windowsCount;
    cin >> windowsCount;
    vector<serviceWindow*> windows(windowsCount);
    vector<int> servedCount(windowsCount, 0);
    for (int i = 0; i < windowsCount; i++)
    {
        windows[i] = new serviceWindow(i);
    }
    double totWaitTimeInMinutes = 0;
    int maxWaitTimeInMinutes = 0;
    for (auto& customer : customers)
    {
        int minAvailableWindowID = windowsCount;
        serviceWindow* earliestAvailableWindow = nullptr;
        for (auto& window : windows)
        {
            if (earliestAvailableWindow == nullptr)
            {
                earliestAvailableWindow = window;
                minAvailableWindowID = window->id;
            }
            else if (window->availableTime <= earliestAvailableWindow->availableTime)
            {
                if (earliestAvailableWindow->availableTime <= customer.ARRIVE_TIME)
                {
                    if (window->id < minAvailableWindowID)
                    {
                        earliestAvailableWindow = window;
                        minAvailableWindowID = window->id;
                        break;
                    }
                }
                else if (window->availableTime ^ earliestAvailableWindow->availableTime)
                {
                    earliestAvailableWindow = window;
                    minAvailableWindowID = window->id;
                }
            }
        }
        if (earliestAvailableWindow != nullptr)
        {
            if (earliestAvailableWindow->availableTime > customer.ARRIVE_TIME)
            {
                totWaitTimeInMinutes += earliestAvailableWindow->availableTime - customer.ARRIVE_TIME;
                maxWaitTimeInMinutes = max(maxWaitTimeInMinutes, earliestAvailableWindow->availableTime - customer.ARRIVE_TIME);
                earliestAvailableWindow->availableTime += customer.TIME_COST;
                ++servedCount[minAvailableWindowID];
            }
            else
            {
                earliestAvailableWindow->availableTime = customer.ARRIVE_TIME + customer.TIME_COST;
                ++servedCount[minAvailableWindowID];
            }
        }
    }
    cout << fixed << setprecision(1) << totWaitTimeInMinutes / customerCount << " ";
    cout << maxWaitTimeInMinutes << " ";
    int lastFinishTime = 0;
    for (auto& window : windows)
    {
        lastFinishTime = max(lastFinishTime, window->availableTime);
    }
    cout << lastFinishTime << "\n";
    for (int i = 0; i < windowsCount; i++)
    {
        cout << servedCount[i];
        if (i ^ (windowsCount - 1))
        {
            cout << " ";
        }
    }
    cout << "\n";
    return 0;
}

 

发表回复

您的电子邮箱地址不会被公开。