PAT1017. Queueing at Bank (25)

来源:互联网 发布:桌面规划软件 编辑:程序博客网 时间:2024/05/22 17:16

题目如下:

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 (<=10000) - 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 307:55:00 1617:00:01 207:59:59 1508:01:00 6008:00:00 3008:00:02 208:03:00 10
Sample Output:
8.2
题意就是给出顾客人数及窗口数,根据到达时间和处理时间算出平均等待时间。和之前银行排队的题目PAT1014也比较类似。我也是用模拟了整个排队过程,用开始时间和结束时间来计算出整个的等待时间。要注意一点就是8点前的等待时间也要计算,我一开始忘记计算了。代码如下:

#include <iostream>#include <vector>#include <string>#include <queue>#include <algorithm>#include <iomanip>#define START 28800#define END 61200using namespace std;struct Customer {int arriveTime;int processTime;int waitingTime;int startTime;int endTime;};int N, K;vector<Customer> customers;queue<Customer> windows[101];double total = 0;int countNum = 0;bool compara(Customer a, Customer b){if (a.arriveTime < b.arriveTime)return true;elsereturn false;}int findEmptyWindow(){for (int i = 0;i < K;i++){if (windows[i].size() == 0)return i;}return -1;}int findEarliestWindow(){int min = -1;for (int i = 0;i < K;i++){if (windows[i].size() != 0){min = i;break;}}if (min == -1)return min;for (int i = 0;i < K;i++){if (windows[i].size() != 0 && windows[min].front().endTime > windows[i].front().endTime){min = i;}}return min;}void enterWindow(Customer c){if (c.arriveTime <= END){int index = findEarliestWindow();if (index != -1)//不是全空{if (c.arriveTime >= windows[index].front().endTime){total += (double)windows[index].front().waitingTime / 60;countNum += 1;c.startTime = c.arriveTime;c.waitingTime = 0;c.endTime = c.startTime + c.processTime;windows[index].pop();windows[index].push(c);}else{int i = findEmptyWindow();if (i != -1)//有空窗口{if (c.arriveTime < START){c.startTime = START;}else{c.startTime = c.arriveTime;}c.waitingTime = c.startTime - c.arriveTime;c.endTime = c.startTime + c.processTime;windows[i].push(c);return;}else{total += (double)windows[index].front().waitingTime / 60;countNum += 1;c.startTime = windows[index].front().endTime;c.waitingTime = c.startTime - c.arriveTime;c.endTime = c.startTime + c.processTime;windows[index].pop();windows[index].push(c);}}}else  //全空{if (c.arriveTime < START)c.startTime = START;elsec.startTime = c.arriveTime;c.waitingTime = c.startTime - c.arriveTime;c.endTime = c.startTime + c.processTime;windows[0].push(c);}}}void outWindow(){for (int i = 0;i < K;i++){if (windows[i].size() != 0){total += (double)windows[i].front().waitingTime / 60;countNum += 1;windows[i].pop();}}}int main(){cin >> N >> K;string s;Customer temp;for (int i = 0;i < N;i++){cin >> s >> temp.processTime;temp.arriveTime = atoi(s.substr(0, 2).c_str()) * 60 * 60 + atoi(s.substr(3, 5).c_str()) * 60 + atoi(s.substr(6, 8).c_str());temp.processTime *= 60;customers.push_back(temp);}sort(customers.begin(),customers.end(),compara);for (int i = 0;i < N;i++){enterWindow(customers[i]);}outWindow();cout << fixed << setprecision(1) << total / countNum << endl;}

一次通过全部用例。


0 0
原创粉丝点击