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.
Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤104) - 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.
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.
思路最奇特的就是将所有窗口的值作为08:00:00,然后push到优先队列中。这样顾客与top的值进行比较,也不用担心在8点前到的顾客的等待时间没有算上。Orz
#include <iostream> #include <vector> #include <algorithm> #include <iomanip> #include <queue> using namespace std; struct node { int arriving_time; int processing_time; }; node customers[10005]; int time_convert_seconds(string time) { int res = 0; int hour = (time[0] - '0') * 10 + time[1] - '0'; int mins = (time[3] - '0') * 10 + time[4] - '0'; int seconds = (time[6] - '0') * 10 + time[7] - '0'; res = hour * 3600 + mins * 60 + seconds; return res; } bool cmp(node a, node b) { return a.arriving_time < b.arriving_time; } int main() { int n, k; cin >> n >> k; int cnt = 0; // cnt服务人数,total为总的等待时间 double total = 0.0; string time; int processing_time; for (int i = 1; i <= n; i++) { cin >> time >> processing_time; if (time_convert_seconds(time) <= 61200) { // 超过17点不服务了 customers[++cnt].processing_time = processing_time * 60; customers[cnt].arriving_time = time_convert_seconds(time); } } sort(customers + 1, customers + 1 + cnt, cmp); priority_queue<int, vector<int>, greater<int>> q; for (int i = 1; i <= k; i++) q.push(28800); for (int i = 1; i <= cnt; i++) { if (customers[i].arriving_time < q.top()) { // 说明顾客到了,但是窗口没有空闲,所以等待 int temp = q.top(); total += q.top() - customers[i].arriving_time; q.pop(); q.push(temp + customers[i].processing_time); } else { q.pop(); q.push(customers[i].arriving_time + customers[i].processing_time); } } if (cnt != 0) { cout << fixed << setprecision(1) << total / (60 * cnt) << endl; } else { cout << "0.0" << endl; } return 0; }