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 (≤) - the total number of customers, and K (≤) - 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
题目分析:(写了好长时间没写出来,还是去看柳神的了)
将时间处理为秒数读入 将符合条件的读入vector中
对于窗口,每次选取一个时间最小的窗口进行处理
如果 要处理的顾客到达时间比 那个时间最小的窗口还小 无需等待 直接处理并更新窗口时间
反之 记录等待时间
 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<iostream>
 3 #include<vector>
 4 #include<queue>
 5 #include<algorithm>
 6 using namespace std;
 7 struct Node
 8 {
 9     int come, time;
10 }customer;
11 bool compare(const Node& a, const Node& b)
12 {
13     return a.come < b.come;
14 }
15 int main()
16 {
17     int N, K;
18     int Size = 0;
19     cin >> N >> K;
20     vector<Node>Custom;
21     for (int i = 0; i < N; i++)
22     {
23         int hh, mm, ss, tt;
24         scanf("%d:%d:%d%d", &hh, &mm, &ss, &tt);
25         int cometime = hh * 3600 + mm * 60 + ss;
26         if (cometime > 61200)continue;
27         customer = { cometime,tt*60};
28         Custom.push_back(customer);
29         Size++;
30     }
31     sort(Custom.begin(), Custom.end(), compare);
32     vector<int>window(K, 28800);
33     double SumTime=0;
34     for (int i = 0; i < Size; i++)
35     {
36         int Min = window[0];
37         int Minp = 0;
38         for (int j = 1; j < K; j++)
39         {
40             if (window[j] < Min)
41             {
42                 Min = window[j];
43                 Minp = j;
44             }
45         }
46         if (Custom[i].come >= window[Minp])
47             window[Minp] = Custom[i].come + Custom[i].time;
48         else
49         {
50             SumTime += window[Minp] - Custom[i].come;
51             window[Minp] += Custom[i].time;
52         }
53     }
54     if (Size)
55         printf("%.1f", SumTime / (Size * 1.0) / 60);
56     else
57         printf("0.0");
58     return 0;
59 }
View Code
原文地址:https://www.cnblogs.com/57one/p/11922172.html