PAT (Advanced Level) Practice 1014 Waiting in Line(30分)【模拟、优先队列】

Suppose a bank has N N N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are:

  • The space inside the yellow line in front of each window is enough to contain a line with M M M customers. Hence when all the N N N lines are full, all the customers after (and including) the ( N M + 1 ) (NM+1) (NM+1)st one will have to wait in a line behind the yellow line.
  • Each customer will choose the shortest line to wait in when crossing the yellow line. If there are two or more lines with the same length, the customer will always choose the window with the smallest number.
  • C u s t o m e r i Customer_i Customeri will take T i T_i Ti minutes to have his/her transaction processed.
  • The first N N N customers are assumed to be served at 8:00am.

Now given the processing time of each customer, you are supposed to tell the exact time at which a customer has his/her business done.

For example, suppose that a bank has 2 windows and each window may have 2 custmers waiting inside the yellow line. There are 5 customers waiting with transactions taking 1, 2, 6, 4 and 3 minutes, respectively. At 08:00 in the morning, C u s t o m e r 1 Customer_1 Customer1​​ is served at w i n d o w ​ 1 window_​1 window1 while C u s t o m e r 2 Customer_2 Customer2 is served at w i n d o w 2 window_2 window2. C u s t o m e r 3 Customer_3 Customer3 will wait in front of w i n d o w 1 window_1 window1 and C u s t o m e r 4 Customer_4 Customer4 will wait in front of w i n d o w 2 window_2 window2. C u s t o m e r 5 Customer_5 Customer5 will wait behind the yellow line.

At 08:01, C u s t o m e r 1 Customer_1 Customer1 is done and C u s t o m e r 5 Customer_5 Customer5 enters the line in front of w i n d o w 1 window_1 window1 since that line seems shorter now. C u s t o m e r 2 Customer_2 Customer2 will leave at 08:02, C u s t o m e r 4 Customer_4 Customer4 at 08:06, C u s t o m e r 3 Customer_3 Customer3 at 08:07, and finally C u s t o m e r 5 Customer_5 Customer5 at 08:10.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers: N N N (≤20, number of windows), M M M (≤10, the maximum capacity of each line inside the yellow line), K K K (≤1000, number of customers), and Q Q Q (≤1000, number of customer queries).

The next line contains K K K positive integers, which are the processing time of the K K K customers.

The last line contains Q Q Q positive integers, which represent the customers who are asking about the time they can have their transactions done. The customers are numbered from 1 to K K K.

Output Specification:

For each of the Q Q Q customers, print in one line the time at which his/her transaction is finished, in the format HH:MM where HH is in [08, 17] and MM is in [00, 59]. Note that since the bank is closed everyday after 17:00, for those customers who cannot be served before 17:00, you must output Sorry instead.

Sample Input:

2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7

Sample Output:

08:07
08:06
08:10
17:00
Sorry

题意

银行有n个窗口,每个窗口可以排队m人。有k名用户,给出每名用户的服务时间。银行在8点开始服务,如果有窗口还没有排满就入队,如果已经排满了就要站在黄线外等待。如果同时有多个窗口没有排满,选择窗口号小的那个排队。如果一个用户在17点前还没有开始服务,他就不会被服务。给出q个查询,计算客户服务结束时的时间。

思路

n*m个人可以直接在窗口前排队,第n*m+1名及以后的客户只能等待某个窗口完成一个服务之后排到这个窗口后面。可以用优先队列来得到最先完成服务的窗口。当一个人在某个窗口前排队时,他的开始服务时间就已经是确定的了。

代码

#include 
#include 
#include 
#include 
#include 

using namespace std;

#define time2Minute(h, mi) ((h) * 60 + (mi))

struct wq {
    int index;
    int time;

    bool operator>(const wq &b) const {
        if (time != b.time) return time > b.time;
        else return index > b.index;
    }
};

int n, m, k, q;
// 黄线之前的队列
queue<int> waitQ[25];
// 客户的处理时间 客户开始被服务的时间 窗口前没有人排队时的时间
int processTime[1005], serveTime[1005], availableTime[25];

int main() {
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios::sync_with_stdio(false);

    cin >> n >> m >> k >> q;
    for (int i = 1; i <= k; ++i) cin >> processTime[i];

    int startTime = time2Minute(8, 0);
    int endTime = time2Minute(17, 0);

    fill_n(serveTime, k + 1, endTime);
    fill_n(availableTime, 25, startTime);

    // 前n*m个用户直接进入黄线前等待被服务
    int nextCustomer = 1;  // 下一个排进黄线前的客户
    for (int i = 0; i < m and nextCustomer <= k; ++i) {
        for (int j = 0; j < n and nextCustomer <= k; ++j, ++nextCustomer) {
            waitQ[j].push(nextCustomer);
            serveTime[nextCustomer] = availableTime[j];
            availableTime[j] += processTime[nextCustomer];
        }
    }

    // 优先队列用来得到最先完成服务的窗口
    priority_queue<wq, vector<wq>, greater<wq>> pq;
    for (int i = 0; i < n; ++i) {
        int top = waitQ[i].front();
        pq.push({i, serveTime[top] + processTime[top]});
    }

    // 从第n*m+1个客户开始,需要等待某个窗口处理完成后才能进入黄线前排队
    while (nextCustomer <= k) {
        int queue = pq.top().index;

        pq.pop();
        waitQ[queue].pop();

        waitQ[queue].push(nextCustomer);
        serveTime[nextCustomer] = availableTime[queue];
        availableTime[queue] += processTime[nextCustomer];
        ++nextCustomer;

        int top = waitQ[queue].front();
        pq.push({queue, serveTime[top] + processTime[top]});
    }

    for (int i = 0; i < q; ++i) {
        int x;
        cin >> x;

        if (serveTime[x] < endTime) {
            int doneTime = serveTime[x] + processTime[x];
            cout << setw(2) << setfill('0') << doneTime / 60 << ":"
                 << setw(2) << setfill('0') << doneTime % 60 << "\n";
        } else
            cout << "Sorry\n";
    }
}

你可能感兴趣的:(PAT)