Bootstrap

【数据结构OJ】D : DS队列--银行单队列多窗口模拟

Description

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

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

Input

输入第1行给出正整数  N(≤1000),为顾客总人数;

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

Output

在一行中输出平均等待时间(输出到小数点后1位)、最长等待时间、最后完成时间,之间用1个空格分隔,行末不能有多余空格。

Sample

samplein

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

sampleout

6.2 17 62

AC代码 

#include <iostream>
#include <vector>
#include <queue>
#include <iomanip>
#include <algorithm>
using namespace std;

// 定义顾客类,包含到达时间和服务时间
class Customer {
public:
    int arrive_time;
    int serve_time;
    Customer(int a, int s) : arrive_time(a), serve_time(s) {}
};

int main() {
    int n, k;
    cin >> n; // 输入顾客数量

    // 读取顾客信息并存储在customers向量中
    vector<Customer> customers;
    for (int i = 0; i < n; i++) {
        int arrive, serve;
        cin >> arrive >> serve;
        customers.push_back(Customer(arrive, serve));
    }

    // 按照到达时间对顾客进行排序
    sort(customers.begin(), customers.end(), [](const Customer& a, const Customer& b) {
        return a.arrive_time < b.arrive_time;
    });

    cin >> k; // 输入窗口数量
    priority_queue<int, vector<int>, greater<int>> windowQueue;
    vector<int> finishTime(k, 0); // 存储每个窗口的结束时间

    double totalWaitTime = 0.0; // 总等待时间
    int maxWaitTime = 0; // 最大等待时间

    // 处理每个顾客
    for (const Customer& customer : customers) {
        int windowStart = max(finishTime[0], customer.arrive_time); // 窗口开始服务的时间
        int windowFinish = windowStart + customer.serve_time; // 窗口结束服务的时间

        totalWaitTime += (windowStart - customer.arrive_time); // 更新总等待时间

        maxWaitTime = max(maxWaitTime, windowStart - customer.arrive_time); // 更新最大等待时间

        finishTime[0] = windowFinish; // 更新第一个窗口的结束时间
        make_heap(finishTime.begin(), finishTime.end(), greater<int>()); // 维护最小堆性质
    }

    // 计算最后一个窗口的结束时间(使用finishTime中的最大值)
    int lastWindowFinish = *max_element(finishTime.begin(), finishTime.end());

    // 输出结果
    cout << fixed << setprecision(1) << totalWaitTime / n << " " << maxWaitTime << " " << lastWindowFinish << endl;

    return 0;
}

写的我都嘎了,不知道你们看不看得懂

;