PAT 1017. Queueing at Bank (25)

1017. Queueing at Bank (25)

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 3
07:55:00 16
17:00:01 2
07:59:59 15
08:01:00 60
08:00:00 30
08:00:02 2
08:03:00 10
Sample Output:
8.2

 

  1 #include <iostream>
  2 #include <set>
  3 #include <string>
  4 #include <sstream>
  5 #include <algorithm>
  6 #include <iomanip>
  7 
  8 using namespace std;
  9 
 10 struct Customer 
 11 {
 12     string arrivingTime;
 13     int serDuration;
 14     string serTime;
 15 };
 16 
 17 Customer customers[10000];
 18 set<string> inService;
 19 
 20 bool cmp(const Customer& c1, const Customer& c2)
 21 {
 22     return c1.arrivingTime < c2.arrivingTime;
 23 }
 24 
 25 string AddDuration(string start, int duration)
 26 {
 27     for (int i = 0; i < start.size(); i++)
 28         if (start[i] == ':')
 29             start[i] = ' ';
 30     int h0, m0, s0;
 31     stringstream ss;
 32     ss << start;
 33     ss >> h0 >> m0 >> s0;
 34     m0 += duration;
 35     h0 = h0 + m0 / 60;
 36     m0 = m0 % 60;
 37     string res = string(1, h0 / 10 + '0') + string(1, h0 % 10 + '0') + ":" + string(1, m0 / 10 + '0') + \
 38         string(1, m0 % 10 + '0') + ":" + string(1, s0 / 10 + '0') + string(1, s0 % 10 + '0');
 39 
 40     return res;
 41 }
 42 
 43 double GetDuration(string start, string end)
 44 {
 45     for (int i = 0; i < start.size(); i++)
 46         if (start[i] == ':')
 47             start[i] = end[i] = ' ';
 48     int h1, h2, m1, m2, s1, s2;
 49     stringstream ss;
 50     ss << start;
 51     ss >> h1 >> m1 >> s1;
 52     ss.clear();
 53     ss << end;
 54     ss >> h2 >> m2 >> s2;
 55 
 56     return (h2 - h1) * 60 + (m2 - m1) + 1.0*(s2 - s1) / 60;
 57 }
 58 
 59 int main()
 60 {
 61     int customerNum, windowsNum;
 62     cin >> customerNum >> windowsNum;
 63 
 64     for (int i = 0; i < customerNum; i++)
 65     {
 66         cin >> customers[i].arrivingTime >> customers[i].serDuration;
 67         if (customers[i].arrivingTime >= "17:00:00")    
 68         {
 69             customerNum--;
 70             i--;
 71         }
 72         if (customers[i].serDuration > 60)
 73             customers[i].serDuration = 60;
 74     }
 75     sort(customers, customers + customerNum, cmp);
 76 
 77     for (int i = 0; i < windowsNum && i < customerNum; i++)    //initial the first group customer served
 78     {
 79         if (customers[i].arrivingTime <= "08:00:00")
 80             customers[i].serTime = "08:00:00";
 81         else
 82             customers[i].serTime = customers[i].arrivingTime;
 83         inService.insert(AddDuration(customers[i].serTime, customers[i].serDuration));
 84     }
 85     int wait = customerNum > windowsNum ? windowsNum : customerNum;    //the customer who is waiting at first
 86 
 87     while (!inService.empty())
 88     {
 89         string first = *(inService.begin());
 90         inService.erase(inService.begin());
 91         if (wait < customerNum)
 92         {
 93             if (first > customers[wait].arrivingTime)    //arriving befroe first customer finishing
 94             {
 95                 inService.insert(AddDuration(first, customers[wait].serDuration));
 96                 customers[wait].serTime = first;
 97             }
 98             else    //arriving after first customer finishing
 99             {
100                 inService.insert(AddDuration(customers[wait].arrivingTime, customers[wait].serDuration));
101                 customers[wait].serTime = customers[wait].arrivingTime;
102             }
103             wait++;
104         }
105     }
106 
107     double totalWait = 0;
108     for (int i = 0; i < customerNum; i++)
109         totalWait += GetDuration(customers[i].arrivingTime, customers[i].serTime);
110 
111     cout << fixed << setprecision(1)  << totalWait / customerNum;
112 }

 

posted @ 2015-08-22 22:36  JackWang822  阅读(218)  评论(0)    收藏  举报